I would like to filter part of a multidimensional array. I have used the array_filter function. When I print the filtered data, it shows correctly, but I can't seem to save the data back to the array.
Here is the multidimensional array (called $posted_product_details) beforehand, containing the internal array ([data]) which I would like filter:
Array
(
[column_1] => Array
(
[name] => Colour
[data] => Array
(
[0] => Blue
[1] => Green
[2] => Red
[3] => Yellow
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_2] => Array
(
[name] => Pack QTY
[data] => Array
(
[0] => 3
[1] => 3
[2] => 3
[3] => 3
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_3] => Array
(
[name] => Product Code
[data] => Array
(
[0] => 65030
[1] => 65029
[2] => 65028
[3] => 65031
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_4] => Array
(
[name] => Barcode
[data] => Array
(
[0] => 5099570650307
[1] => 5099570650291
[2] => 5099570650284
[3] => 5099570650314
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_5] => Array
(
[name] =>
[data] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
)
Here I attempt to loop through the array and filter the data:
foreach ($posted_product_details as $column => $info) {
$name = $info['name'];
$data = $info['data'];
$info['data'] = array_filter($data);
}
However, upon printing the array afterward, the array has not changed.
Pass the value by reference to modify the original array:
foreach ($posted_product_details as $column => & $info) {
$name = $info['name'];
$data = $info['data'];
$info['data'] = array_filter($data);
}
This will correctly filter the data part of your array. However, if you need to filter out deeper elements, you'll have to use a recursive function, such as this one.
Demo!
The foreach construct makes a copy of each piece of the array as you iterate over it. You have to explicitly call the original array to edit it:
$posted_product_details[$column]['data'] = array_filter($data);
Related
I have an array of customers/people with a subarray of projects pertaining to that customer. Projects come in two types "typeJob" or "typePipeline". I need to sum the values of individual columns in the projects subarray (specifically columns 5, 6, 7 - representing revenue for month 1, month 2, and month 3) by Client.
I have tried various possible avenues including the one below, but I seem to always end up either testing if the condition is satisfied for the array as a whole rather than for each row - or if I go the way of for each loops generate errors because either there's an array/single value mismatch or the condition just doesn't do anything.
Here is what I have tried most recently. It works in the sense that it checks if "typeJob" is in the array but then sums all rows (even those without "typeJob") if "typeJob" is found for any of the Client's projects. I want to exclude rows where "typeJob" is not found.
foreach ($arrays as $row) {
$indexJob = array_search('typeJob', array_column($row['projects'],0));
if ($indexJob !== false) {
$job_total1[$row['Client']] = array_sum(array_column($row['projects'],5));
$job_total2[$row['Client']] = array_sum(array_column($row['projects'],6));
$job_total3[$row['Client']] = array_sum(array_column($row['projects'],7));
}
}
And here is the array:
(
[331] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9999
[Client] => BBC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 915
[2] => Zyxeldy
[3] =>
[4] =>
[5] => 15000
[6] =>
[7] =>
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 956
[2] => Awesome project, Step 1
[3] =>
[4] =>
[5] => 1833.3333
[6] => 1833.3333
[7] => 1833.3333
[8] =>
)
[2] => Array
(
[0] => typePipeline
[1] => 957
[2] => Awesome project, Step 2
[3] =>
[4] =>
[5] => 7000
[6] =>
[7] =>
[8] =>
)
)
)
[344] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9998
[Client] => ABC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 487
[2] => CRM integration
[3] =>
[4] =>
[5] =>
[6] => 98750
[7] => 98750
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 839
[2] => Data Warehouse
[3] =>
[4] =>
[5] =>
[6] => 11643.0601
[7] =>
[8] =>
)
)
)
[350] => Array
(
[KeyAccountID] => 1236
[KeyAccountName] => Ringo Starr
[ClientID] => 9997
[Client] => XYY
[projects] => Array
(
[0] => Array
(
[0] => typeJob
[1] => 867
[2] => Data Mining
[3] =>
[4] =>
[5] => 10000
[6] =>
[7] =>
[8] =>
)
)
)
[351] => Array
(
[KeyAccountID] => 1235
[KeyAccountName] => Poul McCartney
[ClientID] => 9996
[Client] => XYZ
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 715
[2] => XYZ, CSM
[3] =>
[4] =>
[5] => 22083.3333
[6] => 22083.3333
[7] => 22083.3333
[8] =>
)
)
)
etc.
You could filter the $row['projects'] array before summing the values:
foreach ($arrays as $row) {
$typeJobs = array_filter($row['projects'], function ($v) { return $v[0] == 'typeJob'; });
$job_total1[$row['Client']] = array_sum(array_column($typeJobs,5));
$job_total2[$row['Client']] = array_sum(array_column($typeJobs,6));
$job_total3[$row['Client']] = array_sum(array_column($typeJobs,7));
}
It may be quicker to just loop through them and keep a running total...
foreach ($arrays as $row) {
$totals = [0,0,0];
foreach ( $row['projects'] as $project ) {
if ( $project[0] == 'typeJob' ) {
$totals[0] += $project[5];
$totals[1] += $project[6];
$totals[2] += $project[7];
}
}
$job_total1[$row['Client']] = $totals[0];
$job_total2[$row['Client']] = $totals[1];
$job_total3[$row['Client']] = $totals[2];
}
it doesn't look as slim as using the array_... methods, but this only processes the array once instead of once for each call.
This is the array as I currently have,please convert it to the below array with single dimensional array:-
Array
(
[0] => Array
(
[is_custom] => yes
)
[1] => Array
(
[custom_amount] => 45
)
[2] => Array
(
[custom_amount_text] => Enter Amount
)
[3] => Array
(
[amount_btn] => Dropdown
)
[4] => Array
(
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
)
[5] => Array
(
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)
[6] => Array
(
[recurring_interval] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
)
[7] => Array
(
[admin_mail_subject] =>
)
[8] => Array
(
[admin_mail_body] =>
)
[9] => Array
(
[user_mail_subject] =>
)
[10] => Array
(
[user_mail_body] =>
)
[11] => Array
(
[is_recurrance] => yes
)
[12] => Array
(
[is_onetime] => yes
)
)
Now I want to convert this into something like this
Array([0]=>
[is_custom] => yes
[custom_amount] => 45
[custom_amount_text] => Enter Amount
[amount_btn] => Dropdown
[multiple_amounts] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[recurring_plans] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
)
Array 0 key will have all the keys can It be possible anyhow.I tried but failed.
Please help me with a possible solution
Use RecursiveIteratorIterator for fast and simple.
$output_array = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($input_array)), 0); //$input_array-Replace your arrray
echo "<pre>";
print_r($output_array);
echo "</pre>";
Result:
Array
(
[0] => yes
[1] => 45
[2] => Enter Amount
[3] => Dropdown
[4] => W3sidGl0bGUiOiJMYWJlbCIsImFtb3VudCI6IjQ1In0seyJ0aXRsZSI6IkxhYmVsIiwiYW1vdW50IjoiNDU1In1d
[5] => W3sibmFtZSI6IkxhYmVsIiwicmVjdXJyaW5nX2Ftb3VudCI6Ijc4In0seyJuYW1lIjoidSIsInJlY3VycmluZ19hbW91bnQiOiI3ODgifV0=
[6] => WyJNb250aGx5IiwiUXVhcnRlcmx5IiwiSGFsZi1ZZWFybHkiXQ==
[7] =>
[8] =>
[9] =>
[10] =>
[11] => yes
[12] => yes
)
Run Yourself:
http://sandbox.onlinephpfunctions.com/code/86a50fd58455d64c4de074b888d5d2ea5ee1dd13
try this one
$newArray=array_map(function($v){
return current($v);
},$oldArray);
I want to change order of following array to 2nd array's values.
Array
(
[2] => Array
(
[title] => Photometric Interpretation
[name] => photometric_interpretation
)
[3] => Array
(
[title] => Make
[name] => make
)
[4] => Array
(
[title] => Model
[name] => model
)
[5] => Array
(
[title] => Strip Offsets
[name] => strip_offsets
)
[6] => Array
(
[title] => Samples Per Pixel
[name] => samples_per_pixel
)
[7] => Array
(
[title] => Rows Per Strip
[name] => rows_per_strip
)
)
I want to change order of above to following array's values.
Array
(
[0] => 3
[1] => 4
[2] => 7
[3] => 6
[4] => 5
[5] => 2
)
What I have tried
$index = array_flip(['3,4,7,6,5,2']);
$assigned_fields = array_merge($fisrt_array, $index);
My desired output is
Array
(
[3] => Array
(
[title] => Make
[name] => make
)
[4] => Array
(
[title] => Model
[name] => model
)
[7] => Array
(
[title] => Rows Per Strip
[name] => rows_per_strip
)
[6] => Array
(
[title] => Samples Per Pixel
[name] => samples_per_pixel
)
[5] => Array
(
[title] => Strip Offsets
[name] => strip_offsets
)
[2] => Array
(
[title] => Photometric Interpretation
[name] => photometric_interpretation
)
)
This should work fine.
$a = ['2' => ['title' => 'Photometric Interpretation',
'name' => 'photometric_interpretation'],
'3' => ['title' => 'Make',
'name' => 'make']];
$b = Array
(
0 => 3,
1 => 2
);
$c = [];
foreach($b as $s) {
$c[$s] = $a[$s];
}
print_r($c);
You need to use array_replace instead of array_merge.
$assigned_fields = array_replace(array_flip($index), $fisrt_array);
i have search SO but most of the results is similar to How to remove duplicate values from a multi-dimensional array in PHP
i've tried array_unique, but does not work.
Array
(
[0] => Array
(
[0] => ball_24.gif
[1] => ball_8.gif
[2] => ball_1.gif
[3] => ball_33.gif
[4] => ball_43.gif
[5] => ball_1.gif
[6] => ball_8.gif
[7] => ball_24.gif
[8] => ball_33.gif
[9] => ball_43.gif
)
)
$array[0] = array_unique($array[0]);
i have the following array and want to get rid/remove the empty array and rearrange it in an order.can anyone help me please.
Array
(
[ufile] => Array
(
[name] => Array
(
[0] => chicken soup.jpg
[1] =>
[2] => hot n sour sup.jpg
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[type] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[tmp_name] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[error] => Array
(
[0] => 1
[1] => 4
[2] => 1
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
)
[size] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
)
)
)
I believe this will cleanup your array: remove empty elements (those that evaluate to empty, eg "" and 0 equally), remove duplicate elements, and sort it.
$cleaned = array_map('array_filter', $array_to_be_cleaned);
$cleaned = array_map('array_unique', $cleaned);
$cleaned = array_map('sort', $cleaned);
To filter out the empty elements of the array, check out array_filter.
To sort the elements, check out sort (or refer to this list of sorting functions to see what meets your needs)
$newarray = array_filter($myarray);
sort($newarray);
This will take the array you pass it (ie. $myarray) and it will strip out any empty values, then it will store the results to $newarray. After that, sort will organize the remaining values from least to greatest.
unset( $var['ufile']['type'] );
unset( $var['ufile']['tmp_name'] );
To sort, use sort(), usort(), or any other you want. Here's a good comparison of the different sort methods.
Try array_filter($array). It will remove all NULL elements and return the cleared array.