问题描述:

I have an array $result_array, i am trying to push multi dimentional values into this array

without specifying the index as like below

$result_array[]["name"] = "Balan"

$result_array[]["active"] = true

But the resultant array looks like

Array

(

[0] => Array

(

"name" => Balan

)

[1] => Array

(

"active" => true

)

)

But the result i would like to have is like below

Array

(

[0] => Array

(

"name" => Balan

"active" => true

)

)

Please note, i am aware i can initialize a $key = 0 and increment it on each loop to

achieve this. But i would like to know how i can do this without using a separate variable and increment it.

Thanks.

网友答案:

You could do like this..

<?php

$arr=Array(0 => Array("name" => 'Balan'),1 => Array("active" => "true"));
foreach($arr as $arr1)
{
    foreach($arr1 as $k=>$v)
    {
        $new_arr[0][$k]=$v;
    }
}
print_r($new_arr);

OUTPUT :

Array
(
    [0] => Array
        (
            [name] => Balan
            [active] => true
        )

)
网友答案:

I found a solution

    $result_array[]["name"] = "Balan";

    end($result_array);       
    $key = key($result_array);

    $result_array[$key]["active"] = true;

Should be useful for someone..!!

相关阅读:
Top