0

Hello!
I already posted a similar problem and it was resolved, but now I'm trying to work with array of objects and I'm not sure how to resolve this:

So, I have an array similar to this:

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

And I need to format it in this way:

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

Thanks for any help!

flag

Describe your problem please. – Jan Hančič Jan 4 at 15:42
Well, I need to 'group' some of data here from first array... I'm not sure how to do it :) I cannot reference to some ID like this $output[$eventId] (see my previous problem - link in the first sentence) because I'm working with objects now, not an array... – errata Jan 4 at 15:47

2 Answers

1

A modified version of the answer to your previous question - will this do it?

<?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>';
link|flag
Perfect! Many thanks for quick help!! – errata Jan 4 at 17:21
1

In first place, how did you end up with these objects? Can't you create the objects the way you want it upfront?

Either way you can do

$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
link|flag
I'm pulling some data from MySQL relational database... I think I cannot create objects which I need directly with MySQL query...? Your solution looks interesting and I will try it for sure, but I choose the other solution as answer because it was first here... Thank you very much! – errata Jan 4 at 17:24
It depends on your query, but it's possible. Even with the same query you could populate the objects the right way straight ahead... – michal kralik Jan 5 at 11:20

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.