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

PHP 使用 array_push 向多維數(shù)組添加元素

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

問題描述

我有一個多維數(shù)組 $md_array,我想向來自從表中讀取數(shù)據(jù)的循環(huán)的子數(shù)組 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.

在循環(huán)中,我為每一行創(chuàng)建一個新表 $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 數(shù)組附加到以下多維數(shù)組:

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 數(shù)組添加新元素(數(shù)組)的語法是什么?我永遠(yuǎn)無法理解多維數(shù)組,我有點困惑.

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.

推薦答案

如果你想在關(guān)聯(lián)數(shù)組中以遞增順序添加數(shù)據(jù),你可以這樣做:

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 通常用在有順序索引的數(shù)組中: $arr[0] , $ar[1] .. 不能直接在關(guān)聯(lián)數(shù)組中使用.但是由于您的子數(shù)組具有這種索引,您仍然可以像這樣使用它

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);

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

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

相關(guān)文檔推薦

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 設(shè)置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動填充選擇框)
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 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 日本一级黄色 | 日韩一级在线观看 | 青青草视频在线观看 | 国产精品久久 | av久久 | 黄色国产精品 | 小镇姑娘国语版在线观看免费 | 四虎黄色网址 | 五月婷婷中文字幕 | 亚洲乱码一区二区 | 超碰91在线 | 无毒不卡 | 日韩一级片视频 | 欧美人与性动交α欧美精品 | 在线免费看av| 免费av在线播放 | 久热综合 | 中文字幕在线观看免费 | 欧美久久综合 | 免费黄色网页 | 日韩成人免费 | 伊人999| 日韩成人精品一区二区 | 午夜免费福利视频 | 91成人免费 | 国产suv一区二区 | 三级视频在线观看 | 一级毛片免费看 | www.色网| 岛国av噜噜噜久久久狠狠av | 久久久久久久91 | 伊人黄色 | 国产一区精品在线观看 | 国产aⅴ爽av久久久久成人 | 亚洲免费在线观看视频 | 精品免费| 国产黄色在线播放 | 亚洲成人日韩 | 国产午夜三级一区二区三 | 精品亚洲一区二区三区四区五区 | 国产精品久久久国产盗摄 |