2
288
   36
      123
      456
      789
29
   36
      123
      456
      789
295
   36
      123
      456
      789

288,29,295 are my first array 36,36,36 are my array of array and 123,456,789 are my another array.

I want to display

288
   36
      123
29
   36
      456
295
   36
      789

Here is my code

$nos = $_POST['nos'];
$nos2 = $_POST['nos2'];
$nos3 = $_POST['nos3'];
    foreach($nos as $nbs)
    {
        echo $nbs."<br>";   
        foreach($nos2 as $nbs2)
        {
            echo "&nbsp;&nbsp;&nbsp;".$nbs2."<br>";
            foreach($nos3 as $nbs3)
            {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nbs3."<br>";

            }
            break;
        }
    }

how can i do it using foreach
help me please.

3 Answers 3

3

Use for loop instead :

    $nos = $_POST['nos'];
    $nos2 = $_POST['nos2'];
    $nos3 = $_POST['nos3'];
    for($i = 0 ; $i < count($nos) ; $i++){
      if(isset($nos[$i])) echo $nos[$i]."<br>";
      if(isset($nos2[$i])) echo "&nbsp;&nbsp;&nbsp;".$nos2[$i]."<br>";
      if(isset($nos3[$i])) echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nos3[$i]."<br>";
    }

Or foreach like this

    $nos = $_POST['nos'];
    $nos2 = $_POST['nos2'];
    $nos3 = $_POST['nos3'];
    foreach($nos as $i => $n){
      echo $n."<br>";
      if(isset($nos2[$i])) echo "&nbsp;&nbsp;&nbsp;".$nos2[$i]."<br>";
      if(isset($nos3[$i])) echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nos3[$i]."<br>";
    }

EDIT

If you haven't numeric key or different index, you can change assignation by array_values http://php.net/manual/en/function.array-values.php

    $nos = array_values($_POST['nos']);
    $nos2 = array_values($_POST['nos2']);
    $nos3 = array_values($_POST['nos3']);

https://eval.in/671937

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

1 Comment

its work fine but what happen when key was not 0,1,2(key was different) for array? eval.in/671936
0

Try this, use counter, so it will display foreach of $nos3(last foreach loop) value as expected output

$counts = 0;
foreach($nos as $nbs)
{   
    echo $nbs."<br>";   
    foreach($nos2 as $nbs2)
    {
        echo "&nbsp;&nbsp;&nbsp;".$nbs2."<br>";
        $sub_counts = 0;
        foreach($nos3 as $nbs3)
        {
            if($counts == $sub_counts)
            {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nbs3."<br>";
            }           
            $sub_counts ++;
        }
        break;
    }
    $counts ++;
}

i hope it will be helpful.

DEMO

Comments

0

If you are just debugging the contents of your nested array and you want to look at it in a human friendly way you could try either:

print_r($my_nested_array) - see here

or

var_dump($my_nested_array) - see here

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.