久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP 使用 array_push 向多維數組添加元素

PHP add elements to multidimensional array with array_push(PHP 使用 array_push 向多維數組添加元素)
本文介紹了PHP 使用 array_push 向多維數組添加元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個多維數組 $md_array,我想向來自從表中讀取數據的循環的子數組 recipe_type 和美食添加更多元素.

I have a multidimensional array $md_array and I want to add more elements to the sub-arrays recipe_type and cuisine coming from a loop that reads data from a table.

在循環中,我為每一行創建一個新表 $newdata:

In the loop, I create a new table $newdata for each row:

$newdata =  array (
          'wpseo_title' => 'test',
          'wpseo_desc' => 'test',
          'wpseo_metakey' => 'test'
        );

然后,使用 array_push() 我需要將 $newdata 數組附加到以下多維數組:

and then, using array_push() I need to append the $newdata arrays to the following multidimensional array:

$md_array= array (
     'recipe_type' => 
      array (
        18 => 
        array (
          'wpseo_title' => 'Salads',
          'wpseo_desc' => 'Hundreads of recipes for Salads',
          'wpseo_metakey' => ''
        ),
        19 => 
        array (
          'wpseo_title' => 'Main dishes',
          'wpseo_desc' => 'Hundreads of recipes for Main dishes',
          'wpseo_metakey' => ''
        )
      ),
     'cuisine' => 
      array (
        22 => 
        array (
          'wpseo_title' => 'Italian',
          'wpseo_desc' => 'Secrets from Sicily in a click',
          'wpseo_metakey' => ''
        ),
        23 => 
        array (
          'wpseo_title' => 'Chinese',
          'wpseo_desc' => 'Oriental dishes were never this easy to make',
          'wpseo_metakey' => ''
        ),
        24 => 
        array (
          'wpseo_title' => 'Greek',
          'wpseo_desc' => 'Traditional Greek flavors in easy to make recipies',
          'wpseo_metakey' => ''
        )
      ) 
    );

使用array_push 向recipe_type 數組添加新元素(數組)的語法是什么?我永遠無法理解多維數組,我有點困惑.

Whats the syntax for adding a new element (array) to the recipe_type array with array_push? I could never get my head around multidimensional arrays and I'm a bit confused.

推薦答案

如果你想在關聯數組中以遞增順序添加數據,你可以這樣做:

if you want to add the data in the increment order inside your associative array you can do this:

$newdata =  array (
      'wpseo_title' => 'test',
      'wpseo_desc' => 'test',
      'wpseo_metakey' => 'test'
    );

// for recipe

$md_array["recipe_type"][] = $newdata;

//for cuisine

 $md_array["cuisine"][] = $newdata;

這將被添加到食譜或菜肴中,具體取決于最后一個索引是什么.

this will get added to the recipe or cuisine depending on what was the last index.

Array push 通常用在有順序索引的數組中: $arr[0] , $ar[1] .. 不能直接在關聯數組中使用.但是由于您的子數組具有這種索引,您仍然可以像這樣使用它

Array push is usually used in the array when you have sequential index: $arr[0] , $ar[1].. you cannot use it in associative array directly. But since your sub array is had this kind of index you can still use it like this

array_push($md_array["cuisine"],$newdata);

這篇關于PHP 使用 array_push 向多維數組添加元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 一区二区三区视频播放 | av网站免费在线观看 | 午夜a v电影 | 一级在线观看 | 99精品久久 | 国产精品精品久久久 | 免费久久网 | 久久久久久成人 | 韩日在线观看视频 | 九九精品在线 | 国产免费高清 | 草久久久| sese视频在线观看 | 岛国二区| 九一视频在线观看 | 欧美视频二区 | 亚洲国产成人精品女人久久久野战 | 中文字幕视频免费 | 国产精品国产自产拍高清 | 久久国产免费 | 国产免国产免费 | 日韩中文字幕在线不卡 | 一区二区高清在线观看 | 91免费视频 | 黄色国产区 | 国产精品伦理一区 | 国产乱码久久久 | 中国大陆高清aⅴ毛片 | 黄色网址在线免费播放 | 91av久久久 | 久草综合在线视频 | 久久只有精品 | 香蕉久久久 | 97国产爽爽爽久久久 | 成人网视频| 午夜免费| 欧美久久久久 | 91资源在线 | 中文字幕亚洲一区二区三区 | 国产999精品久久久久久 | 亚洲精品久久久久久久久久久 |