Reputation: 1
Can anyone please help me to understand and fix; why the following code is clearing all the items from the array. Code is meant to remove all items recursively in the array that do not have '_label' in their key
function checkiflabel($item,$key){
if(strpos($key,'_label')!==false)
return true;
else
return false;
}
function walk_recursive_remove (array $array, callable $callback) {
foreach ($array as $k => $v) {
if (is_array($v)) {
if (($callback($v, $k))==false)
unset($array[$k]);
else
$array[$k] = walk_recursive_remove($v, $callback);
} else {
if (($callback($v, $k))==false) {
unset($array[$k]);
}
}
}
return $array;
}
I am calling this using
$data = walk_recursive_remove($data1,'checkiflabel');
print_r($data); die;
Its shows empty array and for sure $data1 is an array and has values because if I change
if (($callback($v, $k))==false)
to
if ($callback($v, $k))
it does remove all items that have _label and the rest items are fine. Below is the print_r for $data1 with some data obfuscation
Array ( [1] => Array (
[gridReviewerDecision] => ACCEPTED
[gridReviewerDecision_label] => Accepted
[gridReviewerComment] => ok
[gridReviewerComment_label] => ok
[gridReviewerAcceptedAmount] => 100
[gridReviewerAcceptedAmount_label] => 100
[gridUploadFilesByReviewer] => Array (
[0] => Array (
[appDocUid] => 5810427715e892c81ab34c8030653093
[name] => drawing.pdf
[version] => 1
)
)
[gridUploadFilesByReviewer_label] => Array (
[0] => drawing.pdf
)
[gridContest] => 1
[gridContest_label] => true
[gridTypeOfMOtive] => AAAAA
[gridTypeOfMOtive_label] => BBBBB
[gridComment] => test 1
[gridComment_label] => test 1
[gridUploadFiles] => Array (
[0] => Array (
[appDocUid] => 3735522335e892c08eefa43074490443
[name] => drawing.jpg
[version] => 1
)
[1] => Array (
[appDocUid] => 6071415935e892c10331426048606075
[name] => drawing.png
[version] => 1
)
)
[gridUploadFiles_label] => Array (
[0] => drawing.jpg
[1] => drawing.png
)
[gridContestationAmount] => 100
[gridContestationAmount_label] => 100
[DA_PLATFORME] => XXXXXX
[DA_PLATFORME_label] => XXXXXX
[EAN] => 3257980158208
[EAN_label] => 3257980158208
[DESIGNATIONARTICLE] => ZZZZZZ
[DESIGNATIONARTICLE_label] => ZZZZZZ
[VA] => 00
[VA_label] => 00
[DPROMORPERM] => P
[DPROMORPERM_label] => P
[CODERUPTURECOMMANDE] => 06
[CODERUPTURECOMMANDE_label] => 06
[DATELIVR] => 04/12/09
[DATELIVR_label] => 04/12/09
[NOCDE] => 12336422
[NOCDE_label] => 12336422
[RELIQUAT] =>
)
)
Upvotes: 0
Views: 76
Reputation: 1070
EDIT My misunderstanding - you are working with associative arrays. This was not initially clear to me.
When I test your posted code against the following array:
$a = [
'test' => 'a',
'test2' => 'b',
'test3' => 'c',
'test_label' => 'd',
'test_label_2' => [
'test_label_3' => 'e',
'test4' => [
'test_label_4' => 'f'
]
]
];
It seems to do what you expect.
OUTPUT:
Array (
[test_label] => d
[test_label_2] => Array (
[test_label_3] => e
)
)
Original Answer
The issue is with the order of parameters.
if (($callback($v, $k))==false) {
unset($array[$k]);
}
In this case, $v
is the string, and $k
is the array index. So, your checkiflabel
method is comparing the numeric array indexes to a string.
The solution is simply to reverse the order that you call the method with.
if (($callback($k, $v))==false) {
unset($array[$k]);
}
As a small tip, in the future when you run into similar issues: You would've quickly noticed this by simply adding a var_dump
to the checkiflabel
method to ensure that the parameters are as you expected them to be! :)
Upvotes: 2