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',
    )
),
);
5

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

i have a loop while as follow:
@hananeazee:Your sample array as multidimension array with key and value then why while loop? Refer php.net/manual/en/function.array-replace-recursive.php
this array is a result of my loop while
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.

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.