I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
I want to remove duplicate entry of [Import][product_id]. So my expected result is :
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
Would you write a function to filter this type of array and produce expected result. I have been googling for 2 days but no luck.
This is a handy one liner that should do the trick:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
If the underlying arrays are not identical, that won't work, in which case I think you could do:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
Tested: http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d
Here is a quick stable sort and reduce which runs in linearithmic time. First-encountered product Id's are kept, and entries with duplicate product Id's are ignored.
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== #$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
Demo: http://ideone.com/BP0eUJ
Update: Here is an even better linear-time algorithm that does the same as above using a fast "hash table" lookup. Again, the first-encountered product Id is kept and subsequent ones of the same Id are ignored.
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
Demo: http://ideone.com/5RF0og
Related
I am using an array that I get from a database and I would like to create an associative array from that one.
Here is the code I am using:
print_r($data_sites);
// Format for easy use
$data_sites_formatted = array();
foreach ($data_sites as $key => $row) {
if (empty($data_formatted[$row->project_loe_id])) {
$data_sites_formatted[$row->project_loe_id] = array();
}
$data_sites_formatted[$row->project_loe_id][$row->name] = array();
$data_sites_formatted[$row->project_loe_id][$row->name]['id'] = $row->id;
$data_sites_formatted[$row->project_loe_id][$row->name]['quantity'] = $row->quantity;
$data_sites_formatted[$row->project_loe_id][$row->name]['loe_per_quantity'] = $row->loe_per_quantity;
}
print_r($data_sites_formatted);
Here is the information I receive from the database:
Illuminate\Support\CollectionObject ( [items:protected] => Array (
[0] => stdClass Object ( [id] => 1 [project_loe_id] => 1 [name] => site [quantity] => 20 [loe_per_quantity] => 1 )
[1] => stdClass Object ( [id] => 2 [project_loe_id] => 1 [name] => switches [quantity] => 5 [loe_per_quantity] => 3 )
[2] => stdClass Object ( [id] => 3 [project_loe_id] => 2 [name] => site [quantity] => 20 [loe_per_quantity] => 1 )
[3] => stdClass Object ( [id] => 4 [project_loe_id] => 2 [name] => ap [quantity] => 5 [loe_per_quantity] => 3 )
[4] => stdClass Object ( [id] => 5 [project_loe_id] => 2 [name] => wireless [quantity] => 5 [loe_per_quantity] => 3 ) ) )
And when I do the transformation, here is the result:
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
I lost 3 lines and I really have no idea why. I can see when I look into details step by step that it deletes some lines from 1 iteration to the next:
Iteration:0
Array (
[1] => Array ( [site] => Array ( [id] => 1 [quantity] => 20 [loe_per_quantity] => 1 ) ) )
Iteration: 1
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
Iteration: 2
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [site] => Array ( [id] => 3 [quantity] => 20 [loe_per_quantity] => 1 ) ) )
Iteration: 3
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [ap] => Array ( [id] => 4 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
Iteration: 4
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
You can see from iteration 0 to iteration 1, it deleted the line from iteration 0 and in iteration 1 I should have 2 lines.
You have a typo here:
if (empty($data_formatted[$row->project_loe_id])) {
$data_sites_formatted[$row->project_loe_id] = array();
}
$data_formatted is never defined (you meant to write $data_sites_formatted) so empty($data_formatted[$row->project_loe_id]) is always true, and always runs the next line, replacing anything previously added to $data_sites_formatted[$row->project_loe_id] with an empty array. So all you end up with is the last item to be added in each group.
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
)
)
I have a multidimensional array, I want to take the key 'data' from each array item and form another array, like first array 'data' element has 3 items, second has 1 item, 3rd and 4th are empty, and 5th has one item, I want to make a separate array like $temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]
input array is,
Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
)
the output to be like this
Array
(
[0] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
[1] => Array
(
[0] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
[2] => Array
(
[0] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
)
You need to do it like below:-
$data_array= array_column($array,'data');
$count = 0;
foreach($data_array as $data){
$real_count = count($data);
if($real_count > $count){
$count = $real_count;
}
}
echo $count;
$final_array = [];
foreach($data_array as $data_arr){
for($i=0;$i< $count; $i++){
$final_array[$i][] = (count($data_arr[$i])>0)? $data_arr[$i]: array();
}
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/925383
Reference:- PHP: array_column - Manual
You can use array_column() function
Example :-
<?php
$array = [
[
"meal_type" => "bf",
"data" => [
"name" => "test",
"email" => "test#ymail.com"
]
],
[
"meal_type" => "bf",
"data" => []
]
];
echo "<pre>";
print_r(array_column($array, "data"));
?>
You can do this using foreach and array_push.
$data_item_array = [];
foreach ($array as $item) {
$data = $item['data'];
foreach ($data as $t) {
array_push($data_item_array, $t);
}
}
print_r($data_item_array);
Output will be :
Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[3] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
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.