PHP命名一个未命名的多维关联数组

时间:2018-02-26 23:05:00

标签: php multidimensional-array

如何将['访问'] [0]更改为名称:' location_1',['访问'] [1]:&# 39; location_2'等?

(
    [visits] => Array
        (
            [0] => Array
                (
                    [ID] => 24682
                    [Address] => 1234 Some Street, Houston TX 77777
                )
            [1] => Array
                (
                    [ID] => 24685
                    [Address] => 2345 Any Street, Houston TX 77777
                )
        )

)

1 个答案:

答案 0 :(得分:2)

循环并创建一个新数组:

foreach ($array["visits"] as $key => $value) { 
     $newArray["location_".($key+1)] = $value;
}

示例:

http://sandbox.onlinephpfunctions.com/code/b2bab7e7431f553faee32e028a0823b001214607