我有这个...
$routes = array();
loop starts...
$routes[]['location1'] = MyValue1
$routes[]['location2'] = MyValue2
$routes[]['distance'] = MyValue3
...loop ends
我想要的是...
Array
(
[0] => Array
(
[location1] => MyValue1
[location2] => MyValue2
[distance] => MyValue3
)
[1] => Array
(
[location1] => MyValue1
[location2] => MyValue2
[distance] => MyValue3
)
}
但是我得到的是...
Array
(
[0] => Array
(
[location1] => MyValue1
)
[1] => Array
(
[location2] => MyValue2
)
[2] => Array
(
[distance] => MyValue3
)
[3] => Array
(
[location1] => MyValue1
)
[4] => Array
(
[location2] => MyValue2
)
[5] => Array
(
[distance] => MyValue3
)
}
使用的正确语法是什么?还是我需要添加某种计数器来使用诸如此类的东西来计算整个循环的迭代次数$routes[$a]['distance']
?
最简单的方法是构建子数组,然后将其添加到单个分配中:
$routes = array();
loop starts...
$routes[] = array (
'location1' => MyValue1,
'location2' => MyValue2,
'distance' => MyValue3
);
...loop ends
每次进行任何操作时$routes[]
,都需要向中添加一个元素$routes
。因此,您必须执行上述操作或跟踪正在使用的索引。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句