2009-10-10 29 views
0

多維數組的簡單的格式我有從MySQL關係表的結果的數組。它看起來類似於這樣:從MySQL在PHP

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. 
} 

我想打一個多維數組這將是這樣一個:

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" 
     } 
    } 
    } 
} 

我開始創建一個FOR循環數組,但我堅持與創建這個藝術家[]數組和我完全糊塗了最後30分鐘=)

預先感謝您的幫助!

回答

3
<?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>'; 
+0

非常感謝!正是我需要的! 我需要改變的唯一的事情是$事件[「EVENTID」]反對語法$事件 - >事件ID。感謝您的快速幫助;-) – errata