tommytx Posted September 27, 2017 Share Posted September 27, 2017 I have the following code... Array ( [fields] => City|Price [values] => Las Vegas|$600,000 [template] => similar_listings.php [orderby] => Price [orderdir] => DESC [sort_type] => LOCAL [count] => 3 [masterdb] => 1 [get] => Array ( [sorteddir] => DESC ) ) I want to change auto remove the $ sign on the flyI have tried the following.. since the array is named $param i tried $param[1][values] = str_replace('$', "', $param[1][values]); But it did not work...Then i tried $cnt=0 foreach($param as $x) {if ($cnt==1) $x = "Las Vegas|600,000)$cnt++;} and it failed also.Thanks for any suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/ Share on other sites More sharing options...
cyberRobot Posted September 27, 2017 Share Posted September 27, 2017 Are you getting errors? If so, what are the exact messages? For the first attempt, try changing it to $param[1]['values'] = str_replace('$', '', $param[1]['values']); Note that I added quotes around the "values" array key. I also changed the second parameter for str_replace(). For some reason, the code posted had a double quote followed by a single quote. For the second attempt, you can get the array key in the foreach loop. foreach($param as $key => $x) { Then you need to assign the modified value back to the original array...assuming you want to use $param later in the script. foreach($param as $key => $x) { if ($key==1) $param[$key]['values'] = "Las Vegas|600,000"; } Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/#findComment-1552000 Share on other sites More sharing options...
Solution benanamen Posted September 27, 2017 Solution Share Posted September 27, 2017 Simple enough... <?php $param = [ "fields" => "City|Price", "values" => "Las Vegas|$600,000", "template" => "similar_listings.php", "orderby" => "Price", "orderdir" => "DESC", "sort_type" => "LOCAL", "count" => 3, "masterdb" => 1, "get" => [ "sorteddir" => "DESC" ] ]; echo $param['values'] = str_replace('$', '', $param['values']); Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/#findComment-1552001 Share on other sites More sharing options...
tommytx Posted September 27, 2017 Author Share Posted September 27, 2017 I don't get any errors.. but when i do it the way you showed.. its seems to work but added it to the end of my array while i need it to repace the existing one.. as you can see it still there in orig form but added at bottom also.. This one did not actually work... foreach($param as $key => $x) { if ($key==1) $param[$key]['values'] = "Las Vegas|600,000";}but this one did $params[1]['values'] = "Las Vegas|600,000";but as you can see below it added it to the bottom vice replacing it..Thank you so much for your help i already understand this much better. Array ( [fields] => City|Price [values] => Las Vegas|$600,000 [template] => similar_listings.php [orderby] => Price [orderdir] => DESC [sort_type] => LOCAL [count] => 3 [masterdb] => 1 [get] => Array ( [sorteddir] => DESC ) [1] => Array ( [values] => Las Vegas|600,000 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/#findComment-1552003 Share on other sites More sharing options...
tommytx Posted September 28, 2017 Author Share Posted September 28, 2017 (edited) God bless you both.. i have learned sooooo much on this.. but the part that finally worked for me is removing the [1] like this $param[1]['values']); $param['values'] Should it have worked also with athe [1] looks like it should .. wish i could give yu both a star for such great help... but thank you sooo much.. This line made it all work for me... echo $param['values'] = str_replace(', '', $param['values']); Edited September 28, 2017 by tommytx Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/#findComment-1552009 Share on other sites More sharing options...
Sepodati Posted September 28, 2017 Share Posted September 28, 2017 No, it shouldn't have. That's why it didn't. There is no $param[1]. You can see how you created that new nested array in your last output. Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/#findComment-1552012 Share on other sites More sharing options...
cyberRobot Posted September 28, 2017 Share Posted September 28, 2017 Should it have worked also with athe [1] looks like it should... Since you are using an associative array, you would not. However, you would use it for multidimensional associative arrays like the following: $param = array( array( "fields" => "City|Price", "values" => "New York City|$800,000", "template" => "similar_listings.php", "orderby" => "Price", "orderdir" => "DESC", "sort_type" => "LOCAL", "count" => 3, "masterdb" => 1, "get" => array( "sorteddir" => "DESC" ) ), array( "fields" => "City|Price", "values" => "Las Vegas|$600,000", "template" => "similar_listings.php", "orderby" => "Price", "orderdir" => "DESC", "sort_type" => "LOCAL", "count" => 3, "masterdb" => 1, "get" => array( "sorteddir" => "DESC" ) ) ); Sorry for any confusion. I didn't pay enough attention to the array structure you provided. Quote Link to comment https://forums.phpfreaks.com/topic/305133-how-to-modify-one-element-of-an-array-on-the-fly/#findComment-1552035 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.