【问题标题】:Working with multidimensional array and objects in php在 php 中使用多维数组和对象
【发布时间】:2024-04-16 13:55:02
【问题描述】:

我已经发布了一个similar 问题并已解决,但现在我正在尝试处理对象数组,但不知道如何解决:

所以,我有一个类似这样的数组:

array(10) {
  [0]=>
  object(VO)#6 (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(VO)#7 (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(VO)#8 (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(VO)#9 (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(VO)#10 (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]=>
  object(VO)#6 (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]=>
  object(VO)#7 (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 7"
        ["artistDescription"]=>
        string(20) "ARTIST 7 description"
      }
    }
  }
}

感谢您的帮助!

【问题讨论】:

  • 请描述您的问题。
  • 好吧,我需要从第一个数组中“分组”这里的一些数据...我不知道该怎么做 :) 我无法引用像这样的一些 ID $output[$eventId ](请参阅我之前的问题 - 第一句中的链接)因为我现在使用的是对象,而不是数组...

标签: php mysql arrays object multidimensional-array


【解决方案1】:

首先,您是如何得到这些对象的?你不能按照你想要的方式创建对象吗?

你可以做任何事情

$in = array(0 => $obj1, 1 => $obj2, ...);
$out = array();
foreach($in as $obj) {
  if(isset($out[$obj->eventID]) {
    $out[$obj->eventID]->artists[] = array('artistName' => $obj->artistName, 'artistDescription' => $obj->artistDescription);
  } else {
    $out[$obj->eventID] = $obj;
    $obj->artists = array(
      array('artistName' => $obj->artistName, 'artistDescription' => $obj->artistDescription)
    );

    unset($obj->artistName);
    unset($obj->artistDescription);
  }
}

// $out contains your object

【讨论】:

  • 我正在从 MySQL 关系数据库中提取一些数据...我想我无法使用 MySQL 查询直接创建我需要的对象...?您的解决方案看起来很有趣,我肯定会尝试,但我选择其他解决方案作为答案,因为它是第一个在这里......非常感谢!
  • 这取决于您的查询,但这是可能的。即使使用相同的查询,您也可以直接以正确的方式填充对象...
【解决方案2】:

您上一个问题的答案的修改版本 - 可以这样做吗?

<?php
$output = array();

foreach($resultset as $event)
{

   $eventId = $event->eventID;
   $artistId = $event->artistID;

   if (!isset($output[$eventId]))
   {
      $output[$eventId] = new stdClass();

      $output[$eventId]->eventTitle = $event->eventTitle;
      $output[$eventId]->eventID = $event->eventID;
   }

   if (!isset($output[$eventId]->artists)) $output[$eventId]->artists = array();

   $output[$eventId]->artists[$artistId] = array();
   $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>';

【讨论】: