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 8231127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:22:28+00:00 2026-06-07T17:22:28+00:00

Im trying to add a key=>value to a existing array with a specific value.

  • 0

Im trying to add a key=>value to a existing array with a specific value.

Im basically looping through a associative array and i want to add a key=>value foreach array that has a specific id:

ex:

[0] => Array
    (
        [id] => 1
        [blah] => value2

    )

[1] => Array
    (
        [id] => 1
        [blah] => value2
    )

I want to do it so that while

foreach ($array as $arr) {

     while $arr['id']==$some_id {

            $array['new_key'] .=$some value
            then do a array_push
      }    
}

so $some_value is going to be associated with the specific id.

  • 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-07T17:22:31+00:00Added an answer on June 7, 2026 at 5:22 pm

    The while loop doesn’t make sense since keys are unique in an associative array. Also, are you sure you want to modify the array while you are looping through it? That may cause problems. Try this:

    $tmp = new array();
    foreach ($array as $arr) {
    
         if($array['id']==$some_id) {
                $tmp['new_key'] = $some_value;
          }    
    }
    
    
    array_merge($array,$tmp);
    

    A more efficient way is this:

    if(in_array($some_id,$array){
      $array['new_key'] = $some_value;
    }
    

    or if its a key in the array you want to match and not the value…

    if(array_key_exists($some_id,$array){
          $array['new_key'] = $some_value;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing array to which I want to add a value. I'm
I've been trying to add a value to a pre-existing dictionary key. The value
I have an array that I need to add some key/value pairs too but
I am trying to add a key value pair and having trouble adding the
I'm trying to add some additional key/value pairs to an NSMutableDictionary, using: Tag *tag1
While trying to add value into foreign key field, if same data doesn't exist
I'm trying to add a foreign key to an existing table, and was having
I'm trying to create a table that has a primary key (Department_ID) that auto
Hi I'm new to python. I am trying to add different key value pairs
I'm trying to use XPath/XSLT to add a node to an existing node that

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.