Php 动态数组键入while循环
我正在努力让它工作: 我有一个数组,每个循环都“更深”。我需要在最深的“children”键中添加一个新数组Php 动态数组键入while循环,php,multidimensional-array,Php,Multidimensional Array,我正在努力让它工作: 我有一个数组,每个循环都“更深”。我需要在最深的“children”键中添加一个新数组 while($row = mysql_fetch_assoc($res)) { array_push($json["children"], array( "id" => "$x", "name" => "Sta
while($row = mysql_fetch_assoc($res)) {
array_push($json["children"],
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
}
因此,在一个循环中,它将是:
array_push($json["children"] ...
array_push($json["children"][0]["children"] ...
array_push($json["children"][0]["children"][0]["children"] ...
。。。等等你知道如何让按键选择器像这样动态吗
$selector = "[children][0][children][0][children]";
array_push($json$selector);
Hmmm-最好通过引用指定:
$children =& $json["children"];
while($row = mysql_fetch_assoc($res)) {
array_push($children,
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
$children =& $children[0]['children'];
}
Hmmm-最好通过引用指定:
$children =& $json["children"];
while($row = mysql_fetch_assoc($res)) {
array_push($children,
array(
"id" => "$x",
"name" => "Start",
"children" => array()
)
);
$children =& $children[0]['children'];
}
如果要通过字符串路径读取数组,请将字符串拆分为索引,然后可以执行类似操作来获取值
function f($arr, $indices) {
foreach ($indices as $key) {
if (!isset($arr[$key])) {
return null;
}
$arr = $arr[$key];
}
return $arr;
}
如果要通过字符串路径读取数组,请将字符串拆分为索引,然后可以执行类似操作来获取值
function f($arr, $indices) {
foreach ($indices as $key) {
if (!isset($arr[$key])) {
return null;
}
$arr = $arr[$key];
}
return $arr;
}