i have multidimensional array..in this array i have many arrays..i want to filter their keys..to do that i want to remove the keys that has not any value in all arrays..so how to do this in the right way
$cell_values = array_map('array_filter', $values);
print_r($cell_values);
this will filter them all but i want to let the key if it have any value in another array..
arrays like thts
Array
(
[2] => Array
(
[A] => 1010
[B] => -6989548.4
[C] =>
[D] =>
)
[3] => Array
(
[A] => 1020
[B] => 20554
[C] => 28.8
[D] =>
)
[4] => Array
(
[A] => 1030
[B] => 15151
[C] =>
[D] =>
)
the expected output like this
Array
(
[2] => Array
(
[A] => 1010
[B] => -6989548.4
[C] =>
)
[3] => Array
(
[A] => 1020
[B] => 20554
[C] => 28.8
)
[4] => Array
(
[A] => 1030
[B] => 15151
[C] => 28.8
)
First figure out which keys have at least 1 value. Then loop through the array and unset any keys that do not have at least 1 value.
$keys_with_values = array();
foreach($arrays as $array) {
foreach($array as $key => $value) {
if($value) {
$keys_with_values[$key] = 1;
}
}
}
foreach($arrays as &$array) {
foreach($array as $key => $value) {
if(!isset($keys_with_values[$key]))
unset($array[$key]);
}
}