0

My code to delete key like this :

<?php
    $photoList = array(
        array(
            'id' => 1,
            'name' => 'chelsea.jpg'
        ),
        array(
            'id' => 2,
            'name' => 'mu.jpg'
        ),
        array(
            'id' => 3,
            'name' => 'city.jpg'
        )
    );
    if(count($photoList) > 1) {
        $id = 1;
        foreach($photoList as $key => $value) {
            if($value['id'] == $id)
                unset($photoList[$key]);   
        }
    }
    echo '<pre>';print_r($photoList);echo '</pre>';
?>

If the code executed, the result like this :

Array
(
    [1] => Array
        (
            [id] => 2
            [name] => mu.jpg
        )

    [2] => Array
        (
            [id] => 3
            [name] => city.jpg
        )
)

I want the value re-update. So id start from 1 and the key start from 0 like this :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => mu.jpg
        )

    [1] => Array
        (
            [id] => 2
            [name] => city.jpg
        )
)

How can I do it? Please help me, what is wrong? thanks

moses toh
  • 12,344
  • 71
  • 243
  • 443

4 Answers4

2

You can keep a flag $delete and use that to remember if a delete has been done and then change the values if it's true.

if(count($photoList) > 1) {
    $id = 1;
    $delete = false;
    foreach($photoList as $key => &$value) { // notice the & to make it by reference (editable)
        if($value['id'] == $id && $delete == false){
            $delete = true;
            unset($photoList[$key]);   
        }Else if($delete == true){
            $value["id"] = $id;
            $id++; // update id for next value in array
        }
    }
}
$photoList= array_values($photoList);

https://3v4l.org/N2qjm


Without reference:

if(count($photoList) > 1) {
    $id = 1;
    $delete = false;
    foreach($photoList as $key => $value) {
        if($value['id'] == $id && $delete == false){
            $delete = true;
            unset($photoList[$key]);   
        }Else if($delete == true){
            $photoList[$key]["id"] = $id;
            $id++;
        }
    }
}
$photoList= array_values($photoList);
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • May I remove the mark `&` in the `foreach($photoList as $key => &$value) {`. Seem in my case the mark `&` is problematic. I can not explain to you. Because it will be long. So I need you remove mark `&` only. – moses toh Mar 05 '18 at 16:14
  • If I remove the & the code won't work. The & means we can edit the values in $photolist from $value. You can of course reference to the photlist instead of value, but not sure why this is a problem for you. I will update the code with a non referenced code. – Andreas Mar 05 '18 at 16:23
  • I use laravel framework. If I use referenced code, it's problematic. But thanks. Your update code works – moses toh Mar 05 '18 at 17:04
  • Maybe it's that you need to unset the value after the foreach? I have never encountered the problem myself, but some say you should always unset referenced values after the foreach. `unset($value);` – Andreas Mar 05 '18 at 17:37
1

Try this..

   <?php
            $photoList = array(
                array(
                    'id' => 1,
                    'name' => 'chelsea.jpg'
                ),
                array(
                    'id' => 2,
                    'name' => 'mu.jpg'
                ),
                array(
                    'id' => 3,
                    'name' => 'city.jpg'
                )
            );
            $newphotolist = [];
            $counter_id = 0;
            if(count($photoList) > 1) {
                $id = 1;
                foreach($photoList as $key => $value) {
                    if($value['id'] != $id){
                        $counter_id++;
                        $arr = array('id' => $counter_id, 'name' => $value['name']);
                        $newphotolist[] = $arr;
                    }
                }
            }
            echo '<pre>';print_r($newphotoList);echo '</pre>';
        ?>
Ariel Pepito
  • 619
  • 4
  • 13
0
$photoList = array(
        array(
            'id' => 1,
            'name' => 'chelsea.jpg'
        ),
        array(
            'id' => 2,
            'name' => 'mu.jpg'
        ),
        array(
            'id' => 3,
            'name' => 'city.jpg'
        )
    );
    if(count($photoList) > 1) {
        $id = 1;
        foreach($photoList as $key => $value) {
            if($value['id'] == $id)
                unset($photoList[$key]);   
        }
    }

at the end of your job, reorder your key first.

$photoList = array_values($photolist);

and then, reorder your id.

foreach($photoList as $key=>$val) {
    $photoList[$key]['id'] = $key+1;
}
Meow Kim
  • 445
  • 4
  • 14
0

<?php $photoList = array( array( 'id' => 1, 'name' => 'chelsea.jpg' ), array( 'id' => 2, 'name' => 'mu.jpg' ), array( 'id' => 3, 'name' => 'city.jpg' ) ); if(count($photoList) > 1) { $id = 1; $intStartId = 1; foreach($photoList as $key => &$value) { if($value['id'] == $id) unset($photoList[$key]);
else { $value['id'] = $intStartId; ++ $intStartId; } } } $photoList = array_merge(array(), $photoList); echo '<pre>';print_r($photoList);echo '</pre>';

Hao Dong
  • 53
  • 5