PHP foreach循环构建多阵列 [英] PHP foreach loop to build multi array
问题描述
我试图构建一个多阵列这样的结构:
阵列
(
[2] =>数组//这是用户的唯一ID
(
[名] =>插口
[位置] =>萨默维尔,马萨诸塞州,美国
[汽车] =>排列
(
[10] =>丰田//这是汽车的唯一ID
[11] =>保时捷
)
) [5] =>排列
(
[名] =>卢克
[位置] =>施韦尔姆,北莱茵 - 威斯特法伦州,德国
[汽车] =>排列
(
[1] =>本田
[2] => VW
[5] =>玛莎拉蒂
)
) [1] =>排列
(
[名] =>贾巴
[位置] =>丹佛,科罗拉多州,美国
[汽车] =>排列
(
[3] =>特斯拉
)
)
)
我使用这个的foreach
循环,但我坚持在得到汽车
阵列嵌入<$ C $内C>搜索数据阵列。
每个用户可以拥有一个以上的车,所以我需要遍历所有汽车为每个用户生成阵列,并把它放在原始的的foreach
循环。
$ search_data =阵列();
的foreach($查询 - &GT;的结果()为$行){
$ search_data [$按行和SEQ ID =阵列( '名'=&GT; $行向&GT;的名字,
位置= GT; $行向&GT;的位置,
'车'=&GT;阵列($行向&GT; car_id),//这是我需要插入另一个数组
);
}
返回$ search_data;
任何建议,如何做到这一点?
感谢您的帮助!
样表数据
用户名位置CARS
2 JACK丰田
2 JACK保时捷
5作者:Luke B HONDA
5作者:Luke B VW
5作者:Luke B玛莎拉蒂
1 JABBAÇTESLA
看来你是通过创建一个数据库表中的数组。所以,可以请您给2个或更多的样本数据。它会更容易给出一个答案,如果样本数据是存在的。
编辑:
嗯,这可能不是最好的code,但我想,你就可以看到这后一个更好的办法弄清楚
的$ id = $行['身份证'];
如果(!使用isset($ search_data [$ ID])){
$ search_data [$ ID] =阵列();
}
$ search_data [$ ID] ['名'] = $行[名称];
$ search_data [$ ID] ['位置'] = $行['位置'];
如果(使用isset($ search_data [$ ID] ['汽车'])){
array_push($ search_data [$ ID] ['汽车'],$行['汽车']);
}其他{
$ search_data [$ ID] ['汽车'] =阵列($行['汽车']); //这是我需要插入另一个数组
}
I'm trying to build a multi array with this structure:
Array
(
[2] => Array //this is the user's unique ID
(
[name] => Jack
[location] => Somerville, Massachusetts, United States
[cars] => Array
(
[10] => Toyota //this is the car's unique ID
[11] => Porsche
)
)
[5] => Array
(
[name] => Luke
[location] => Schwelm, North Rhine-Westphalia, Germany
[cars] => Array
(
[1] => Honda
[2] => VW
[5] => Maserati
)
)
[1] => Array
(
[name] => Jabba
[location] => Denver, Colorado, United States
[cars] => Array
(
[3] => Tesla
)
)
)
I am using this foreach
loop but am stuck in getting the cars
array embedded within the search data
array.
Each user may have more than one car so I would need to loop through all cars for each user, generate that array, and put it in the original foreach
loop.
$search_data = array();
foreach ($query->result() as $row) {
$search_data[$row->id] = array(
'name' => $row->name,
'location' => $row->location,
'cars' => array($row->car_id), //this is where I need to insert another array
);
}
return $search_data;
Any suggestions how to do this?
Thanks for helping!
SAMPLE TABLE DATA
USER NAME LOCATION CARS
2 JACK A TOYOTA
2 JACK A PORSCHE
5 LUKE B HONDA
5 LUKE B VW
5 LUKE B MASERATI
1 JABBA C TESLA
It seems that you are creating the array through a database table. So can you please give 2 or more sample data. it'll be easier to give an answer if sample data is there.
Edit: Well this might not be the best code but I think you'll be able to figure out a better way after seeing this
$id = $row['id'];
if (!isset($search_data[$id])){
$search_data[$id] = array();
}
$search_data[$id]['name'] = $row['name'];
$search_data[$id]['location'] = $row['location'];
if (isset($search_data[$id]['cars'])) {
array_push($search_data[$id]['cars'],$row['cars']);
}else{
$search_data[$id]['cars'] = array($row['cars']); //this is where I need to insert another array
}
这篇关于PHP foreach循环构建多阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!