从数组中删除元素并从0重新索引

时间:2022-06-02 21:22:17
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

我有一个像上面这样的数组输出,我想从数组中删除一个元素然后再次我想从0重新索引它。我尝试在php中使用array_value但是在执行此操作后,它将删除[REF_DETAIL] 0和TOTAL_COUNT为1 ,请在php中提供解决方案

3 个解决方案

#1


4  

try

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

#2


5  

Use array_splice (php docs), it automatically reindexes.

使用array_splice(php docs),它会自动重新索引。

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

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

#3


1  

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
)

#1


4  

try

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

#2


5  

Use array_splice (php docs), it automatically reindexes.

使用array_splice(php docs),它会自动重新索引。

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

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

#3


1  

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
)