数组过滤PHP

时间:2010-06-01 14:32:23

标签: php arrays filter

我有一个数组,其值如:

Array
(
    [0] => Array
        (
            [parent] => Basic
            [parentId] => 1
            [child] => Birthday
            [childId] => 2
        )

    [1] => Array
        (
            [parent] => Basic
            [parentId] => 1
            [child] => Gender
            [childId] => 3
        )

    [2] => Array
        (
            [parent] => Geo
            [parentId] => 10
            [child] => Current City
            [childId] => 11
        )

    [3] => Array
        (
            [parent] => Known me
            [parentId] => 5
            [child] => My personality
            [childId] => 7
        )

    [4] => Array
        (
            [parent] => Known me
            [parentId] => 5
            [child] => Best life moment
            [childId] => 8
        )
)

我想过滤这个数组,以便根据索引进行过滤,最终结果如下:

Array
(
    [0] => Array
        (
            [parent] => Basic
            [parentId] => 1
            [child] => Array
                (
                    [0] => Birthday
                    [1] => Gender
                )

        )

    [1] => Array
        (
            [parent] => Geo
            [parentId] => 10
            [child] => Array
                (
                    [0] => Current City                  
                )

        )

    [2] => Array
        (
            [parent] => Known me
            [parentId] => 5
            [child] => Array
                (
                    [0] => My personality
                    [1] => Best life moment
                )

        )
)

为此,我编码:

$filter = array();
$f = 0;
for ($i=0; $i<count($menuArray); $i++) {
    $c = 0;
    for( $b = 0; $b < count($filter); $b++ ){

        if( $filter[$b]['parent'] == $menuArray[$i]['parent'] ){
            $c++;
        }
    }
    if ($c == 0) {
        $filter[$f]['parent'] = $menuArray[$i]['parent'];
        $filter[$f]['parentId'] = $menuArray[$i]['parentId'];
        $filter[$f]['child'][] = $menuArray[$i]['child'];
        $f++;
    } 
}

但结果是:

Array
(
    [0] => Array
        (
            [parent] => Basic
            [parentId] => 1
            [child] => Array
                (
                    [0] => Birthday
                )

        )

    [1] => Array
        (
            [parent] => Geo
            [parentId] => 10
            [child] => Array
                (
                    [0] => Current City
                )

        )

    [2] => Array
        (
            [parent] => Known me
            [parentId] => 5
            [child] => Array
                (
                    [0] => My personality
                )

        )
)

有人能指出我失踪的LOC吗?

2 个答案:

答案 0 :(得分:1)

尝试:

$filter = array();
foreach ($menuArray as $menu) {
  if (!array_key_exists($menu['parent_id'], $filter)) {
    $filter[$menu['parent_id']] = array(
      'parent' => $menu['parent'],
      'parent_id' => $menu['parent_id'],
      'child' => array()
    );
  }
  $filter[$menu['parent_id']]['child'][$menu['child_id']] = $menu['child'];
}

这会产生一个像:

这样的数组
Array
(
    [1] => Array
        (
            [parent] => Basic
            [parentId] => 1
            [child] => Array
                (
                    [2] => Birthday
                    [3] => Gender
                )

        )

    [10] => Array
        (
            [parent] => Geo
            [parentId] => 10
            [child] => Array
                (
                    [11] => Current City                  
                )

        )

    [5] => Array
        (
            [parent] => Known me
            [parentId] => 5
            [child] => Array
                (
                    [7] => My personality
                    [8] => Best life moment
                )

        )
)

请注意,数组索引与ID匹配。你不能用for循环来循环它,但你可以foreach ($filter as $parent_id=>$parent)正确。如果您愿意,可以将我的代码的第4行更改为$filter['key_' . $menu['parent_id']]以强制使用字符串,并且for循环将起作用

答案 1 :(得分:0)

下一段代码是完全未经测试的,并且基于它是按parentId排序的想法。

$filter = array();
$option = null;
for( $i = 0; $i < count( $menuArray ); $i++ ) {
    if( count( $filter ) < 1 || $filter[count($filter)-1]['parentId'] != $menuArray['parentId'] ) {
        if( $option != null ) {
            $filter[]   = $option;
        }

        $option = array(
            "parent"    => $menuArray[$i]['parent'],
            "parentId"  => $menuArray[$i]['parentId'],
            "child"     => array()
        );
    }

    $option['child'][]  = $menuArray[$i]['child'];
}

$filter[]   = $option; // one last time, because we left the loop already.
unset( $option ); // we don't need it anymore.

它做什么,它为每个父母创建一个$option。只要我们点击下一个parentId,我们就会将当前$option添加到$filter数组并创建一个新的$option对象。

所有孩子都会继续加入当前的$option