0

EDIT: UPDATED CODE. PLEASE SEE COMMENTED LINES:

This is my problem:

I have 2 arrays:

$numbers = array(0,3,8);

$data = array('January' => array('BATH' => array('ID', 'user', 'type'), 'KITCHEN' => array('ID', 'user', 'type')), 'february' => array('BATH' => array('ID', 'user', 'type'), 'KITCHEN' => array('ID', 'user', 'type')), 'march' => array('BATH' => array('ID', 'user', 'type'), 'KITCHEN' => array('ID', 'user', 'type')));

Expected output must be:

array (size=6)
  'January' => 
    array (size=2)
      'BATH' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'KITCHEN' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'total_lunar' => int '0' // // This is the first value from first array
  'february' => 
    array (size=2)
      'BATH' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'KITCHEN' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
       'total_lunar' => int '3' // This is the second value from first array
  'march' => 
    array (size=2)
      'BATH' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'KITCHEN' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
       'total_lunar' => int '8' // This is the last value from first array

My PHP code is:

for ($i = 0; $i < count($numbers); $i++) 
{
    foreach ($data as $key => $value) 
    {
        $data[$key]['total_lunar'][$i] =  $numbers[$i];
    }
}

I don't know why, the result sets each array but not each value of first array:

array (size=6)
  'January' => 
    array (size=2)
      'BATH' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'KITCHEN' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'total_lunar' => 
          array (size=3)
             0 => int 0
             1 => int 3
             2 => int 8
  'february' => 
    array (size=2)
      'BATH' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'KITCHEN' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
       'total_lunar' => 
          array (size=3)
             0 => int 0
             1 => int 3
             2 => int 8
  'march' => 
    array (size=2)
      'BATH' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
      'KITCHEN' => 
        array (size=3)
          0 => string 'ID' (length=2)
          1 => string 'user' (length=4)
          2 => string 'type' (length=4)
       'total_lunar' => 
          array (size=3)
             0 => int 0
             1 => int 3
             2 => int 8

['total_lunar'] key should return one value as the expected output above, not all array values. Looked all over for a solution but can't manage to solve this.

7
  • 1
    The second level of your $data array is the keys 'BATH' etc. and not a numeric index. Commented May 1, 2020 at 15:09
  • $data[$key][$i] doesn’t exist to push to. Just use $data[$key][$i] = $numbers[$i]; see the Notes at php.net/manual/en/function.array-push.php Commented May 1, 2020 at 15:16
  • You still seem to be having issues with arrays. When in doubt put a print_r($data[$key]); in your code an see whats really in the array you are looking at Commented May 1, 2020 at 15:16
  • array_push($data[$key]['0'], $numbers[$i]); Commented May 1, 2020 at 15:19
  • 1
    Yes, thank you for understanding this time. Commented May 1, 2020 at 15:34

1 Answer 1

2

Keep it simple. I think this is what you're looking for?

$n=0;
foreach($data as &$d) {
    $d['total_lunar'] = $numbers[$n++];
}

The expected output you would se by using this:

echo '<pre>';
print_r($data);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

10 Comments

Updated post. please see.
So you want an total_lunar key instead of [0]?
I added it, but it returns me all values from numbers array, instead one by one as in my expected output.
Yes it's functional. Thanks a million times.
I tried myself this before but was missing & from &$d in foreach.
|

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.