find and delete next array inside an array

It’s much easier to access and delete the item in a for loop:

<?php
$items = json_decode('[{"link": "1ndexample","mime_type": "image"},{"link": "2ndexample","mime_type": "video"},{"link": "3ndexample","mime_type": "image"},{"link": "4ndexample","mime_type": "image"}]');

for ($i = count($items) - 1; $i > 0; $i--) {
    if ($items[$i-1]->mime_type === 'video') {
        unset($items[$i]);
    }
}

var_dump($items);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top