Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:56:28+00:00 2026-06-02T09:56:28+00:00

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

  • 0

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'];
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:56:30+00:00Added an answer on June 2, 2026 at 9:56 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array as3 example: var arrayDefinitionsargsAmfPhp:Array = new Array(); arrayDefinitionsargsAmfPhp['tabela'] =
I have a question about associative arrays in php. I have following array in
I have the following array, named $usergrouppermissions: Array ( [0] => Array ( [UserGroupPermission]
I have two models named message.php and user.php In message.php, I have following method
I have the following array, $myarray[0]['first_name'] $myarray[0]['last_name'] I want to turn it into: $myarray['first_name']
I have the following 'key' array: (Array 1) Array ( [0] => first_name [1]
I have the following code function generate_pdf() { $fdf_data_strings = $this->get_hash_for_pdf(); #$fdf_data_names = array('49a'
I have following array, that I need to operate by hand on bitmaps. const
I have the following array. <cfset ItemHasUsers = arrayNew(1)> <cfloop query=qReadData> <cfset ItemHasUsers[qReadData.currentrow][ID] =
I have the following array: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] I use it for some visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.