我現在二維數組中有1千多條數據,每條數據中字段都有冗餘,我現在每條數據中只需要我紅框框出來的3條目標字段就好啦,其他的都不要,
我現在知道要用雙重的foreach循環來做,但一直做不出來,還請大家幫忙解答一下,謝謝
圖如下:
我現在二維數組中有1千多條數據,每條數據中字段都有冗餘,我現在每條數據中只需要我紅框框出來的3條目標字段就好啦,其他的都不要,
我現在知道要用雙重的foreach循環來做,但一直做不出來,還請大家幫忙解答一下,謝謝
圖如下:
題主你是想把二位數組裡你想要的字段取出來嗎, 不用兩次foreach
啊
<code class="php"><?php //---构造测试数据 --- $arry = []; for ($i=0; $i < 1000; $i++) { $arry[] = [ 'platform_good_id' => mt_rand(1, 2000), 'good_name' => 'goodname' . $i, 'xxxxxxx' => 'xxxxx', 'wadadad' => 'adwawdadwad', 'remake' => '' ]; } //---构造测试数据 --- $data = []; foreach ($arry as $key => $value) { $data[] = [ 'platform_good_id' => $value['platform_good_id'], 'good_name' => $value['good_name'], 'remake' => $value['remake'], ]; } var_dump($data); </code>
如下:
<code><!DOCTYPE html> <html> <body> <?php $arr = array( "a"=>array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e"), "b"=>array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e"), "c"=>array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e"), "d"=>array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e"), "e"=>array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e")); $result = array(); foreach ($arr as $k => $v){ $result[$k] = array(); foreach($v as $key => $value){ if($key === "b" || $key === "d" || $key === "e"){ $result[$k][$key] = $value; } } } print_r($arr); print("<br/>"); print_r($result); ?> </body> </html></code>
以上程式碼是提取其中鍵值為 b d e
的元素然後組成一個新的陣列。