• Resolved George

    (@chainkiller)


    Lets say i have a loop for books

    Array
    (
        [0] => Array
            (
                [title] => book 1
                [author] => author 1
    	)
    
        [1] => Array
            (
                [title] => book 2
                [author] => author 2
            )
    
        [2] => Array
            (
                [title] => book 3
                [author] => author 3
    
            )
        [3] => Array
            (
                [title] => book 4
                [author] => author 4
    
            )
    )

    Now if i delete books[2] the array becomes

    Array
    (
        [0] => Array
            (
                [title] => book 1
                [author] => author 1
    		)
    
        [1] => Array
            (
                [title] => book 2
                [author] => author 2
            )
    
        [3] => Array
            (
                [title] => book 4
                [author] => author 4
    
            )
    )

    books[3] stays books[3], i assumed it would become books[2]

    Is this how it’s supposed to be?

    Someone reported this before.

    https://wordpress.org/plugins/custom-field-suite/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    That’s how PHP works. It will not modify any array keys for you.

    Thread Starter George

    (@chainkiller)

    $arr1 = array(
                'title' => 'book 1',
                'author' => 'author 1'
    	);
    	$arr2 = array(
                'title' => 'book 2',
                'author' => 'author 2'
    	);
    	$arr3 = array(
                'title' => 'book 3',
                'author' => 'author 3'
    	);
    	$arr4 = array(
                'title' => 'book 4',
                'author' => 'author 4'
    	);
    
    	$array = array($arr1, $arr2, $arr3, $arr4);

    In the code above if i remove $arr3 (not unset, delete completely from code), the index changes.

    $arr1 = array(
                'title' => 'book 1',
                'author' => 'author 1'
    	);
    	$arr2 = array(
                'title' => 'book 2',
                'author' => 'author 2'
    	);
    	$arr4 = array(
                'title' => 'book 4',
                'author' => 'author 4'
    	);
    
    	$array = array($arr1, $arr2, $arr4);

    So shouldn’t deleting a field have the same result?

    Plugin Author Matt Gibbs

    (@mgibbs189)

    That’s because you’re creating an entirely new array.

    See this: http://stackoverflow.com/questions/5943149/rebase-array-keys-after-unsetting-elements

    Thread Starter George

    (@chainkiller)

    Thank you, array_values() does the job.
    Sorry for late reply.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Deleting item from lop’ is closed to new replies.