亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除嵌套 foreach 中的循環數組值?

如何刪除嵌套 foreach 中的循環數組值?

慕桂英546537 2022-06-16 16:36:24
注意:我正在使用 jkanban.js 插件進行拖放,這就是為什么我需要以 json 格式獲取結果這是我的代碼//Declare global empty array$nodes = [];$docs = [];foreach($getStations as $key => $step){   foreach($docs as $key=>$val) {       $docs[] = array(                    "id" =>"$val[0]",                    "title" => "$val[2]",                    "class" => "color$color"                );   }//second foreach  $nodes[] = array(    "id"=>"step_ID",    "title"=>"Some Title",    "class"=>"Some Class",    "item" =>$docs //Pushing the array $docs);}//end main foreach//Display the resultecho json_encode($nodes);這是結果的截圖括號內的數字是項目總數我想要的是擺脫節點內數組文檔的循環結果
查看完整描述

2 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

如果您嘗試推送第二個循環,請在第一個循環中聲明您的變量。


$arr1 = array();

foreach(){

    $arr2 = array();

    foreach(){


    }

}


查看完整回答
反對 回復 2022-06-16
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

這現在有效

通過在第一個 foreach 循環中聲明變量 $docs 為空數組...感謝 marjameson


 //Declare global empty array

$nodes = [];


foreach($getStations as $key => $step){

  //Declare docs empty array

   $docs = [];


   foreach($docs as $key=>$val) {

       $docs[] = array(

                    "id" =>"$val[0]",

                    "title" => "$val[2]",

                    "class" => "color$color"

                );

   }//second foreach


  $nodes[] = array(

    "id"=>"step_ID",

    "title"=>"Some Title",

    "class"=>"Some Class",

    "item" =>$docs //Pushing the array $docs

  );

}//end main foreach


//Display the result

echo json_encode($nodes);


查看完整回答
反對 回復 2022-06-16
  • 2 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號