php - 在php中从mysql简单格式化多维数组

标签 php mysql arrays multidimensional-array

我有一个来自 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 循环中创建一个数组,但我一直在创建这个 arts[] 数组,最后 30 分钟我完全困惑了 =)

预先感谢您的帮助!

最佳答案

<?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>';

关于php - 在php中从mysql简单格式化多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1549420/

相关文章:

php - 使用 PHP 如何找到 CSS 标签/id/类并替换大括号内的样式?

php - 动态改变 CSS 背景图片

mysql - ColdFusion - 将阿拉伯/波斯字符插入 mysql

php - Jquery, PHP, MySQL Live Search 某些结果出现中断,如何避免?

arrays - Bash 脚本和 gdal - 如何引用数组变量中的元素?

arrays - 如何将 Swift 字符串集转换为数组

php - Docker上的Nginx默认页面

php - 通过wget ''在Docker中运行TYPO3

php - 太多的 mySql 事件

c - 如何在数组中找到匹配值并将它们存储在新数组中?