How to remove duplicates from multidimensional array in php [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP remove duplicate values from multidimensional array
i have an array like:
Array
(
[prom] => Array
(
[cab] => Array
(
[0] => Array
(
[code] => 01
[price1] => 1000
[price2] => 2000
[available] => 2
[max] => 2
[gca] => 2
)
[1] => Array
(
[code] => 04
[price1] => 870
[price2] => 2500
[available] => 3
[max] => 4
[gca] => 10
)
[2] => Array
(
[code] => 01
[price1] => 1000
[price2] => 2000
[available] => 2
[max] => 2
[gca] => 2
)
[3] => Array
(
[code] => 05
[price1] => 346
[price2] => 1022
[available] => 10
[max] => 2
[gca] => 20
)
)
[cab1] => Array........
)
[prom1] = Array....
)
What i have to do is to remove duplicates inside every [cab*] array..
so to have something like:
Array
(
[prom] => Array
(
[cab] => Array
(
[0] => Array
(
[code] => 01
[price1] => 1000
[price2] => 2000
[available] => 2
[max] => 2
[gca] => 2
)
[1] => Array
(
[code] => 04
[price1] => 870
[price2] => 2500
[available] => 3
[max] => 4
[gca] => 10
)
[2] => Array
(
[code] => 05
[price1] => 346
[price2] => 1022
[available] => 10
[max] => 2
[gca] => 20
)
)
[cab1] => Array........
)
[prom1] = Array....
)
In know that there is array_unique combined with array_map to remove duplicates.. but i know that it works only on 2D array.. what can i do? can someone help me pls? thanks!!!

Try it:-
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = super_unique($value);
}
}
return $result;
}
$input = super_unique($myarray);
echo "<pre>";
print_r($input);

Related

How to map multidimensional array

I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example

Sort a multidimensional an array with numeric keys but keep the keys same just change the order [duplicate]

This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)

combine array using child index value

I have following array output.i want to combine them according to tax class id Please check the below array and another array format which i want is 2nd array
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-S
[qty_ordered] => 5
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-M
[qty_ordered] => 9
)
[2] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-L
[qty_ordered] => 9
)
[3] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-XL
[qty_ordered] => 9
)
I want to combine array those have same tax_class_id like below
[0] => Array(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-S
[qty_ordered] => 5
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-L
[qty_ordered] => 9
)
)
[1] => Array(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-M
[qty_ordered] => 9
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-XL
[qty_ordered] => 9
)
)
How can i get array in above format. where sub array has same tax_class_id.
Solution....
foreach($array as $row){
$new[$row['tax_class_id']][] = $row;
}
echo "<pre>";print_r($new);
Result
Array
(
[18] => Array
(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-S
[qty_ordered] => 5
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-L
[qty_ordered] => 9
)
)
[28] => Array
(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-M
[qty_ordered] => 9
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-XL
[qty_ordered] => 9
)
)
)
You could do a combination of "usort()" and creating a new array via loop from the resulting usort() array.
usort() example to get you started (based on Sort multi-dimensional array by specific key )
<!DOCTYPE html>
<html>
<body>
<?php
$age = array(
array(
"id"=>"1",
"cat_id"=>"11",
"text_id"=>"43"
),
array(
"id"=>"2",
"cat_id"=>"22",
"text_id"=>"22"
),
array(
"id"=>"3",
"cat_id"=>"33",
"text_id"=>"43"
),
array(
"id"=>"4",
"cat_id"=>"44",
"text_id"=>"43"
),
array(
"id"=>"5",
"cat_id"=>"55" ,
"text_id"=>"22"
)
);
function cmp($a, $b)
{
return strcmp($a['text_id'], $b['text_id']);
}
usort($age, "cmp");
// Just to show that the array has been sorted
echo '<pre>';
print_r($age);
echo '</pre>';
// Create new array using loops here
//......
?>
</body>
</html>
You can do a loop like this:
$val = [];
$newArray = []
foreach ($products as $product) {
$key = array_search($product['tax_class_id'],$val);
if(!$key) {
$val[] = $product['tax_class_id'];
$key = array_search($product['tax_class_id'],$val);
}
$newArray[$key][] = $product;
}
demo:https://ideone.com/OhRieF#stdin

How to reformat an array using php?

I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 50
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 2
[category_id] => 2
[product_id] => 2
[amount] => 15
[cost] => 3000
[comment] =>
[created] => 2015-06-22 18:10:36
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
I want to remove duplicate value of same product_id inside [Import][product_id] but want sum [Import][amount]. My expected array is:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 65
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
It will be really a gift if anyone give a function to solve this issue.
$filteredArray = [];
foreach ($array as $productData) {
if (isset($filteredArray[$productData['Import']['product_id']])) {
$filteredArray[$productData['Import']['product_id']]['Import']['amount'] += $productData['Import']['amount'];
}
else {
$filteredArray[$productData['Import']['product_id']] = $productData;
}
}
print_r($filteredArray);
Ah.. forgot to mention - $array is Your base array.
/**
* Removes duplicate summing amount from products array
*
* #param array $array
* #return array
*/
function removeDuplicates($array)
{
$idsCount = array();
foreach ($array as $key => $value) {
$idsCount[$value['Import']['product_id']]['count'] += 1;
$idsCount[$value['Import']['product_id']]['sum'] += $value['Import']['amount'];
if ($idsCount[$value['Import']['product_id']]['count'] > 1) {
unset($array[$key]);
$array[$idsCount[$value['Import']['product_id']]['key']]['Import']['amount'] = $idsCount[$value['Import']['product_id']]['sum'];
} else {
$idsCount[$value['Import']['product_id']]['key'] = $key;
}
}
return $array;
}
i know it looks crazy but is formatted on your specific array.

extracting an element of an array [duplicate]

This question already has answers here:
Get index of row with qualifying value from a 2d array
(5 answers)
Closed 7 years ago.
Assume this array:
Array ( [0] => Array ( [id] => 1171 [product_id] => 140 [fileid] => 479717 [purchid] => 847 [cartid] => 833 [uniqueid] => f100c3b3a853202fb6559fbacf025a6aa07f52c7 [downloads] => 99998 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[1] => Array ( [id] => 1172 [product_id] => 140 [fileid] => 313624 [purchid] => 847 [cartid] => 833 [uniqueid] => f00a3c91378ad469f333abeec64753b275f10670 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[2] => Array ( [id] => 1173 [product_id] => 140 [fileid] => 313618 [purchid] => 847 [cartid] => 833 [uniqueid] => ac125595e2dbca6a086261434582f6e7dfc5638e [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[3] => Array ( [id] => 1174 [product_id] => 140 [fileid] => 313526 [purchid] => 847 [cartid] => 833 [uniqueid] => 3e6123e0a4453de71dec91a177f5b34217625680 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 ))
I want to "extract" the [0] array and use it for something else BUT it has to be conditioned.
I said the 0 element because it has the fileid = 479717 the one that i want.
So i`m looking at extracting the array (in my case [0]) that has filed = $myvalue. Where i can set $myvalue to whatever i want.
Does this fits your needs ?
function findById($id, $arrayOfArrays){
foreach ( $arrayOfArrays as $contents )
if ( $contents['id'] == $id ) return $contents;
}
function filter($fileid, array $array) {
foreach ($array as $key => $value) {
if ($value['fileid'] === $fileid) {
return $value;
}
}
}

Categories