Question

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.

Was it helpful?

Solution

foreach ($slides as $key => $slide) {
    if (in_array('images', $slide)) unset($slides[$key]);
}

OTHER TIPS

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); });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top