0

I'm a PHP developer who typically uses arrays in lieu of objects, and I'd like to know if there are any best practices for object manipulation based on key values.

Take the following multi-dimensional array of events, typical of something you'd pull directly out of the database.

$events = array(
   0=>array(
      'eventid'=>1,
      'title'=>'First Event',
      'date'=>'20131010'
   ),
   1=>array(
      'eventid'=>2,
      'title'=>'Second Event',
      'date'=>'20131022'
   ),
   2=>array(
      'eventid'=>3,
      'title'=>'Third Event',
      'date'=>'20131010'
   ),
),

For display purposes in my template, I want to make this a multi-dimensional array based on the date first. This is fairly easy transformation that I use a helper function for.

assoc($events,'date','eventid');

Resulting in:

$events = array(
   20131010=>array(
       1 => array(
           'eventid'=>1,
           'title'=>'First Event',
           'date'=>'20131010'
       ),
       3 => array(
           'eventid'=>3,
           'title'=>'Third Event',
           'date'=>'20131010'
       ),
   ),
   20131022=>array(
       2 => array(
           'eventid'=>1,
           'title'=>'First Event',
           'date'=>'20121010'
       ),
   ),
),

In this way, I can easily run through the array on the template side for each date, create a header and for that date, and then display events for that day underneath.

I could go ahead and try to make a similar assoc() function for Objects, but it strikes me this should be a fairly common thing and there may be a standard way of doing it. If you had an object instead of an array for $events, how would you go about formatting it the second way I've described, or would you?

One additional piece of information: I am using CodeIgniter, in case it has some helper functions that I'm not aware of that could be useful.

daprezjer
  • 75
  • 5
  • 1
    Object/array doesn't make much difference for how the data is retrieved or sorted. What you've done is common practice, and the alternative is sorting your data correctly in your database query. – scrowler Dec 22 '13 at 21:37
  • Are you talking stdObject or a custom class ? – nl-x Dec 22 '13 at 22:27
  • nl-x, since I don't know the answer to that, I'm guessing that's a problem. But, if I var_dump, it says stdObject I believe. It's what's retrieved from CodeIgniter's standard active record call. $query = $this->db->get('events'); return $query->result(); – daprezjer Dec 22 '13 at 22:59

1 Answers1

0

In your assoc function you could do an is_object check and cast to an array if it is an object.

function your_assoc_func($events){
  if(is_object($events)){
    $events = (array)$events
  }

  // If the object is more complex cast any containing objects you may run across
  foreach($events as $event){
    if(is_object($event)){
      $event = (array)$event
    }
  }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2923779
  • 230
  • 1
  • 4
  • Yes, I could easily do that. Just not sure if that's the common practice. – daprezjer Dec 22 '13 at 23:00
  • More specifically, I'm wondering if it's common practice to change the object to an array when something like this is needed. Now that I'm using objects more and seeing a lot of things like $message->title, it seems odd to do this transformation and suddenly see $events['title']. – daprezjer Dec 22 '13 at 23:26
  • I see what you are saying - you pass in an object and get back an array. You could easily hold a flag to make sure you return an object. The only other option I see is to declare a custom Event class and make it iterable: http://www.php.net/manual/en/language.oop5.iterations.php – user2923779 Dec 22 '13 at 23:32
  • Thanks for the info. I hope this is my last question, which will decide which direction I go. Which is more common when sending information to a template? Is the information typically in array or object form by the time it gets sent to the template. If the former, then I'll make all my objects into arrays before passing. If the latter, I'll attempt to keep objects objects and try to create an assoc() function that returns an object. – daprezjer Dec 23 '13 at 01:15
  • I think it is really whatever you are comfortable with. I personally like to manipulate data as an array of objects indexed by some sort of GUID. This is a really good discussion I just quickly found about the performance of arrays vs objects. http://stackoverflow.com/questions/2193049/php-objects-vs-arrays. Maybe this will help you with your decision. – user2923779 Dec 23 '13 at 03:33