0

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] => 
    )
  )
3
  • Hello. Please edit your question and outline what the code is meant to do Commented Apr 5, 2020 at 15:19
  • Thank You! edited it, hope it explains better! Commented Apr 5, 2020 at 15:38
  • 1
    Thank you again for the edit, I edited it again to provide a better example and have beautified the array as you did. Commented Apr 6, 2020 at 10:41

1 Answer 1

2

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! :)

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much and very kind of you for a quick reply, would you be kind to please recheck the same, I have added print_r dump pf my array in my original question.
@user13229327 seems I misunderstood your original question. See my edit. Can you clarify what the problem is?
Thank you! your understanding is correct, I have put an exact dump of the array with _label keys, for me, the same code is giving an empty array, and it doesn't make sense to me don't know what is going wrong.
@user13229327 What version of PHP are you using?
PHP version is 7.1.33
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.