Getting rid of arrays in PHP



Ahoy!

Okay so want to know something I hate? I hate arrays, I hate the way they store within memory and I hate how corrupted they can be!

You want to know what I love? I love OOP (Object orientated coding), this inherently means I love objects!

So without further ado, lets get rid of all those arrays coming into your data matrix and replace them with objects… (oooooooh!)

Okay so as some of you may not be aware there is a freaking awesome little class in PHP called the stdclass.

so lets say you have the following array of data:

$videoIncoming = array(

‘name’ => “my special day”,

‘genre’ => “romantic”,

‘director’ => “Karl Kloppenborg”

);

Okay so this is a fairly standard array of data and in a normal operation would be totally acceptable to use provided that its now immutable (at least structure wise)

Arrays tend to get out of hand when you start having multidimensional arrays with heaps and heaps of data residing in their memory, this is a not so much of a design flaw in PHP but more of a coding flaw in the programmer.

The reason for this is simple:

PHP Assigns memory dynamically!

Arrays in PHP are mutable in all contexts, you (the programmer) tend to not take consideration for the memory assignment inside these arrays, PHP handles this for you by doing dynamic memory assignment, however this leads to massive segfault problems or memory out of range problems, this tends to only happen when multidimensional arrays are used for storage matrix’s or you start adding and removing lots of inner data from these “multidimensional” arrays.

Oh and the fact that I still hate arrays, so I am just biased to want to use objects! 😉

Objects tend to use the same amount of memory as arrays do but when it comes to reading them and cycling them you tend to get a little speed boost when handling large data sets.

…And I still hate arrays 🙂

So anyway’s, how do we transfer that above array to object form?

Easy, just typecast the little fella!

$myVideoObject = (object)$videoIncoming;

echo $myVideoObject->name;

Great, simple huh?

However here’s the caveat, it doesn’t work with multi dimensional arrays (sigh sigh stab stab) 😐

However with that stdClass and a little recursion you can certainly overcome this issue…

Lets take that array above and add an array within it to make it multidimensional:

$videoIncoming = array(

‘name’ => “my special day”,

‘genre’ => “romantic”,

‘chapters’ => array(‘chapter1’ => array(‘name’ => “the proposal”), ‘chapter2’ => array(‘name’ => “paris trip 2012”)),

‘director’ => “Karl Kloppenborg”

);

Okay we have a good multidimensional array now!

Lets use that recursion to convert this to an array:

function objectMaker($a) {

//check if its an array

if(!is_array($a))

{

//if it isn’t lets return it

return $a;

}

//declare object class

$object = new stdClass();

if (is_array($a) && count($a) > 0)

{
//okay array has data, lets cycle it

foreach ($a as $k => $v)

{

//if the v value is an array lets dive dive dive

$k = strtolower(trim($k));

if (!empty($k))

{

$object->$k = ObjectMaker($v);

}

}

return $object;

}

else

{

return false;

}

}

So all you have to do is use this function to convert your array and you’ll be able to now go:

$myVideoObject = objectMaker($videoIncoming);

echo $myVideoObject->chapters->chapter1->name;

Enjoy 😉

edit: wow that code turned out horrible… we need a better blog :S

Thanks,

Karl.
 


 


  • Matt

    Thank you for the tip!!

  • Mark Tees

    You know $myVideoObject->chapters->chapter1->name; looks very similar to a hash reference in Perl.

    Nice post 🙂

  • @Mark Tees, @Matt,

    If you’d like anything posted about PHP / C++ etc coding in general, comment and let me know.. Would love some content to write about!

    Thanks,
    Karl.

  • wx

    I ended up here because i was searching for a way to make arrays use less memory, especially the bloated multidimentional ones, and instead found some rant post about arrays that highly discourages their use but doesn’t actually solve anything, nicely done.

  • Loved the array-to-object trick, didn’t know you could do that with a cast!

    I suspect that arrays perform faster in general terms than objects, but don’t know of any hard testing for that fact — and of course performance isn’t critical to most code in real life situations. I always prefer objects if I can!