subhomoy Posted March 12, 2014 Share Posted March 12, 2014 hello everybody I have an multidimentional array and i want to update it programitically as per required.. The code is sown below... $array = Array ( [10/3/14] => Array ( [UK] => 1 [US] => 5 ) [11/3/14] => Array ( [UK] => 5 [US] => 10 ) [12/3/14] => Array ( [UK] => 15 [US] => 20 ) [13/3/14] => Array ( [UK] => 25 [US] => 30 ) } I want to check the server date with the dates present in the array as the [key] and if it matches, i want to change the values of that [key]... ----------------------------------------------------------------------------------------------------------- example let date = 10/3/14 if it matches in the array, then only i want to change the value of US to 100... ----------------------------------------------------------------------------------------------------------- It should be done dynamically... Any help will be greatly appreciated... Thank u in advance... Quote Link to comment https://forums.phpfreaks.com/topic/286904-use-of-multidimentional-array/ Share on other sites More sharing options...
Rifts Posted March 12, 2014 Share Posted March 12, 2014 if(in_array($newdate,$array)){ //found date, change value $array[$newdate]['US'] = 100; } else { //dont chanage not in array } Quote Link to comment https://forums.phpfreaks.com/topic/286904-use-of-multidimentional-array/#findComment-1472277 Share on other sites More sharing options...
kicken Posted March 12, 2014 Share Posted March 12, 2014 in_array checks the values, not the keys. To check the key you can use either array_key_exists or isset. $date='10/3/14'; if (isset($array[$date])){ $array[$date]['US'] = 100; } Quote Link to comment https://forums.phpfreaks.com/topic/286904-use-of-multidimentional-array/#findComment-1472316 Share on other sites More sharing options...
Solution Psycho Posted March 12, 2014 Solution Share Posted March 12, 2014 (edited) Using isset() in this scenario is probably fine, but it can cause problems when using it in some scenarios to see if a specific array key exists. The manual for array_key_exists() even includes a specific statement about the two isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does. I actually got tripped up by this before. Although isset() would work in this specific scenario, in the interest of good programming practices, I think array_key_exists() would be appropriate. Building upon Kicken's solution, you can build a function to do this: function updateArray(&$sourceArray, $date, $country, $value) { //Check if date exists in source array if(!array_key_exists($date, $sourceArray)) { //Date not found return false; } //Update value $sourceArray[$date][$country] = $value; return true; } //Usage updateArray($array, '10/3/14', 'US', 100); Edited March 12, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/286904-use-of-multidimentional-array/#findComment-1472318 Share on other sites More sharing options...
subhomoy Posted March 12, 2014 Author Share Posted March 12, 2014 Thanks @ Psycho... It works fine.... Thanks for da help... Quote Link to comment https://forums.phpfreaks.com/topic/286904-use-of-multidimentional-array/#findComment-1472336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.