0

After searching for an answer in this forum I found only related questions but under other context that would not apply to my case. Here's my problem:

I have a 3-dim array defined in a function like this:

$m_Array[h][$family][$iterator]

the values for

$family range from 6-10;

$iterator from 0-3 but has duplicates (0,1,2,3,1),

and the  $m_Array results in values (25,26,30,31,33).

I am unable to echo the result using those indices to get these results once returned from the function. NOTE: I was able to echo when I had 2-dim $m_Array[h][$iterator] but could not use it because the last value for the iterator would replace the second in the array. Since I was able to echo the 2-dim, this is not a question on getting the return from the function or iterate over the indices. Thanks.

2
  • what do you mean by echo? you just want to just show the array in the screen? why not just do var_dump()? Commented Nov 24, 2015 at 5:36
  • loop your foeach loop you will get d answer...Eg : foreach within a foreach Commented Nov 24, 2015 at 6:05

3 Answers 3

1

Use print_r($arrayName) to print an array. You cannot echo an Array or Object

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

Comments

0

as others mentioned, you can use var_dump() or print_r(). If you need to access each item then you're going to need nested loops.

foreach($m_Array as $i => $h)
{
     //echo $i, $key for h
     foreach($h as $j => $family)
     {
          //echo $j, key for family
          foreach($family as $k => $iterator)
          {
                echo $iterator;
          }
     }
}

Comments

0

try this:

$keys = array_keys($h);
for($i = 0; $i < count($h); $i++) {
    echo $keys[$i] . "{<br>";
    foreach($h[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
    echo "}<br>";
}

It prints all values and keys

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.