0

I use multidimensional arrays to store product attributes (well Virtuemart does, to be precise). When I tried to echo the sub-arrays value, if the sub-array did not exist PHP threw:

Fatal error: Cannot use string offset as an array

To get around this, I attempted to create a function to check on every array level if it is an actual array, and if it is empty (when trying on the whole thing at once such as: is_array($array['level1']['level2']['level3']), I got the same error if level1 or level2 are not actual arrays).

This is the function ($array contains the array to check, $array_levels is an array containing the names of the sub-arrays, in the order they should apper):

function check_md_array($array,$array_levels){
    if(is_array($array)){
        $dimension = null;  //This will store the dimensions string

        foreach($array_levels as $level){
            $dimension .= "['" . $level . "']"; //Add the current dimension to the dimensions string

            if(!is_array($array/* THE CONTENT OF $dimension SHOULD BE INSERTED HERE*/)){
                return false;
            }
        }           
        return true;
    }
}

How can I take the string contained in $dimensions, and insert it into the code, to be part of the statement?

Dean
  • 7,814
  • 8
  • 30
  • 31

1 Answers1

3

Short of doing an eval, I don't think you can do it.

if(eval("!is_array($array".$dimension.")"))
    return false

However, this is another way to do it

function check_md_array($array,$array_levels){
    if(!is_array($array))
         return false;

    foreach($array_levels as $level){
        if(!isset($array[$level]) || !is_array($array[$level]))
            return false;
        $array = $array[$level];
    }

    return true;
}
Damp
  • 3,328
  • 20
  • 19
  • 1
    Better use a reference instead of copying the array. – Gumbo Feb 16 '11 at 15:43
  • @Dean: You need to use a separate variable for the reference and not `$array` itself. – Gumbo Feb 16 '11 at 16:40
  • @Gumbo: I see. If I am using a reference to the same array, that means if I'm iterating over it, I am modifying the array pointer. Or I misunderstood you, please explain it a bit more. – Dean Feb 16 '11 at 22:05
  • @Damp: Sincerest apologies, I made a mistake. It works perfectly fine. +1 and answer accepted. – Dean Feb 16 '11 at 22:07
  • @dean Thx! Also, regarding the pointer, that's exactly right. – Damp Feb 17 '11 at 01:55
  • @Dean: You would just use the reference to walk though the array. But as it’s only a reference, you’re not changing/copying parts of the array as it’s only the reference that changes. So instead of `$array` you would use a reference on `$array` like `$ref = &$array` and then use that reference `$ref` instead of the original `$array` (i.e. `$ref = &$ref[$level]`, etc.). – Gumbo Feb 17 '11 at 09:16
  • @Gumbo: First, if the function catches the passed array into a variable it is creates a copy of the original array. Any modifications that are maid to the array in the function (iterating modifies the pointer) affect the copy only. If I use a reference, that means the reference is actually pointing to the same place in memory that holds the original array, any modification to the reference will modify the original array. In my case I want to perform a test on the array WITHOUT MODIFYING it. – Dean Feb 17 '11 at 09:38
  • @Gumbo: Check out this question: http://stackoverflow.com/questions/2030906/are-arrays-in-php-passed-by-value-or-by-reference – Dean Feb 17 '11 at 09:41
  • @Dean: Using a reference on the array doesn’t alter it in any way. But as the parameter is passed by value and `foreach` works on an internal copy of the array, it doesn’t matter whether you’re using just a reference to walk the array or copying the parts over and over as you it doesn’t change the original array anyway. – Gumbo Feb 17 '11 at 09:45
  • From php.net:"Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it. " – Dean Feb 17 '11 at 10:15
  • @Dean: That array pointer refers to the internal array pointer that PHP maintains for every array. But this only affects `current`, `key` and other functions that change that internal array pointer: `$arr = array(0,1,2); end($arr); var_dump(key($arr)); // int(2) foreach ($arr as $val) {} var_dump(key($arr)); // NULL and not int(2)` – Gumbo Feb 17 '11 at 16:18