vishalg
vishalg

Reputation: 437

remove element from array and reindex from 0

Array (
    [REF_DETAILS] = Array
        (
            [0] = Array
                (
                    [ID] => 1231312
                    [USER] => USER
                )

            [1] = Array
                (
                    [TID] => 2754042         
                    [USER] = USER
                )
            [1] = Array
                (
                    [TID] => 534535         
                    [USER] = USER
                )

        )

    [TOTAL_COUNT] = 31
)

I have a array output like this above and I want to remove one element from the array then again i want to reindex it from 0. I tried with array_value in php but after doing this it is removing [REF_DETAIL] with 0 and TOTAL_COUNT as 1 , please provide the solution in php

Upvotes: 5

Views: 4097

Answers (3)

Barmar
Barmar

Reputation: 782693

Use array_splice (php docs), it automatically reindexes.

array_splice($array['REF_DETAILS'], 1, 1)

Upvotes: 5

Toto
Toto

Reputation: 91518

Use array_shift

$arr = Array (
    'REF_DETAILS' => Array(
        0 => Array(
            'ID' => '> 1231312',
            'USER' => '> USER',
        ),
        1 => Array(
            'TID' => '> 2754042         ',
            'USER' => 'USER',
        ),
        2 => Array(
            'TID' => '> 534535         ',
            'USER' => 'USER',
        ),
    ),
    'TOTAL_COUNT' => 31,
);

array_shift($arr['REF_DETAILS']);
print_r($arr);

output:

Array
(
    [REF_DETAILS] => Array
        (
            [0] => Array
                (
                    [TID] => > 2754042
                    [USER] => USER
                )

            [1] => Array
                (
                    [TID] => > 534535
                    [USER] => USER
                )

        )

    [TOTAL_COUNT] => 31
)

Upvotes: 1

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

try

unset($array['REF_DETAILS'][1]);
$array['REF_DETAILS'] = array_values($array['REF_DETAILS']);

Upvotes: 4

Related Questions