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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:50:05+00:00 2026-06-08T08:50:05+00:00

Possible Duplicate: PHP Pass by reference in foreach Why does the value change for

  • 0

Possible Duplicate:
PHP Pass by reference in foreach

Why does the value change for both items in array? I’m just trying to change the value of the key that is equal to $testitem.

Desired result of following code:
item:5 Quantity:12
item:6 Quantity:2

The current result of the following code is:
item:5 Quantity:12
item:6 Quantity:12

<?php
            $items = array(
                '5' => '4',
                '6' => '2',
            );

            $testitem = '5';
            $testvalue = '8';

            foreach($items as $key => &$value)
            {   
                if ($key == $testitem)
                {
                    $value = $value + $testvalue;   
                }
            }

            foreach($items as $key => $value)
            {                       
                print 'item:'.$key.' Quantity:'.$value.'<br/>';
            }
?>
  • 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-08T08:50:07+00:00Added an answer on June 8, 2026 at 8:50 am

    The problem comes when you attempted to pass the $value variable as a reference. You will be able to achieve your desired result by modifying your foreach loop to look like this –

    foreach($items as $key => $value){   
      if ($key == $testitem){
        $items[$key] = $value + $testvalue;   
      }
    }
    

    Simply use the current $key or the value of $testitem for that matter, as a reference to your $items array – and modify the contents like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP get both array value and array key I am using Codeigniters'
Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: PHP function with unlimited number of parameters How to pass array as
Possible Duplicate: How to pass an array via $_GET in php? So I have
Possible Duplicate: Can't pass mysqli connection in session in php I'm trying to store
Possible Duplicate: How to pass value from javascript to php file would something like
Possible Duplicate: PHP 5.4 Call-time pass-by-reference - Easy fix available? I'm currently modifying someone's
Possible Duplicate: Pass a PHP string to a Javascript variable (including escaping newlines) Hy,
Possible Duplicate: php calculate float The accuracy of PHP float calculate Why does PHP

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.