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

  • SEARCH
  • Home
  • 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 6696385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:20:07+00:00 2026-05-26T06:20:07+00:00

How does a reference variable work in this case? Modifying it works when it’s

  • 0

How does a reference variable work in this case? Modifying it works when it’s passed one way. I couldn’t explain it better other than showing you the code.

//this is the haystack and the array to be modified
$data = array(
    'one_1' => array('value' => ''), 
    'one_2' => array('value' => ''), 
    'one_3' => array('value' => '')
);

// index to search for
$needle = 'one_2';

// value to assign to
$value = 'awesome';

// start haystack modification
modify_arr($data, $needle, $value);

// this function forms the reference to the needle in the haystack e.g $data['one_2']
function modify_arr(&$ref, $index, $value) {
    $res = $ref;
    foreach ($ref as $key => $arr) {
        if(is_array($arr)) 
            $res[$key] = modify_arr($arr, $index, $value);
        if ($key == $index)
            write_values($ref, $key, $value);
    }
    // assign back the modified copy of $ref
    $ref = $res;
    return 
}

function write_values(&$ref, $key, $value) { 
    if (empty($ref[$key]['value']) || !$ref[$key]['value']) {
        // assign the value when value is empty
        $ref[$key]['value'] = $value;
    } else {
        // if it's not empty, increment the needle's suffix to form a new needle and assign to it instead
        // result would be: from "one_1" to "one_2" 
        $key_parts = split_key($key);
        $new_key = $key_parts[0] . '_' . ((int)$key_parts[1] + 1); // result is "one_2" 
        return modify_arr($ref, $new_key, $value);
    } 
    return;
}

It works when “one_2” is empty, but when it’s not it doesn’t…

  • 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-05-26T06:20:08+00:00Added an answer on May 26, 2026 at 6:20 am

    You should really, really, really take a look at array_walk_recursive()!

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

Sidebar

Related Questions

How does one obtain programmatically a reference to the object of which a FieldInfo
What does this mean: Next add reference to: MySql.Data actually I have downloaded mysql
What does it mean to take a variable number of arguments by reference? Does
According to the Qt documentation, QVariant::operator== does not work as one might expect if
why does this code not work for referencing a const from a class? Background:
I've installed SlimDX but the assembly reference does not come up as a choice
Does anybody have any reference material that details Cauchy-Reed algorithm? Googling for Cauchy-Reed Solomon
Does routed events in WPF store strong reference to the handler that is attached
Does anyone know of a link to a reference on the web that contains
Does String.ToLower() return the same reference (e.g. without allocating any new memory) if all

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.