I know this is a simple question, and one that has certainly been asked but my googlefu is seriously failing me here and I'm drowning.
我知道这是一个简单的问题,而且肯定会被问到,但是我的googlefu在这里严重失败了,我淹死了。
I have an array of values like so
我有一组像这样的值
array(
array(topCat: "1", secondCat: "1", listItem: "List Item 1"),
array(topCat: "1", secondCat: "1", listItem: "List Item 2"),
array(topCat: "1", secondCat: "2", listItem: "List Item 1"),
array(topCat: "1", secondCat: "3", listItem: "List Item 2"),
array(topCat: "2", secondCat: "1", listItem: "List Item 1")) etc etc
I need it to be like this:
我需要它像这样:
array(
array(topCat: 1, secondCat: 2, array("list item 1", "List Item 2"))).
One of my searches gave me this:
我的一次搜索给了我这个:
$newArray = array();
foreach ($oldArray as $row){
$newArray[$row['topCat']][$row['secondCat']][] = $row['listItem'];
}
And that almost works except it assigns the key to the value so it becomes array(1, 2: array())
这几乎可以工作,除了它为键赋值,所以它成为数组(1,2:array())
Which isn't what I want.
这不是我想要的。
I need to loop over the values and assign those as keys, how do I do this in PHP?
我需要遍历值并将它们分配为键,我如何在PHP中执行此操作?
In the end the array needs to have 3 dimensions, topCat which would contain all of the second categories, secondCat which would contain all of the list items.
最后,数组需要有3个维度,topCat包含所有第二个类别,secondCat包含所有列表项。
1 个解决方案
#1
1
try this :
尝试这个 :
<pre><?php
$oldArray = array(
array('topCat'=> "1", 'secondCat'=> "1", 'listItem'=> "List Item 1"),
array('topCat'=> "1", 'secondCat'=> "1", 'listItem'=> "List Item 2"),
array('topCat'=> "1", 'secondCat'=> "2", 'listItem'=> "List Item 1"),
array('topCat'=> "1", 'secondCat'=> "3", 'listItem'=> "List Item 2"),
array('topCat'=> "2", 'secondCat'=> "1", 'listItem'=> "List Item 1"));
var_dump($oldArray);
$newArray = array();
$newArray = array();
foreach ($oldArray as $row){
$newArray[$row['topCat']][$row['secondCat']][] = $row['listItem'];
}
var_dump($newArray);
$t2 = array();
foreach($newArray as $index=>$back){
foreach($back as $index2=>$back2){
$t2[] = array('topCat'=>$index, 'secondCat'=>$index2, 'listItem'=>$back2);
}}
var_dump($t2);
returns :
回报:
array(4) {
[0]=>
array(3) {
["topCat"]=>
int(1)
["secondCat"]=>
int(1)
["listItem"]=>
array(2) {
[0]=>
string(11) "List Item 1"
[1]=>
string(11) "List Item 2"
}
}
[1]=>
array(3) {
["topCat"]=>
int(1)
["secondCat"]=>
int(2)
["listItem"]=>
array(1) {
[0]=>
string(11) "List Item 1"
}
}
[2]=>
array(3) {
["topCat"]=>
int(1)
["secondCat"]=>
int(3)
["listItem"]=>
array(1) {
[0]=>
string(11) "List Item 2"
}
}
[3]=>
array(3) {
["topCat"]=>
int(2)
["secondCat"]=>
int(1)
["listItem"]=>
array(1) {
[0]=>
string(11) "List Item 1"
}
}
}
#1
1
try this :
尝试这个 :
<pre><?php
$oldArray = array(
array('topCat'=> "1", 'secondCat'=> "1", 'listItem'=> "List Item 1"),
array('topCat'=> "1", 'secondCat'=> "1", 'listItem'=> "List Item 2"),
array('topCat'=> "1", 'secondCat'=> "2", 'listItem'=> "List Item 1"),
array('topCat'=> "1", 'secondCat'=> "3", 'listItem'=> "List Item 2"),
array('topCat'=> "2", 'secondCat'=> "1", 'listItem'=> "List Item 1"));
var_dump($oldArray);
$newArray = array();
$newArray = array();
foreach ($oldArray as $row){
$newArray[$row['topCat']][$row['secondCat']][] = $row['listItem'];
}
var_dump($newArray);
$t2 = array();
foreach($newArray as $index=>$back){
foreach($back as $index2=>$back2){
$t2[] = array('topCat'=>$index, 'secondCat'=>$index2, 'listItem'=>$back2);
}}
var_dump($t2);
returns :
回报:
array(4) {
[0]=>
array(3) {
["topCat"]=>
int(1)
["secondCat"]=>
int(1)
["listItem"]=>
array(2) {
[0]=>
string(11) "List Item 1"
[1]=>
string(11) "List Item 2"
}
}
[1]=>
array(3) {
["topCat"]=>
int(1)
["secondCat"]=>
int(2)
["listItem"]=>
array(1) {
[0]=>
string(11) "List Item 1"
}
}
[2]=>
array(3) {
["topCat"]=>
int(1)
["secondCat"]=>
int(3)
["listItem"]=>
array(1) {
[0]=>
string(11) "List Item 2"
}
}
[3]=>
array(3) {
["topCat"]=>
int(2)
["secondCat"]=>
int(1)
["listItem"]=>
array(1) {
[0]=>
string(11) "List Item 1"
}
}
}