0

I am having multiple indexed array,here the index changing dynamically.Actually the index of the arrays are id's of group.How can i pass these array in to foreach loop

 Array
(
    [0] => Array
        (
            [462] => Array
                (
                    [0] => Array
                        (
                            [group_name] => First
                            [invoice_no] => 

                        )

                    [1] => Array
                        (
                            [invoice_no] => 
                            [invoice_no] => 2Q

                        )

                )

            [525] => Array
                (
                    [0] => Array
                        (
                            [group_name] => Second
                            [invoice_no] => 

                        )

                    [1] => Array
                        (
                            [group_name] => 
                            [invoice_no] => 3QW

                        )

                )


        )

)

This is my array structure,Please help me to print results using foreach loop in php with out changing array structure I am trying this code

foreach ($Sale_list_array as $key => $value) {
    echo ($key);

    echo $value[$key][group_name];
}

But,this won't fix my problem. I am hoping output like this in table structure

<table>
<thead>
<tr>
<td>Group</td>
<td>Invoice</td>
</tr>
</thead>
<tbody>
<tr><td>First</td><td></td></tr>
<tr><td></td><td>2Q</td></tr>
<tr><td>Second</td><td></td></tr>
<tr><td></td><td>3QW</td></tr>
</tbody>
</table>
11
  • can you show your expected output? Commented Oct 9, 2017 at 6:22
  • So what is the exact problem? Where's the code that you have tried? Commented Oct 9, 2017 at 6:22
  • 1
    Also which array are you hoping to loop over? Commented Oct 9, 2017 at 6:23
  • $arrayVariable[$keyVariable] = $valueVariable;this will create your array with dynamic key and value Commented Oct 9, 2017 at 6:27
  • so you want $key as key of new array and $value[$key][group_name] as value? Commented Oct 9, 2017 at 6:29

2 Answers 2

1

Try this code below, $wholeArr is variable which holds your full array value

     foreach( $wholeArr as $group ){
        foreach( $group as $groupKey => $groupValues ){
           foreach( $groupValues as $groupVal ){
             echo $groupVal["group_name"]."<br/>";
           }
         }
       }
Sign up to request clarification or add additional context in comments.

1 Comment

@ubm No mention, happy to hear that your problem is solved,
1

try this

$newArray = array();
foreach ($Sale_list_array as $key => $value) {
   $newArray[$key] = $value[$key][group_name];
}
print_r($newArray)

4 Comments

you are trying to access index 0 which is not exist show me your code what you tried
echo $Sale_list_array[0][462][0]['group_name'],then i got first
this will only return first group_name as you are using static key's. You need to use foreach for dynamically travel through array
oh.. i solved the problem using double foreach loop,ok thanks

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.