[英]How can i create a Multi-Dimensional array as the following in php?
Hi guys i am trying to create a multi dimensional array as the format bellow but i am struggling to do so, any help would be appreciated. 嗨,大家好,我正在尝试创建以下格式的多维数组,但我正努力做到这一点,我们将不胜感激。 Thank you. 谢谢。
$movies = array();
$sql = "SELECT u.username AS Username, group_concat(v.title) AS Video_title, group_concat(r.rate) AS Rating
FROM `rating` AS r
JOIN `uploads` AS v ON r.video_id = v.vid_id
JOIN `users` as u ON u.user_id = r.rater_id
GROUP by Username" ;
$query = mysqli_query($conn, $sql);
while($result = mysqli_fetch_assoc($query)){
$movies[] = $result;
}
foreach($movies as $k => $v){
unset($movies[$k]);
$movies[$v['Username']] = $v;
}
foreach($movies as $k => $v){
unset($movies[$k]['Username']);
}
print_r($movies);
This is what i am currently getting: 这是我目前得到的:
[user1] => Array ( [Video_title] => Snakes on the Plane,
Superman Returns,
The Night Listner,
Lady in the Water,
Just my Luck,
You me and Dupree
[Rating] => 3.5,
3.5,
3,
2.5,
3,
2.5
) )
But i am aiming to output it in the following format: 但我的目标是将其输出为以下格式:
'user1' =>
array('Lady in the Water' => 2.5,
'Snakes on a Plane' => 3.5,
'Just My Luck' => 3.0,
'Superman Returns' => 3.5,
'You, Me and Dupree' => 2.5,
'The Night Listener' => 3.0
)
Change the following line: 更改以下行:
$movies[$v['Username']] = $v;
to 至
$movies[$v['Username']][] = $v;
and try again. 然后再试一次。 It will create a new array under Username
index for each iteration. 它将为每次迭代在“ Username
索引下创建一个新数组。
Try this code for your aiming result: 尝试以下代码以获得目标结果:
$movies = array();
$sql = "
SELECT u.username AS Username, v.title AS Video_title, r.rate AS Rating
FROM `rating` AS r
JOIN `uploads` AS v ON r.video_id = v.vid_id
JOIN `users` as u ON u.user_id = r.rater_id
GROUP by Username" ;
$query = mysqli_query($conn, $sql);
while($result = mysqli_fetch_assoc($query)){
$movies[$result['Username']][$result['Video_title']] = $result['Rating'];
}
print_r($movies);
You have to do it like below:- 你必须像下面这样做:
$movies = array();
$sql = "SELECT u.username AS Username, group_concat(v.title) AS Video_title, group_concat(r.rate) AS Rating
FROM `rating` AS r
JOIN `uploads` AS v ON r.video_id = v.vid_id
JOIN `users` as u ON u.user_id = r.rater_id
GROUP by Username" ;
$query = mysqli_query($conn, $sql);
while($result = mysqli_fetch_assoc($query)){
$movies[] = $result;
}
$new_movies = array(); // a new array variable
foreach($movies as $k => $v){
$array_keys = explode(',',$v['Video_title']); // explode Video_title string into array
$trimmed_array_key=array_map('trim',$array_keys); // trim all spaces from each value of $trimmed_array_key array
$array_values = explode(',',$v['Rating']); // explode Rating array
$trimmed_array_values=array_map('trim',$array_values);//remove all spaces from $trimmed_array_values array
$new_movies[$v['Username']] = array_combine($trimmed_array_key,$trimmed_array_values); // combine $trimmed_array_key array and $trimmed_array_values array and assign to corresponding user
}
echo "<pre/>";print_r($new_movies); // print new array
Note:- 注意:-
This line:- $trimmed_array_key=array_map('trim',$array_keys);
这行代码:- $trimmed_array_key=array_map('trim',$array_keys);
Will do something like below:- 将执行以下操作:
https://eval.in/766605 https://eval.in/766605
reference:- http://php.net/manual/en/function.array-combine.php 参考: -http : //php.net/manual/zh/function.array-combine.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.