2

I have the following array named $ingredient_difference in PHP (example output below):

Array (
  [total_remaining_ingredients] => Array (
    [0] => 2 [1] => 3 [2] => 10
  )
  [idrecipe] => Array (
    [0] => 8 [1] => 10 [2] => 9
  )
  [value] => Array ( [0] => 1 [1] => 1 [2] => 1 )
) 

I'm trying to extract at least the values of idrecipe using 'foreach', but I'm getting undefined index with the following code:

foreach($ingredient_difference as $recipe_output)
{
    echo $recipe_output['idrecipe']."<br />";
}

I know the above isn't exactly the way to do it, but this also was not working (undefined index error for 'idrecipe', 'value' and 'total_remaining_ingredients'):

foreach($ingredient_difference as $c => $rowkey)
{
    $sorted_idrecipe[] = $rowkey['idrecipe'];
    $sorted_value[] = $rowkey['value'];
    $sorted_remaining_ingredients[] = $rowkey['total_remaining_ingredients']; 
}

What am I missing in my foreach syntax? Or is there a better way?

This foreach construct is also giving undefined index errors:

foreach($ingredient_difference as $rowkey => $index_value)
{
    $id_value[$key] = $index_value['idrecipe'];
    $value_value[$key] = $index_value['value'];
    $tri_value[$key] = $index_value['total_remaining_ingredients'];
}

Answer thanks to ComFreek:

$result_ingredient_difference = array();
$count_id = count($ingredient_difference['idrecipe']);

for ($i=0; $i<$count_id; $i++)
{
  $result_ingredient_difference[] = array(
  'tri' => $ingredient_difference['total_remaining_ingredients'][$i],
  'idrecipe' => $ingredient_difference['idrecipe'][$i],
  'value' => $ingredient_difference['value'][$i]
  );
}
//rearranged array of $result_ingredient_difference able to call proper indexing with the below
foreach($result_ingredient_difference as $rowkey => $index_value) 
{ 
  $id_value[$key] = $index_value['idrecipe']; 
  $value_value[$key] = $index_value['value']; 
  $tri_value[$key] = $index_value['tri'];
} 

2 Answers 2

4

With your first foreach() loop, you iterate over the main array and not over the values of the subarray idrecipe!

foreach($ingredient_difference['idrecipe'] as $value)
{
  echo $value;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. That definitely gets the 'idrecipe' value. How would I go about getting the three different indices (i.e., 'idrecipe', 'value', and 'total_remaining_ingredients'). I thought using something like foreach($ingredient_difference as $key => $value) does the trick, but I haven't had any success.
What do you want to do exactly? It's not neccessary if you have only three entries. Your foreach() is right but I think you access the specific values in the wrong way. Show us the code, please.
Instead of running three different foreach loops for the 'idrecipe', 'value', and 'total_remaining_ingredients' indices, I wondered if there was a way to construct only one foreach loop to get the values for all three. See amended code above, which is giving me undefined index errors.
Yes - I think that will do the trick. I'll post my results after testing. Thank you.
2

foreach constructs a loop. In your code

foreach($ingredient_difference as $recipe_output) {
echo $recipe_output['idrecipe']."<br />"; }

in the first loop run: $recipe_output is $ingredient_difference[total_remaining_ingredients] in the second loop run: $recipe_output is $ingredient_difference[idrecipe] in the third loop run: $recipe_output is $ingredient_difference[value]

because there is no

$ingredient_difference['total_remaining_ingredients']['idrecipe']
$ingredient_difference['idrecipe']['idrecipe']
$ingredient_difference['value']['idrecipe']

you get the error.

to see how the foreach loop works use the examples on http://php.net/manual/de/control-structures.foreach.php

i Expect what you want to do is:

foreach($ingredient_difference['idrecipe'] as $value_of_recipe)
{
    echo $value_of_recipe."<br />";
}

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.