1

I have this array:

`$arr = array(
    'foo' => 'foo',
    'bar' => array(
        'baz' => 'baz',
        'candy' => 'candy',
        'vegetable' => array(
            'carrot' => 'carrot',
        ),
    ),
);

and I want to change it to this:

$arr = array(
'0' => 'foo',
'1' => array(
    '0' => 'baz',
    '1' => 'candy',
    '2' => array(
        '0' => 'carrot',
    )
),
);

I have tried array_values function but it changes only the first level, like this:

$arr = array(
'0' => 'foo',
'1' => array(
    'baz' => 'baz',
    'candy' => 'candy',
    'vegetable' => array(
        'carrot' => 'carrot',
    )
),
);
markpsmith
  • 4,860
  • 2
  • 33
  • 62
  • https://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element – Rakesh P Dec 19 '18 at 12:13
  • 1
    Possible duplicate of [In PHP, how do you change the key of an array element?](https://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element) – Masivuye Cokile Dec 19 '18 at 12:14
  • baz,candy,vegetable can all be nested arrays or the structure is like we see it with only the vegetable to have nested array? – pr1nc3 Dec 19 '18 at 12:16
  • Possible duplicate of [PHP rename array keys in multidimensional array](https://stackoverflow.com/questions/2212948/php-rename-array-keys-in-multidimensional-array) – Isaac Dec 19 '18 at 12:21
  • pr1nc3 it's dynamique so all can be nested array – hanane azee Dec 19 '18 at 12:39

3 Answers3

1

Dipti code is great. I embellished it a bit:

function array_values_recursive(array $arr ) : array {
    $result = array();
    foreach ($arr as $value) {
        $result[] = is_array($value) ? array_values_recursive($value) : $value;
    }
    return $result;
}
DoubleMiP
  • 71
  • 4
0

Please use below function. You have to iterate loop recursive/nested loop

function array_values_recursive($arr)
{
    $arr2=[];
    foreach ($arr as $key => $value)
    {
        if(is_array($value))
        {            
            $arr2[] = array_values_recursive($value);
        }else{
            $arr2[] =  $value;
        }
    }

    return $arr2;
}
print_r(array_values_recursive($arr))

This should work. Thank you

Dipti
  • 565
  • 3
  • 12
0

this is my real code :

$arr = array(); 
while($d = mysqli_fetch_assoc($result)) {
 $sub_data["id"] = $d["id"];
 $sub_data["date"] = $d["date"]; 
 $sub_data["n_trans"] = $d["n_trans"]; 
 $sub_data["doc"] = $d["doc"];     

    if(!isset($arr[$d['date']])) {
        $arr[$d['date']] = array();
    }

    if(!isset($arr[$d['date']][$d['n_trans']])) {
        $arr[$d['date']][$d['n_trans']] = array();
    }

    array_push($arr[$d['date']][$d['n_trans']], $d['doc']);

}
       echo '<pre>';
       print_r(array_values($arr));  
       echo '</pre>';  
?>

here is the result of the query, I try to change the 2nd level ( [32weds] => Array ) remove the text and put an integer like ( [0] => Array ):

 Array(
    [0] => Array(
            [text] => 2018-11-01
            [nodes] => Array(
                    [32weds] => Array(
                            [text] => 32weds
                            [nodes] => Array
                                (
                                    [0] => 32.png
                                    [1] => 32 (1).png
                                )

                        )

                    [qwerty] => Array
                        (
                            [text] => qwerty
                            [nodes] => Array
                                (
                                    [0] => 5384a97ee9d6b (2).pd
                                )

                        )

                )

        )
    )

without changing the format of the array.