0

My array looks like this:

Array ( 
    [0] => Array ( 
        [value] => Array (
            [source] => vimeo
            [url] => https://vimeo.com/000000
        ) 
        [type] => videos 
    )
    [2] => Array ( 
        [value] => 62 
        [type] => images 
    ) 
)

I want to unset the array id with the type => images.

I tried this:

$key = array_search('images',$slides); 
unset($slides[$key]); 

and it only deletes the first item in the array!!!

Update:

After all, I did it this way:

foreach ( $slides as $slide => $value) {
    if ($display_mode == 'images' &&  $value['type'] == 'videos') {
        unset($slides[$slide]);
    } elseif ($display_mode == 'videos' &&  $value['type'] == 'images') {
        unset($slides[$slide]); 
    }  
}

Thank you.

2 Answers 2

2
foreach ($slides as $key => $slide) {
    if (in_array('images', $slide)) unset($slides[$key]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

eBrian -- you'd need to use a reference to $slide for that to work. Otherwise the changes will be abandoned after each iteration. Otherwise, a foreach is perfectly feasible =)
@KevinNielsen, not true. Note that I am unsetting in the array directly and not attempting to unset $slide. Ultimately what this does is unset($slides[2]).
2

array_search returns false if $needle is not found. false casts to 0 when used as an integer. You might want to consider array_filter for your use case:

$array = array(...);

$array = array_filter($array, function($item) { return in_array('images', $item); });

Comments

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.