1

I want to delete a whole array within an array selected by a specific value. This is the code I got so far.

Since I'm unsetting the array with all the subarrays and then inserting back the subarrays what could (I think) lead to huge performance problems with larger or more arrays.

So my question is there a way to optimise the code below or just remove the one array and leave the rest untouched?

Thanks :)

<?php

$currentValue = '#6';

$otherValue = [ "ID" => '2', "valueID" => '#6' ];
$otherValue2 = [ "ID" => '3', "valueID" => '#7' ];
$otherValue3 = [ "ID" => '4', "valueID" => '#8' ];
$valueArray = [ $otherValue, $otherValue2, $otherValue3 ];

echo 'current value: '.$currentValue;
echo '<br><br>';
print_r($valueArray);
echo '<br><br>';
foreach( $valueArray as $key => $value ) {
    echo 'Value: ';
    print_r($value); 
    if(($key = array_search($currentValue, $value)) !== false) {
        echo ' - true, '.$currentValue.' is in $value<br>';

        unset($value);
        unset($valueArray);

        if( isset($value) ) {
            print_r($value);
            echo '<br>';
        } else {
            echo '$value was deleted<br><br>';
        }
    } else {
        echo ' - false<br>';
        $valueArray[] = $value;
    }

}
echo '<br>';
print_r($valueArray);

?>
6
  • You might wanna checkout codereview.stackexchange.com Commented Jun 25, 2016 at 16:36
  • You're using array_search() because you don't know where $currentValue might be in searched array? If it's always under valueID key then it's much faster to check this concrete key if ($value['valueID'] === $currentValue) {...} or return $value['valueID'] === $currentValue; in case of trincot's answer Commented Jun 25, 2016 at 16:57
  • @shudder, indeed, but in my code it would in fact be !==. Commented Jun 25, 2016 at 17:04
  • Yep that makes sense to look in the key rather than the whole array, thanks :) Commented Jun 25, 2016 at 17:06
  • @Antari24, just so you know: There is no need to put SOLVED in the title, as StackOverflow already will indicate this. Also, you should not really add the answer in the question. Answers are separate posts, and the site works best like that. It will actually be confusing to see answer-code in your question. I rolled back the question to its original version. Commented Jun 25, 2016 at 17:18

1 Answer 1

1

Your code will return the wrong result in many cases, for instance when the searched for value is not found at all, or in the last sub-array. This is because of the following lines:

unset($valueArray);

and

$valueArray[] = $value;

That first statement destroys any previous result made with the second. Or if the first is never executed, the second just adds to the original array, making it twice its original size.

Instead you could use array_filter:

$valueArray = array_filter($valueArray, function ($value) use ($currentValue) {
    return array_search($currentValue, $value) === false;
});

See it run on eval.in.

As @shudder noted in comments, it is strange that you want to search the value in all the keys. From the example you have given it looks like you are expecting to find it under the valueID key.

In that case you can optimise your code and do:

$valueArray = array_filter($valueArray, function ($value) use ($currentValue) {
    return $value['valueID'] !== $currentValue;
});

See it run on eval.in.

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

3 Comments

Haven't used array_filter, but will give it a try.
OK, also note the comment from shudder, and the paragraph I just added.
array_filter works like a charm! Thanks for pointing that out.

Your Answer

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