【问题标题】:Simple formatting of multidimensional array from mysql in phpphp中mysql中多维数组的简单格式化
【发布时间】:2009-10-10 23:59:54
【问题描述】:

我有一组来自 mysql 关系表的结果。它看起来像这样:

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 分钟我完全糊涂了 =)

提前感谢您的帮助!

【问题讨论】:

    标签: php mysql arrays multidimensional-array


    【解决方案1】:
    <?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>';
    

    【讨论】:

    • 非常感谢!正是我需要的!我唯一需要更改的是 $event['eventID'] 到对象语法 $event->eventID。感谢您的快速帮助;-)
    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多