用于比较的PHP高级数组循环概念
我有一个如下的数组用于比较的PHP高级数组循环概念,php,arrays,Php,Arrays,我有一个如下的数组 array(2) { ["gender"]=> array(2) { [1]=> string(4) "Male" [2]=> string(6) "Female" } ["agegroup"]=> array(3) { [3]=> string(7) "18 - 24" [4]=> string(7) "25 - 40" [5]=>
array(2) {
["gender"]=>
array(2) {
[1]=>
string(4) "Male"
[2]=>
string(6) "Female"
}
["agegroup"]=>
array(3) {
[3]=>
string(7) "18 - 24"
[4]=>
string(7) "25 - 40"
[5]=>
string(7) "40 - 65"
}
}
array(2) {
["location"]=>
array(2) {
[A]=>
string(4) "New York"
[B]=>
string(6) "London"
}
["gender"]=>
array(2) {
[1]=>
string(4) "Male"
[2]=>
string(6) "Female"
}
["agegroup"]=>
array(3) {
[3]=>
string(7) "18 - 24"
[4]=>
string(7) "25 - 40"
[5]=>
string(7) "40 - 65"
}
}
这个数组是动态的。与“性别”和“年龄组”一样,可以有任意数量的项目。所有这些项都是子数组(关联)。我想在php中编写一个循环,从第一个数组元素循环到下一个数组元素中的每个元素
In the above example... the following output should come...
Male- 18-24
Male- 25-40
Male- 40-65
FeMale- 18-24
FeMale- 25-40
FeMale- 40-65
如果数组如下所示
array(2) {
["gender"]=>
array(2) {
[1]=>
string(4) "Male"
[2]=>
string(6) "Female"
}
["agegroup"]=>
array(3) {
[3]=>
string(7) "18 - 24"
[4]=>
string(7) "25 - 40"
[5]=>
string(7) "40 - 65"
}
}
array(2) {
["location"]=>
array(2) {
[A]=>
string(4) "New York"
[B]=>
string(6) "London"
}
["gender"]=>
array(2) {
[1]=>
string(4) "Male"
[2]=>
string(6) "Female"
}
["agegroup"]=>
array(3) {
[3]=>
string(7) "18 - 24"
[4]=>
string(7) "25 - 40"
[5]=>
string(7) "40 - 65"
}
}
然后。。。输出应该是
New York- Male- 18-24
New York- Male- 25-40
New York- Male- 40-65
New York- FeMale- 18-24
New York- FeMale- 25-40
New York- FeMale- 40-65
London- Male- 18-24
London- Male- 25-40
London- Male- 40-65
London- FeMale- 18-24
London- FeMale- 25-40
London- FeMale- 40-65
如果定义了数组长度,我将能够使用foreach()
来编写代码段。但父数组和子数组的数组长度都是动态的。。。有人能告诉我如何循环它以获得所需的输出吗
function make ($arr, $pref = '') { // pref - saving path to this point
foreach (array_shift($arr) as $item) // take the 1st item of array and remove it
if($arr) make($arr,$pref . $item ." - "); // Call with sub-tree, add item to path
else echo $pref . $item ."\n"; // Empty array - we are at leaf
}
make($arr);
第2例结果:
New York - Male - 18 - 24
New York - Male - 25 - 40
New York - Male - 40 - 65
New York - Female - 18 - 24
New York - Female - 25 - 40
New York - Female - 40 - 65
London - Male - 18 - 24
London - Male - 25 - 40
London - Male - 40 - 65
London - Female - 18 - 24
London - Female - 25 - 40
London - Female - 40 - 65
第2例结果:
New York - Male - 18 - 24
New York - Male - 25 - 40
New York - Male - 40 - 65
New York - Female - 18 - 24
New York - Female - 25 - 40
New York - Female - 40 - 65
London - Male - 18 - 24
London - Male - 25 - 40
London - Male - 40 - 65
London - Female - 18 - 24
London - Female - 25 - 40
London - Female - 40 - 65
第2例结果:
New York - Male - 18 - 24
New York - Male - 25 - 40
New York - Male - 40 - 65
New York - Female - 18 - 24
New York - Female - 25 - 40
New York - Female - 40 - 65
London - Male - 18 - 24
London - Male - 25 - 40
London - Male - 40 - 65
London - Female - 18 - 24
London - Female - 25 - 40
London - Female - 40 - 65
第2例结果:
New York - Male - 18 - 24
New York - Male - 25 - 40
New York - Male - 40 - 65
New York - Female - 18 - 24
New York - Female - 25 - 40
New York - Female - 40 - 65
London - Male - 18 - 24
London - Male - 25 - 40
London - Male - 40 - 65
London - Female - 18 - 24
London - Female - 25 - 40
London - Female - 40 - 65
OP几乎不理解循环,所以你给他们递归?总有一天他会理解递归,为什么现在不行?他写了关于不同数量的循环levels@Viral因为大多数人在跑步之前先学会走路。加上迭代几乎总是比递归快。OP几乎不理解循环,所以你给他们递归?总有一天他必须理解递归,为什么不是现在?他写了不同数量的循环levels@Viral因为大多数人在跑步之前先学会走路。加上迭代几乎总是比递归快。OP几乎不理解循环,所以你给他们递归?总有一天他必须理解递归,为什么不是现在?他写了不同数量的循环levels@Viral因为大多数人在跑步之前先学会走路。加上迭代几乎总是比递归快。OP几乎不理解循环,所以你给他们递归?总有一天他必须理解递归,为什么不是现在?他写了不同数量的循环levels@Viral因为大多数人在跑步之前先学会走路。加上迭代几乎总是比递归快。你要找的关键词是“笛卡尔积”,而且随着时间的推移,它一直在要求相同的结果。@salay,Realy,duplicate。但是有太复杂的解决方案:)你要找的关键词是“笛卡尔积”,而且随着时间的推移,你一直在要求同样的结果。@salay,Realy,replicate。但是有太复杂的解决方案:)你要找的关键词是“笛卡尔积”,而且随着时间的推移,你一直在要求同样的结果。@salay,Realy,replicate。但是有太复杂的解决方案:)你要找的关键词是“笛卡尔积”,而且随着时间的推移,你一直在要求同样的结果。@salay,Realy,replicate。但有太复杂的解决方案:)