0

I have an array and I am trying to loop over it with foreach but I am getting the

Invalid argument supplied for foreach() on the inner loop.

I am not sure what I have incorrect. I have tried with using $key

Here is the array:

array(3) { ["company_id"]=> string(3) "159" 
    [0]=> array(9) { 
        ["company_id"]=> string(3) "159" 
        ["employee_id"]=> string(3) "544" 
        ["pay_week1"]=> string(1) "0" } 
    [1]=> array(9) { 
        ["company_id"]=> string(3) "159" 
        ["employee_id"]=> string(3) "545"  } } 

My loop:

foreach ($this->request->data['MonthlyReturn'] as $key=>$m) // Cycle through each record

{
    foreach ($key as $empl)

    {
        $employee = $empl['employee_id'];
        print '<pre>';
        print_r ($employee);
        exit;
    }
3
  • You foreach the key of an array value. That is not going to work. Foreach the value ($m) Commented Mar 8, 2013 at 10:00
  • I get the same result with $m Commented Mar 8, 2013 at 10:06
  • @KeithPower : Check my answer I explained it. Commented Mar 8, 2013 at 10:07

3 Answers 3

2
  1. In inner foreach you are looping $key Change it to $m

  2. Also your first value of the arrays is not a array ie ["company_id"]=> string(3) "159" is a string (159) so it will give error. so before inner foreach check for array also if(is_array($m)){.

Here is the code :

         foreach ($this->request->data['MonthlyReturn'] as $key=>$m){
              if(is_array($m)){
                  echo $m['employee_id'];
              }
              else{
                   echo $m;
              }
         } 
Sign up to request clarification or add additional context in comments.

6 Comments

I like how you copied his horrible indentation
I am using cakephp so it is setting ["company_id"]=> string(3) if it is a string can I still get the values out?
Yes.. check my answer ... else condition of is_array
all I get each time is $m being echoed out. I have no control over the array is outputted, is there a way to still get the employee_id?
@KeithPower : If you want only employee_id, you dont want the inner loop, see the edited answer
|
1

change this

foreach ($key as $empl)  // $key is array key

to

foreach ($m as $empl)   // $m is your array value

2 Comments

I get the same result I am afraid
@KeithPower check prashant's answer.
0

Your array is not going to work on that code. It expects a multidementional array but it isn't completely There is a string value on the first depth

array(3) {
        ["company_id"]=> string(3) "159" <--- problem
        [0]=> array(9) { 
            ["company_id"]=> string(3) "159" 
            ["employee_id"]=> string(3) "544" 
            ["pay_week1"]=> string(1) "0" 
        } 
        [1]=> array(9) { 
            ["company_id"]=> string(3) "159" 
            ["employee_id"]=> string(3) "545"  
        } 
    } 

It tries to loop through a string value, which is generating an error

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.