1

I have this array(output of var_dump()):

array (size=32)
  2 => &
    array (size=3)
      'data' =>
        array (size=3)
          'parent_id' => string '1' (length=1)
          'id' => string '2' (length=1)
          'options' => string '2' (length=1)
      'attr' =>
        array (size=2)
          'rel' => string 'container' (length=9)
          'id' => string '2' (length=1)
      'children' =>
        array (size=3)
          3 => &
            array (size=3)
              'data' =>
                array (size=3)
                  'parent_id' => string '2' (length=1)
                  'id' => string '3' (length=1)
                  'options' => string '3' (length=1)
              'attr' =>
                array (size=2)
                  'rel' => string 'container' (length=9)
                  'id' => string '3' (length=1)
              'children' =>
                array (size=2)
                  8 => &
                    array (size=3)
                      'data' =>
                        array (size=3)
                          'parent_id' => string '3' (length=1)
                          'id' => string '8' (length=1)
                          'options' => string '1' (length=1)
                      'attr' =>
                        array (size=2)
                          'rel' => string 'container' (length=9)
                          'id' => string '8' (length=1)
                      'children' =>
                        array (size=2)
                          11 => &
                            array (size=3)
                              'data' =>
                                array (size=3)
                                  'parent_id' => string '8' (length=1)
                                  'id' => string '11' (length=2)
                                  'options' => string '3' (length=1)
                              'attr' =>
                                array (size=2)
                                  'rel' => string 'container' (length=9)
                                  'id' => string '11' (length=2)

As you can see some of key are passed by reference, I want change this array to regular array, like this:

array (size=32)
  0 => 
    array (size=3)
      'data' =>
        array (size=3)
          'parent_id' => string '1' (length=1)
          'id' => string '2' (length=1)
          'options' => string '2' (length=1)
      'attr' =>
        array (size=2)
          'rel' => string 'container' (length=9)
          'id' => string '2' (length=1)
      'children' =>
        array (size=3)
          0 => 
            array (size=3)
              'data' =>
                array (size=3)
                  'parent_id' => string '2' (length=1)
                  'id' => string '3' (length=1)
                  'options' => string '3' (length=1)
              'attr' =>
                array (size=2)
                  'rel' => string 'container' (length=9)
                  'id' => string '3' (length=1)
              'children' =>
                array (size=2)
                  0 => 
                    array (size=3)
                      'data' =>
                        array (size=3)
                          'parent_id' => string '3' (length=1)
                          'id' => string '8' (length=1)
                          'options' => string '1' (length=1)
                      'attr' =>
                        array (size=2)
                          'rel' => string 'container' (length=9)
                          'id' => string '8' (length=1)
                      'children' =>
                        array (size=2)
                          0 => 
                            array (size=3)
                              'data' =>
                                array (size=3)
                                  'parent_id' => string '8' (length=1)
                                  'id' => string '11' (length=2)
                                  'options' => string '3' (length=1)
                              'attr' =>
                                array (size=2)
                                  'rel' => string 'container' (length=9)
                                  'id' => string '11' (length=2)

1 Answer 1

1

Just json_encode and json_decode

$array = json_decode(json_encode($array), true);

Also you could use this function.

function deReferencing($value)
    {
    if (is_array($value))
        {
        $return = array();
        foreach ($value as $key => $item)
            {
            unset($value[$key]);
            if (is_numeric($key)) {
               $return[] = deReferencing($item);
               } else {
               $return[$key] = deReferencing($item);
               }  
            }
        }
    else
        $return = $value;

    return $return;
    }

$array = deReferencing($array);
Sign up to request clarification or add additional context in comments.

5 Comments

@ArashMousavi, what do you mean?
I tried your approach, but it didn't worked, and output array was as same as first array, and it hasn't changed...
@ArashMousavi, sorry. It's not working. I'll change my answer.
Thank you, it works fine :) but another issue is remain, how to make array keys squential from 0?
I tried updated function, but it change none numeric keys like 'id', 'parent_id',...

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.