首頁 > 後端開發 > php教程 > php数组过滤问题?

php数组过滤问题?

WBOY
發布: 2016-06-06 20:22:27
原創
1355 人瀏覽過

怎么把类似这样的数组过

<code>["Q"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["V"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["H"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
</code>
登入後複製
登入後複製

滤成这样

<code>["Q"] => array(1) {
        [0] => array(11) {
          ["id"] => string(1) "1"
          ["k"] => string(1) "Q"
        }
}
["V"] => array(2) {
        [0] => array(11) {
          ["id"] => string(1) "3"
          ["k"] => string(1) "V"
        }
         [1] => array(11) {
          ["id"] => string(1) "4"
          ["k"] => string(1) "V"
        }
}
["H"] => array(1) {
         [0] => array(11) {
          ["id"] => string(1) "2"
          ["k"] => string(1) "H"
        }
}
</code>
登入後複製
登入後複製

回复内容:

怎么把类似这样的数组过

<code>["Q"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["V"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["H"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
</code>
登入後複製
登入後複製

滤成这样

<code>["Q"] => array(1) {
        [0] => array(11) {
          ["id"] => string(1) "1"
          ["k"] => string(1) "Q"
        }
}
["V"] => array(2) {
        [0] => array(11) {
          ["id"] => string(1) "3"
          ["k"] => string(1) "V"
        }
         [1] => array(11) {
          ["id"] => string(1) "4"
          ["k"] => string(1) "V"
        }
}
["H"] => array(1) {
         [0] => array(11) {
          ["id"] => string(1) "2"
          ["k"] => string(1) "H"
        }
}
</code>
登入後複製
登入後複製

<code><?php $arr = array(
    'Q'=>array(
        array(
            'id'=>1,
            'k'=>'Q'
        ),
        array(
            'id'=>2,
            'k'=>'H'
        ),
        array(
            'id'=>3,
            'k'=>'V'
        ),
        array(
            'id'=>4,
            'k'=>'V'
        ),                    
    ),    
    'V'=>array(
        array(
            'id'=>1,
            'k'=>'Q'
        ),
        array(
            'id'=>2,
            'k'=>'H'
        ),
        array(
            'id'=>3,
            'k'=>'V'
        ),
        array(
            'id'=>4,
            'k'=>'V'
        ),    
    ),
    'H'=>array(
        array(
            'id'=>1,
            'k'=>'Q'
        ),
        array(
            'id'=>2,
            'k'=>'H'
        ),
        array(
            'id'=>3,
            'k'=>'V'
        ),
        array(
            'id'=>4,
            'k'=>'V'
        ),    
    ),    
);

echo '<pre class="brush:php;toolbar:false">';
print_r($arr);

$new_arr = array();
foreach($arr as $key=>$val) {
    foreach($val as $k=>$v) {
        if($key == $v['k']) {
            $new_arr[$key][] = $v;
        }
    }
}
echo '<hr>';
print_r($new_arr);
登入後複製

array_filter($arr,fun);
fun里面用数组的key去和k判断,不等于的unset掉

<code>
$keys = array_keys($arr);
$newArr = array();
$i = 0;
foreach ($arr as $key => $value) {
    $newArr[$key] = array_filter($value, function ($val) {
        GLOBAL $keys;
        GLOBAL $i;
        return $val["k"] == $keys[$i];
    }); 
    $i++;
}
</code>
登入後複製
相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板