2

我有一组数组作为从 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"
4

3 回答 3

0
foreach ($t1 as $dmp) {
    $combined[$dmp["no"]][] = $dmp["to"];
}

结果:

Array
(
    [2] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
            [3] => 3
        )

    [4] => Array
        (
            [0] => 7
        )

    [7] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [5] => Array
        (
            [0] => 7
        )

    [6] => Array
        (
            [0] => 7
        )

    [3] => Array
        (
            [0] => 7
        )

)
于 2013-02-09T16:28:25.463 回答
0

我真的建议您在从 sql 获取结果时直接创建正确的数组结构。这将为您节省额外的 foreach 循环。

为了完整起见,我为您准备了一个示例,您可以如何按照您的设想进行操作:(为了好玩;))

$a = array (
    array ('no' => 2, 'to' => 1), //  |___ will be merged
    array ('no' => 2, 'to' => 1), //  |
    array ('no' => 2, 'to' => 2),
    array ('no' => 3, 'to' => 2)
);

$result = array();
foreach($a as $item) {
    if(!array_key_exists($item['no'], $result)) {
        $result[$item['no']] = array($item['to']);
    } else if(!in_array($item['to'], $result[$item['no']], TRUE)) {
        $result[$item['no']][] = $item['to'];
    }
}

// get all to values with no=2 :
$elements = $result[2];

转换后结果数组的 var_dump 可能有助于理解发生了什么:

//
var_dump($result);

/* Output:

array(2) {
  [2] =>  <---- 'no' value as index
  array(2) {
    [0] =>    -
    int(1)     |__ different 'to' values
    [1] =>     |
    int(2)    -
  }
  [3] => <---- 'no' value as index
  array(1) {
    [0] =>
    int(2)   <------ 'to' value for 'no'==3
  }
}

*/

正如我所说,您应该在从 sql 中获取结果时直接创建此数组结构。不幸的是,没有内容$path我不能说如何实现这一点。

于 2013-02-09T16:26:21.730 回答
0

你可以更换

 $t1[] = $var;

$t1[$v][] = $var;

它将根据 ID 进行分组...关于如何输出结果的示例:

foreach ( $t1 as $no => $group ) {
    echo "Merge $no <br />" ;
    foreach ( $group as $list ) {
        var_dump($list);
    }
}
于 2013-02-09T16:20:27.217 回答