1

Let's assume that I have an array like following:

$settings = array(
    "age" => "25",
    "data" => array(
            "name" => "John Dewey",
            "zip_code" => "00000"
        )
);

Here's my input:

$target_directory = "data.name";    // targets $settings["data"]["name"]
$new_value = "Micheal";    // I want to change 
                           // $settings["data"]["name"] with this value

I want something similar to following:

$new_array = dont_know_what_to_do($target_directory, $new_value, $settings);

A print_r($new_array) should return following:

Array
(
    [age] => 25
    [data] => Array
        (
            [name] => Micheal,
            "zip_code" => "00000"
        )

)

The change should be totally dynamic, meaning that data.zip_code = "98985" should also change only the zip code value from 00000 to 98985, and so on...

LF00
  • 27,015
  • 29
  • 156
  • 295
Arif Billah
  • 157
  • 2
  • 11
  • Where's the code you have tried? – aldrin27 Jan 20 '17 at 04:32
  • I tried to explode(".", $target_directory); but I am stuck on what to do next? – Arif Billah Jan 20 '17 at 04:36
  • Only the in the data keys are updated? – aldrin27 Jan 20 '17 at 04:40
  • Try my answer. If it works – aldrin27 Jan 20 '17 at 04:43
  • You mean, "Only the data keys are updated?"? If so... Then yes. But if I go another tree down, let's say data.name.first_name = $new_value. then it should alter only ["data"]["name"]["first_name"] = $new_value; – Arif Billah Jan 20 '17 at 04:43
  • @ArifBillah I've seen you profile, why you accept none answer of any of your qustion. – LF00 Jan 20 '17 at 05:15
  • @Kris Roofe I didn't know about that tick mark other than the upvote and downvote buttons. I always used to click on the upvote and it would say: Once you earn enough reputation, your upvote will be shown publicly. Now that I know of that tick mark. I am going to accept answers from previous questions. – Arif Billah Jan 20 '17 at 05:19

3 Answers3

1

Here is the dynamic set funciton, you can use it set any depth. Demo here for you question.

function set($settings, $target_directory, $new_value)
{
  $array = explode('.', $target_directory);
  $ref = &$settings;
  while($v = current($array))
  {
    $ref = &$ref[$v];
    next($array);
  }
  $ref = $new_value;
  return $settings;
}
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Oh! that & sign I have never know of... did it all. Thank you so very much. Learnt something new yet important. – Arif Billah Jan 20 '17 at 05:28
  • @ArifBillah for php & operator refet to http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – LF00 Jan 20 '17 at 05:33
  • That's a whole lot of information. Will have to study for quite a long. Thank you again for that. – Arif Billah Jan 20 '17 at 05:38
0
$settings = array(
    "age" => "25",
    "data" => array(
            "name" => "John Dewey",
            "zip_code" => "00000"
        )
);
$new_value = "Micheal"; 
$settings["data"]["name"] = $new_value;
print_r($settings);
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0
//In your main function
public function something() {
    $settings = array(
     "age" => "25",
     "data" => array(
        "name" => "John Dewey",
        "zip_code" => "00000"
      )
    );

   $target = 'data.name';
   $input = 'Other Name'

  $new_arr = dont_know_what_to_do($target_dir, $input);

  print_r($new_arr);        
}


//Create new function
public function dont_know_what_to_do($settings, $target, $input) {
  $key = explode('.', $target)
  return $settings['data'][$key[1]] = $input;
}
aldrin27
  • 3,407
  • 3
  • 29
  • 43
  • Will work, but if I go another tree down, let's say data.name.first_name = $new_value. then it should alter only ["data"]["name"]["first_name"] = $new_value; – Arif Billah Jan 20 '17 at 04:49