5
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

1
  • The sample data is impossible. Duplicate keys may not exist on a single level in an array. Commented Oct 5, 2020 at 1:32

3 Answers 3

5

Use array_splice (php docs), it automatically reindexes.

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

Sign up to request clarification or add additional context in comments.

Comments

4

try

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

1 Comment

very expensive solution if the arrays get bigger.
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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.