我有一些数据,从MySQL查询这种方式来在一个平面阵列:PHP转换平板嵌套多维数组中的foreach循环
0 => Array
indcode => "A00"
indlabel => "Code label text"
description => "More explanations"
1 => Array
indcode => "NA0"
indlabel => "Un-classified A"
description => "Un-classified A"
2 => Array (3)
indcode => "A01"
indlabel => "Code Label text"
description => "More explanations"
3 => Array (3)
indcode => "A02"
indlabel => "Code label text"
description => "More explanations"
我想窝这种方式:
A00 => Array
indlabel => "Code label text"
description => "More explanations"
NA0 => Array
indlabel => "Un-classified A"
description => "Un-classified A"
A01 => Array
indlabel => "Code Label text"
description => "More explanations"
A02 => Array
indlabel => "Code label text"
description => "More explanations"
所以在我的CMS我在使用中发现了一个非常整洁的代码确实嵌套:
foreach ($dimsDesc as $desc) {
$descriptions[$desc['indcode']][$desc['indlabel']] = $desc['description'];
}
这一工程b我没有找到如何在同一级别(=等号的另一侧)保留indlabel和描述。如果你有其他一些例子的链接或者这个构造的一个很好的参考,那将会被赞赏,因为我将用它来构建动态的报告......并且现在PDO查询有点不在我的触及。我还用array_column()
与NULL
其作品,但我有更多的进入复杂的数据结构...
谢谢,所有人都会工作,但特别是第一个例子,即使它看起来不如第二个简洁,它也帮助我理解我错过了什么! –
非常感谢。我想知道是否有一些(书籍,在线文档,...),我可以找到一些例子或解释。我特别想知道为什么这会起作用(而不是覆盖值或复制数组......).. –
@ Joel.O这里的文档,[http://php.net/manual/en/language.types.array .PHP#language.types.array.syntax.modifying](http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying) –