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

使用 foreach 循環(huán)創(chuàng)建多維數(shù)組

create multidimensional array using a foreach loop(使用 foreach 循環(huán)創(chuàng)建多維數(shù)組)
本文介紹了使用 foreach 循環(huán)創(chuàng)建多維數(shù)組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用 foreach 循環(huán)在 PHP 中創(chuàng)建一個多維數(shù)組.這是目前為止的代碼:

$levels = array('low', 'medium', 'high');$attributes = array('fat', 'quantity', 'ratio', 'label');foreach ($levels as $key => $level):foreach ($attributes as $k =>$attribute):$variables[] = $attribute .'_' .$級別;Endforeach;Endforeach;echo '

'.print_r($levels,1) .'</pre>';echo '

'.print_r($variables,1) .'</pre>';

這段代碼的輸出是一個一維數(shù)組;然而,這不是本意.所需的數(shù)組應如下所示:

應該如何修改代碼才能達到目標?

解決方案

您就快到了.只需將級別添加到數(shù)組創(chuàng)建中:)

$levels = array('low', 'medium', 'high');$attributes = array('fat', 'quantity', 'ratio', 'label');foreach ($levels as $key => $level):foreach ($attributes as $k =>$attribute):$variables[$level][] = $attribute .'_' .$級別;//將 $variables[] 改為 $variables[$level][]Endforeach;Endforeach;echo '

'.print_r($levels,1) .'</pre>';echo '

'.print_r($variables,1) .'</pre>';

輸出

數(shù)組([低] =>大批([0] =>脂肪_低[1] =>數(shù)量_低[2] =>比率_低[3] =>標簽低)[中] =>大批([0] =>肥中[1] =>數(shù)量_中等[2] =>比率_中[3] =>label_medium)[高] =>大批([0] =>脂肪高[1] =>數(shù)量_高[2] =>比率_高[3] =>標簽高))

I am trying to create a multidimensional array in PHP using a foreach loop. Here is the code thus far:

$levels = array('low', 'medium', 'high');
$attributes = array('fat', 'quantity', 'ratio', 'label');

foreach ($levels as $key => $level):
       foreach ($attributes as $k =>$attribute):
             $variables[] = $attribute . '_' . $level;
       endforeach;
endforeach;

echo '<pre>' . print_r($levels,1) . '</pre>';   
echo '<pre>' . print_r($variables,1) . '</pre>';    

The output from this code is a single dimension array; however, that is not the intent. The desired array should look like this:

How should the code be modified to achieve the goal?

解決方案

You're aaalmost there. Just add the level to the array creation :)

$levels = array('low', 'medium', 'high');
$attributes = array('fat', 'quantity', 'ratio', 'label');

foreach ($levels as $key => $level):
       foreach ($attributes as $k =>$attribute):
             $variables[$level][] = $attribute . '_' . $level; // changed $variables[] to $variables[$level][]
       endforeach;
endforeach;

echo '<pre>' . print_r($levels,1) . '</pre>';   
echo '<pre>' . print_r($variables,1) . '</pre>';  

Output

Array
(
    [low] => Array
        (
            [0] => fat_low
            [1] => quantity_low
            [2] => ratio_low
            [3] => label_low
        )

    [medium] => Array
        (
            [0] => fat_medium
            [1] => quantity_medium
            [2] => ratio_medium
            [3] => label_medium
        )

    [high] => Array
        (
            [0] => fat_high
            [1] => quantity_high
            [2] => ratio_high
            [3] => label_high
        )

)

這篇關(guān)于使用 foreach 循環(huán)創(chuàng)建多維數(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)
主站蜘蛛池模板: 国产亚洲精品久久久优势 | 人人操日日干 | 黑人巨大精品欧美一区二区免费 | 99爱在线免费观看 | 久久久久av | 中文字幕在线一 | 性欧美精品一区二区三区在线播放 | 久久久精品黄色 | 精品国产一区二区三区性色av | 一区二区三区国产精品 | 国产欧美日韩精品一区二区三区 | 特级毛片www | 日韩av在线中文字幕 | 国产福利91精品一区二区三区 | 欧美精品一区二区三区在线 | 久久99蜜桃综合影院免费观看 | 黄色综合 | 一区二区三区欧美在线 | 一级欧美 | 午夜精品久久久久久久久久久久 | 黄色网址在线播放 | 亚洲日韩中文字幕一区 | 日韩成人| 91资源在线| 蜜桃av鲁一鲁一鲁一鲁 | 91视频免费视频 | www精品| 久久久久一区 | 国产乱码精品一区二区三区忘忧草 | 久草99| 中文字幕成人av | 毛片a级 | 久久机热 | 中国美女撒尿txxxxx视频 | 免费观看av| 羞羞视频在线观免费观看 | 黑人精品xxx一区一二区 | 欧美精品一区二区三区在线播放 | 久久午夜剧场 | 日本五月婷婷 | 国产伦精品一区二区三区在线 |