Array functions

Arrays (aka hashtables) are jack-of-all-trades in PHP – virtually all data storage is an array (except for objects – which internally don’t differ much – but let’s leave them alone). However, all array functions are flat – one-dimensional. What if we had some functions for non-flat array operations? One example – let’s suppose we have an array representation of a table:

<?php
$arr
= array(
array(
1,2,3),
array(
4,5,6),
array(
7,8,9),
);
?>

Now, how do I get the first row? Oh, that’s easy – $arr[0]! Now, how do I get the first column? Oops… No good way to do that. Maybe we’d have a function for that? The function, sadly, doesn’t have a good way for that either, but at least it can do it faster.

Then, of course, there’s a whole world of matrices, etc., but that’d be another story.

4 thoughts on “Array functions

  1. Well, I’m sure recursive operations can do it, so can loops, but it’s not straightforward. The thing is that now arrays have preferred direction, so to speak. And it’s completely understandable, given how the work in PHP. I am thinking what if there were some function for which this directionality is hidden – i.e. you don’t actually care how they data is stored. E.g. if you had some 2-dimensional (or more-dimensional) data and you wanted to walk it in any arbitrary axis, not just preferred axis.

  2. Yep, sure – I can pick them one by one. I want nice function though to do it in one shot – just as we have array_merge while we could merge arrays by using loops.

  3. Gah, it seems that wordpress doesn’t use htmlspecialchars()..
    That’s what I meant:

    $row = Array();
    foreach ($array as $val) {
    $row[] = $array[0];
    }

Comments are closed.