1

I have an array inside an array and I'd like to simply print out the contents of it... the problem is what I'm trying doesn't work... ie

        foreach($retval as $k=>$v){

          if (is_array($v)){

            foreach($v as $l=>$w){

            echo $l . ' = ' . $w . '<br />';

            }

        } else {

          echo $k . ' = ' . $v . '<br />';

        }

Doing that however I end up with:

id = 2

name = Speakers

form_field = hidden

display_order = 0

groups = Array

So I'm not getting a foreach in that inside array.. what am I doing wrong? It seems actually that my code believes everything return is an array when I'm pretty sure that only 'groups' is an array.


the structure of the array looks like so:

array ( 0 => array ( 'id' => 2, 'name' => 'Speakers', 'form_field' => 'hidden', 'display_order' => '0', 'groups' => array ( 0 => array ( 'bit' => '1', 'name' => 'don', 'display_order' => '4', 'subscribers' => 8, ), 1 => array ( 'bit' => '2', 'name' => 'tyler', 'display_order' => '5', 'subscribers' => 0, ), 2 => array ( 'bit' => '4', 'name' => 'stephen', 'display_order' => '6', 'subscribers' => 0, ), 3 => array ( 'bit' => '8', 'name' => 'daniel', 'display_order' => '7', 'subscribers' => 0, ), 4 => array ( 'bit' => '16', 'name' => 'william', 'display_order' => '8', 'subscribers' => 0, ), 5 => array ( 'bit' => '32', 'name' => 'bobbys', 'display_order' => '9', 'subscribers' => 0, ), ), ), )

Long story short, I'm actually just trying to search this whole thing for say the name 'bobby' and get a simple true or false on whether that value exists anywhere in there.

2
  • what does your array structure look like? you can print it with var_export($retval) Commented Nov 16, 2010 at 23:51
  • 1
    FWIW, your nested foreach() doesn't display any indenting, so it would be producing misleading output. Commented Nov 17, 2010 at 2:29

5 Answers 5

3
print_r($myArray);

PHP Manual

<?php
    $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
    print_r ($a);
?>

would give:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

Not sure if this is what you are looking for...

print_r() gives you a visual representation while var_export() returns valid, executable php code. Manual: var_export()

Sign up to request clarification or add additional context in comments.

Comments

1

You're assuming that arrays are only doubly-nested, when $retval[<something>]['groups'] is also an array. Use recursion to handle this instead of just adding levels upon levels.

3 Comments

they go deeper than 2 levels?!? this is crazy
thanks ignacio, i'm looking up recursive searching stuff now.
I've worked with arrays of seven dimensions with loops nested four deep. Took some planning, let me tell you.
1

It sounds like you need to reconsider what you are trying to do.

If you just want to see what is in your array so that you know how to get at a particular piece of data, then you're probabaly after print_r, var_export or similar as mentioned above.

print_r($retval);

If you want your php script to be able to work out on it's own whether 'bobby' is in the array, then you might have some more information that will help your search. For example, do you know the structure of your array and that you're searching for a name? If so, then you only need to check the strings that are in $array[$foo]['name']. You could adapt your code like so:

foreach($retval as $k=>$v) {
    if ($v['name'] == 'bobby') {
        echo "Bobby is at index $k";
    }
}

PHP contains lots of functions for arrays, although these tend to be for single-dimension arrays.

P.S. Please edit the structure of your array to make it easier to read.

Comments

1

If your goal is to understand if a value is in any nested array you can do this:

foreach($retval as $vec){
    if(in_array("bobby", $vec)
        do something

Comments

1

Use a RecursiveArrayIterator wrapped in RecursiveIteratorIterator. The syntax will then be:

$arr = array('this is your complicated multi-dimensional array');
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($iterator as $key => $value){
    echo '['.$key.'] => '.$value.'<br />';
}

You can even use $iterator->depth() to determine the depth of a current element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.