[英]Simple formatting of multidimensional array from mysql in php
I have an array of results from mysql relational tables. 我有一系列来自mysql关系表的结果。 It looks similar to this:
它看起来类似于:
array(10) {
[0]=>
object(stdClass)#14 (35) {
["eventID"]=>
string(1) "1"
["eventTitle"]=>
string(7) "EVENT 1"
["artistID"]=>
string(1) "1"
["artistName"]=>
string(8) "ARTIST 1"
["artistDescription"]=>
string(20) "ARTIST 1 description"
// etc.
}
[1]=>
object(stdClass)#15 (35) {
["eventID"]=>
string(1) "1"
["eventTitle"]=>
string(7) "EVENT 1"
["artistID"]=>
string(1) "2"
["artistName"]=>
string(8) "ARTIST 2"
["artistDescription"]=>
string(20) "ARTIST 2 description"
// etc.
}
[2]=>
object(stdClass)#16 (35) {
["eventID"]=>
string(1) "1"
["eventTitle"]=>
string(7) "EVENT 1"
["artistID"]=>
string(1) "3"
["artistName"]=>
string(8) "ARTIST 3"
["artistDescription"]=>
string(20) "ARTIST 3 description"
// etc.
}
[3]=>
object(stdClass)#17 (35) {
["eventID"]=>
string(1) "2"
["eventTitle"]=>
string(7) "EVENT 2"
["artistID"]=>
string(1) "5"
["artistName"]=>
string(8) "ARTIST 5"
["artistDescription"]=>
string(20) "ARTIST 5 description"
// etc.
}
[4]=>
object(stdClass)#17 (35) {
["eventID"]=>
string(1) "2"
["eventTitle"]=>
string(7) "EVENT 2"
["artistID"]=>
string(1) "7"
["artistName"]=>
string(8) "ARTIST 7"
["artistDescription"]=>
string(20) "ARTIST 7 description"
// etc.
}
//etc.
}
I want to make a multidimensional array which would look like this one: 我想制作一个看起来像这样的多维数组:
array(2) {
[1]=>
array(9) {
["eventID"]=>
string(1) "1"
["eventTitle"]=>
string(7) "EVENT 1"
["artists"]=>
array(3) {
[1]=>
array(2) {
["artistName"]=>
string(8) "ARTIST 1"
["artistDescription"]=>
string(20) "ARTIST 1 description"
}
[2]=>
array(2) {
["artistName"]=>
string(8) "ARTIST 2"
["artistDescription"]=>
string(20) "ARTIST 2 description"
}
[2]=>
array(2) {
["artistName"]=>
string(8) "ARTIST 3"
["artistDescription"]=>
string(20) "ARTIST 3 description"
}
}
}
[2]=>
array(9) {
["eventID"]=>
string(1) "2"
["eventTitle"]=>
string(7) "EVENT 2"
["artists"]=>
array(3) {
[1]=>
array(2) {
["artistName"]=>
string(8) "ARTIST 5"
["artistDescription"]=>
string(20) "ARTIST 5 description"
}
[2]=>
array(2) {
["artistName"]=>
string(8) "ARTIST 2"
["artistDescription"]=>
string(20) "ARTIST 7 description"
}
[2]=>
array(2) {
["artistName"]=>
string(8) "ARTIST 3"
["artistDescription"]=>
string(20) "ARTIST 7 description"
}
}
}
}
I started to create an array in a FOR
loop but I was stuck with creating this artists[] array and I'm totally confused last 30 mins =) 我开始在
FOR
循环中创建一个数组,但是我被困在创建这个artist []数组的过程中,最后30分钟我完全感到困惑=)
Thank you in advance for any help! 预先感谢您的任何帮助!
<?php
$output = array();
foreach($resultset as $event)
{
$eventId = $event['eventID'];
$artistId = $event['artistID'];
//Using the eventId as the index ensures the event is unique and easy to lookup in the array.
$output[$eventId]['eventTitle'] = $event['eventTitle'];
$output[$eventId]['eventID'] = $event['eventID'];
//Using the 'artistID' as the index of artists ensures each artist is unique and easy to lookup.
$output[$eventId]['artists'][$artistId]['artistID'] = $artistId;
$output[$eventId]['artists'][$artistId]['artistName'] = $event['artistName'];
$output[$eventId]]['artists'][$artistId]['artistDescription'] = $event['artistDescription'];
}
echo '<pre>' . print_r($output,true) . '</pre>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.