我有一组数组作为从 mysql 表查询的结果,在“while”循环中制作:
while ($row = mysql_fetch_array($result)) {
$k_id = $row['kw'];
$path[$level] = $k_id;
if(array_search('7', $path)) {
$it = new CachingIterator(new ArrayIterator($path));
$t1 = array();
foreach ( $it as $v ) {
$var = array("no" => $v,"to" => 0);
if ($it->hasNext()) {
$var["to"] = $it->getInnerIterator()->current();
}
$t1[] = $var;
}
extract($t1, EXTR_PREFIX_ALL, "t1");
for ($i = 0; $i < sizeof($t1); $i++) {
$dmp = print_r($t1[$i], result);
echo $dmp."<br />";
}
}
}
echo $dmp 的结果是一组数组:
Array ( [no] => 2 [to] => 1 )
Array ( [no] => 1 [to] => 4 )
Array ( [no] => 4 [to] => 7 )
Array ( [no] => 7 [to] => 0 )
Array ( [no] => 2 [to] => 1 )
Array ( [no] => 1 [to] => 5 )
Array ( [no] => 5 [to] => 7 )
Array ( [no] => 7 [to] => 0 )
Array ( [no] => 2 [to] => 1 )
Array ( [no] => 1 [to] => 6 )
Array ( [no] => 6 [to] => 7 )
Array ( [no] => 7 [to] => 0 )
Array ( [no] => 2 [to] => 1 )
Array ( [no] => 1 [to] => 3 )
Array ( [no] => 3 [to] => 7 )
Array ( [no] => 7 [to] => 0 )
我的问题是如何合并具有相同“否”键值的数组
mysql_fetch 之前的 var_dump:
array(0) { } array(1) { [0]=> string(1) "2" } array(2) { [0]=> string(1) "2" [1]=> string(1) "1" } array(3) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> string(1) "4" }
因此,我想获得数组,我可以轻松地从以下位置生成这样的代码:
"nodeTo": "4",
"nodeFrom": "1"
"nodeTo": "5",
"nodeFrom": "1"
"nodeTo": "6",
"nodeFrom": "1"
"nodeTo": "3",
"nodeFrom": "1"
"id": "1"