我需要嵌套3个arrays
(因此第一个array
包含一个array
,其中也包含一个array
)。我在2个arrays
上取得了成功,但我却没有3个可以工作。
我让我的代码使用2 arrays
(可以在jusr上正常工作),但是我无法嵌套3 arrays
。
我当前的代码:
if (!empty($sections)) {
foreach ($sections as $d) {
$row_array = array();
$row_array["id"] = intval($d["id"]);
$row_array["sname"] = $d["sname"];
$row_array["sicon"] = $d["sicon"];
$row_array["perc"] = intval($d["perc"]);
$row_array["pvalue"] = intval($d["pvalue"]);
$row_array["vfixed"] = intval($d["vfixed"]);
$row_array["sectionValues"] = array();
array_push($row_array["sectionValues"],
intval($d["perc"]),
intval($d["id"]),
0
);
$row_array["sectionIndicators"] = array();
//loop indicators
if (!empty($indicators)) {
foreach ($indicators as $v) {
if ($d["id"] == $v["idsec"]) {
/*$row_array["sectionIndicators"][] = array(
"iid" => intval($v["iid"]),
"iname" => $v["iname"],
"perc" => intval($v["perc"]),
"pvalue" => intval($v["pvalue"]),
"vfixed" => intval($v["vfixed"])
);*/
$row_array["sectionIndicators"]["iid"] = intval($v["iid"]);
$row_array["sectionIndicators"]["iname"] = $v["iname"];
$row_array["sectionIndicators"]["perc"] = intval($v["perc"]);
$row_array["sectionIndicators"]["pvalue"] = intval($v["pvalue"]);
$row_array["sectionIndicators"]["vfixed"] = intval($v["vfixed"]);
$row_array["sectionIndicators"]["finalArray"] = array();
}
}
}
array_push($data, $row_array);
}
}
我注释掉的部分是我的工作部分(带有2个数组)。新部分可以工作,但仅包含$indicators
的最后一行,因此似乎循环无法正常工作。
$row_array["sectionIndicators"]["finalArray"] = array();
将包含最后嵌套的array
。
非常感谢您的帮助。
解决方案
我发现我必须给一个索引。我的解决方案:
if (!empty($sections)) {
foreach ($sections as $d) {
$row_array = array();
$row_array["id"] = intval($d["id"]);
$row_array["sname"] = $d["sname"];
$row_array["sicon"] = $d["sicon"];
$row_array["perc"] = intval($d["perc"]);
$row_array["pvalue"] = intval($d["pvalue"]);
$row_array["vfixed"] = intval($d["vfixed"]);
$row_array["sectionValues"] = array();
array_push($row_array["sectionValues"],
intval($d["perc"]),
intval($d["id"]),
0
);
$row_array["sectionIndicators"] = array();
//loop indicators
if (!empty($indicators)) {
$i=-1;
foreach ($indicators as $v) {
if ($d["id"] == $v["idsec"]) {
$i++;
$row_array["sectionIndicators"][$i]["iid"] = intval($v["iid"]);
$row_array["sectionIndicators"][$i]["iname"] = $v["iname"];
$row_array["sectionIndicators"][$i]["perc"] = intval($v["perc"]);
$row_array["sectionIndicators"][$i]["pvalue"] = intval($v["pvalue"]);
$row_array["sectionIndicators"][$i]["vfixed"] = intval($v["vfixed"]);
$row_array["sectionIndicators"][$i]["finalArray"] = array();
array_push($row_array["sectionIndicators"][$i]["finalArray"],
intval($v["perc"]),
intval($d["id"]),
intval($v["iid"])
);
}
}
}
array_push($data, $row_array);
}
}
答案 0 :(得分:1)
<?php
// Assumption that your array is like this :
$sections = [0 => ['id'=>1, 'sname'=>'Sname', 'sicon'=>'SIcon', 'perc'=>'Perc', 'pvalue'=>'Pvalue', 'vfixed'=>'Vfixed'], 1 => ['id'=>2, 'sname'=>'Sname2', 'sicon'=>'SIcon2', 'perc'=>'Perc2', 'pvalue'=>'Pvalue2', 'vfixed'=>'Vfixed2']];
$indicators = [ 0 => ['idsec'=>1, 'iid'=>1, 'iname'=>'viname', 'perc'=>'vperc', 'pvalue'=>'vpvalue', 'vfixed'=>'vVfixed'], 1 => ['idsec'=>2, 'iid'=>2, 'iname'=>'viname2', 'perc'=>'vperc2', 'pvalue'=>'vpvalue2', 'vfixed'=>'vVfixed2']];
// Initialisation your result array
$data = [];
if (!empty($sections)) {
foreach ($sections as $d) {
$row_array = array();
$row_array["id"] = intval($d["id"]);
$row_array["sname"] = $d["sname"];
$row_array["sicon"] = $d["sicon"];
$row_array["perc"] = intval($d["perc"]);
$row_array["pvalue"] = intval($d["pvalue"]);
$row_array["vfixed"] = intval($d["vfixed"]);
$row_array["sectionValues"] = array();
array_push($row_array["sectionValues"],
intval($d["perc"]),
intval($d["id"]),
0
);
//loop indicators
if (!empty($indicators)) {
foreach ($indicators as $v) {
if ($d['id'] == $v['idsec']) {
$row_array["sectionIndicators"] = array();
$row_array["sectionIndicators"][] = array(
"iid" => intval($v["iid"]),
"iname" => $v["iname"],
"perc" => intval($v["perc"]),
"pvalue" => intval($v["pvalue"]),
"vfixed" => intval($v["vfixed"])
);
$row_array["sectionIndicators"]["iid"] = intval($v["iid"]);
$row_array["sectionIndicators"]["iname"] = $v["iname"];
$row_array["sectionIndicators"]["perc"] = intval($v["perc"]);
$row_array["sectionIndicators"]["pvalue"] = intval($v["pvalue"]);
$row_array["sectionIndicators"]["vfixed"] = intval($v["vfixed"]);
$row_array["sectionIndicators"]["finalArray"] = array();
}
}
}
array_push($data, $row_array);
}
}
echo '<pre>';
print_r( $data );
结果:
Array
(
[0] => Array
(
[id] => 1
[sname] => Sname
[sicon] => SIcon
[perc] => 0
[pvalue] => 0
[vfixed] => 0
[sectionValues] => Array
(
[0] => 0
[1] => 1
[2] => 0
)
[sectionIndicators] => Array
(
[0] => Array
(
[iid] => 1
[iname] => viname
[perc] => 0
[pvalue] => 0
[vfixed] => 0
)
[iid] => 1
[iname] => viname
[perc] => 0
[pvalue] => 0
[vfixed] => 0
[finalArray] => Array
(
)
)
)
[1] => Array
(
[id] => 2
[sname] => Sname2
[sicon] => SIcon2
[perc] => 0
[pvalue] => 0
[vfixed] => 0
[sectionValues] => Array
(
[0] => 0
[1] => 2
[2] => 0
)
[sectionIndicators] => Array
(
[0] => Array
(
[iid] => 2
[iname] => viname2
[perc] => 0
[pvalue] => 0
[vfixed] => 0
)
[iid] => 2
[iname] => viname2
[perc] => 0
[pvalue] => 0
[vfixed] => 0
[finalArray] => Array
(
)
)
)
)
答案 1 :(得分:0)
我发现我必须给一个索引。我的解决方案:
"$srcdir"