cpanon Posted April 18, 2007 Share Posted April 18, 2007 Hello I am trying to loop through the $_POST, find a key and change its value in $_POST. Hoping to later see the changed value elsewhere. It is not changing with the snippet below. I believe I read something about having to refernece it first. Can someone help? while(list($key, $value) = each($_POST)){ if ($key=="number") { $value =$value*100;} } tia Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 18, 2007 Share Posted April 18, 2007 Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array. As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value. Using each(), I think you would have to do: if ($key=="number") $_POST[$key] =$value*100; Quote Link to comment Share on other sites More sharing options...
trq Posted April 18, 2007 Share Posted April 18, 2007 <?php foreach($_POST as $k => $v) { if ($k == 10) { $_POST[$k] = $v*100; } } ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 just use foreach: <?php foreach($_POST as $key => $val){ if($key == 'your_num'){ $val *= 100; } } ?> EDIT: AH thorpe you beat me to it again! =P Quote Link to comment Share on other sites More sharing options...
trq Posted April 18, 2007 Share Posted April 18, 2007 AH thorpe you beat me to it again! =P Yeah, plus, your code has no effect on the $_POST array. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 so you're saying the alteration i made in the foreach loop only applies for the duration of the loop? this would be effective? <?php foreach($_POST as $key => $val){ if($key == 'your_num'){ $_POST[$key] *= 100; } } ?> Quote Link to comment Share on other sites More sharing options...
cpanon Posted April 18, 2007 Author Share Posted April 18, 2007 Hello Sorry for my obtuseness, however if I have ...?text=text&number=1 in the $_POST array, why doesn't this work: foreach($_POST as $k => $v){ if ($k=='number'){ $_POST[$k] = $v*100; } } beginning minds want to know, grin. <?php foreach($_POST as $k => $v) { if ($k == 10) { $_POST[$k] = $v*100; } } ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 18, 2007 Share Posted April 18, 2007 looks like a GET Array! if I have ...?text=text&number=1 in the $_POST array, why doesn't thorpe code will work fine Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 those variables aren't stored in the $_POST array, they're stored in the $_GET array. although, i've never used $_GET in a foreach loop. i've always called on the variables individually like $_GET['text'] and $_GET['number']. you can try it in the foreach and see what happens. Quote Link to comment Share on other sites More sharing options...
cpanon Posted April 18, 2007 Author Share Posted April 18, 2007 Hi Sorry I showed it as a get array, but I was thinking like an Action/servlet. The variables are in a post, that comes from a simple html form with action post. I still have the problem, if you excuse the syntax of description. Is it something that I am doing wrong with searching for the key as $k=='number' ? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 you're going to have to post more code than that for us to know if your code is wrong... Quote Link to comment Share on other sites More sharing options...
cpanon Posted April 18, 2007 Author Share Posted April 18, 2007 Hi All Sorry it was a typo on my part. Yes, of course, it works. Thank you. BTW, I am getting the response from a post sent by the php script into a array. Is there a way to just pull out the key/value pairs from everything else in the response? I have seen/extrapolate to syntax like this: $response_vars = (explode '&', $_response) $pairvalue = (explode '=', $_response_vars) foreach ($pairvalue as $k => $v){ $data .= $key . '=' . urlencode($value) . '&'; } Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 if $_response is your array, you'd do it like this: <?php foreach($_response as $key => $val){ echo "key: {$key} | val: {$val}<br />\n"; } ?> Quote Link to comment Share on other sites More sharing options...
cpanon Posted April 18, 2007 Author Share Posted April 18, 2007 Hello Thank you. However I want to build up the parameter string of key/value pairs so when I set curl_setopt($curl_handle, CURLOPT_URL, "https://mydomain.com/context/myAction.do?"); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data); $response_2nd = curl_exec ($curl_handle) or die ("There has been an error connecting"); it will execute the well-formed full url and post it. can you explain some of that syntax? tia. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 Hello Thank you. However I want to build up the parameter string of key/value pairs so when I set curl_setopt($curl_handle, CURLOPT_URL, "https://mydomain.com/context/myAction.do?"); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data); $response_2nd = curl_exec ($curl_handle) or die ("There has been an error connecting"); it will execute the well-formed full url and post it. can you explain some of that syntax? tia. click 'Topic Solved' on this thread and start a new thread, please. Quote Link to comment 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.