我已经看过很多关于如何从2D数组中获取数据的教程,但是我需要构建一个类似于此的教程,我没有找到任何可以遵循的逻辑:
$array = array("socks" => array("blue", "red", "green"),
"shirts" => array("small", "medium", "large"));
我似乎无法弄清楚甚至启动代码的逻辑......
for each clothingType // I did this
get options // I did this
for each option //I did this
add to the clothingOption Array //... help!
我只是停留在clothesOption 2D Array
的建筑物上 可能有点像foreach clothingType as $kClothes =>VClothes
get Options
for each Options as $kOptions =>$VOption
$array[$VClothes][]= $VOption
谢谢,我希望这不是太模糊......
答案 0 :(得分:0)
也许(抱歉错误 - 手工):
//$array = array("socks" => array("blue", "red", "green"),
// "shirts" => array("small", "medium", "large"));
$types = array('socks', 'shirts');
$socks = array("blue", "red", "green");
$shirts = array("small", "medium", "large");
$array = array();
foreach ($types as $type) {
foreach($$type as $cloth) {
$array[$type][] = $cloth;
}
}
print_r($array);
答案 1 :(得分:0)
以下情况如何?
<?php
function getMe($type) {
//do some processing and construct array
return array('a','b','c');
}
$myTypes = array('type1','type2','type3');
$answer = array();
foreach($myTypes as $val) {
$answer[$val] = getMe($val);
}
print_r($answer);
?>
输出:
Array ( [type1] => Array ( [0] => a [1] => b [2] => c ) [type2] => Array ( [0] => a [1] => b [2] => c ) [type3] => Array ( [0] => a [1] => b [2] => c ) )