ted_chou12 Posted December 7, 2008 Share Posted December 7, 2008 I have a file that processes POST forms, and these strings need to go through processes such as stripslashes() htmlentities() and other functions, this is the file that includes the form $entrycontent = array($name => 6, $email => 6, $tname => 6, $temail => 6, $imgverify => 6, $message => 6); include("texteditor.php"); list($name, $email, $tname, $temail, $imgverify, $message) = $finalcontent; The texteditor file is the file that processes the string values: //maximum number reached: 6 foreach ($entrycontent as $text => $textecondition) { //for txtfiles and email fields: if ($textecondition == 0 or $textecondition == 1 or $textecondition == 6) { //stripslashes: $text = stripslashes($text); //only for txtfiles: if ($textecondition == 0 or $textecondition == 1) { //textfile newline, separate: $text = str_replace(";seDp#",";sedp#",$text); //only for txtfiles messages: if ($textecondition == 1) { $text = str_replace(";seDpNL#",";sedpnl#",$text); $text = str_replace("\r\n", ";seDpNL#", $text);}}} //close extra tags, only for messages: if ($textecondition == 1 or $textecondition == 3) { $tags = array("[b]" => "[/b]", "[i]" => "[/i]", "[u]" => "[/u]", "[s]" => "[/s]", "[email]" => "[/email]", "[img=http://" => "]", "[url=http://" => "]" => "[/url]", "[quote]" => "[/quote]"); foreach ($tags as $stag => $etag) { while (substr_count($text, $stag) > substr_count($text, $etag)) { $text = $text.$etag;}}} //if special fields, username, first name or last name: if ($textecondition == 4 or $textecondition == 5) { $text = strtolower($text); if ($textecondition == 5) { $text = ucwords($text);}} //html special characters to normal characters, for all: $text = htmlentities($text); $finalcontent[] = $text;} There is no need to care about the lines in between just the first line, foreach() that takes the initial associative array apart and the final line $finalcontent[] that puts them back into a new array, however, the order seems to be out of predicted, they switch, and list($name, $email, $tname, $temail, $imgverify, $message) = $finalcontent; seems to be getting unexpected variables that got switched, eg. $temail became $imgverify, $imgverify became $temail..etc, any ideas why php is having such weird behaviour? Thanks, Ted. Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/ Share on other sites More sharing options...
flyhoney Posted December 7, 2008 Share Posted December 7, 2008 From http://us.php.net/list : Note: list() only works on numerical arrays and assumes the numerical indices start at 0. Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/#findComment-708704 Share on other sites More sharing options...
genericnumber1 Posted December 7, 2008 Share Posted December 7, 2008 At first glance: do you have errors enabled? "[url=http://" => "]" => "[/url]", is not valid... Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/#findComment-708707 Share on other sites More sharing options...
ted_chou12 Posted December 7, 2008 Author Share Posted December 7, 2008 Thanks, @flyhoney what does list() only works with numerical arrays mean? I checked the php manual, there are egs of strings in the array that are not pure numbers. @genericnumber1 Sorry, didnt realize that: "{u}" => "{/u}", "{s}" => "{/s}", "{email}" => "{/email}", "{img}" => "{/img}", "{url}" => "{/url}", "{quote}" => "{/quote}" I think the texteditor of this forum edited my actual codes, so the got messed up. *The actual script is correct. Ted. Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/#findComment-708712 Share on other sites More sharing options...
Mark Baker Posted December 7, 2008 Share Posted December 7, 2008 Do you mean: $entrycontent = array($name => 6, $email => 6, $tname => 6, $temail => 6, $imgverify => 6, $message => 6); or: $entrycontent = array('name' => 6, 'email' => 6, 'tname' => 6, 'temail' => 6, 'imgverify' => 6, 'message' => 6); Also, take a look at the extract() function Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/#findComment-708725 Share on other sites More sharing options...
flyhoney Posted December 7, 2008 Share Posted December 7, 2008 I think maybe you are confusing an array of strings with an associative array. This is an array of strings: <?php $array = array('apple', 'orange', 'banana'); ?> It has numerical indices. If you print_r you get this: Array ( [0] => apple [1] => orange [2] => banana ) However, if you were to do something like this: <?php $array = array('apples' => 5, 'oranges' => 3, 'bananas' => 7); ?> That array does not have numerical indices. If you do a print_r you get this: Array ( [apples] => 5 [oranges] => 3 [bananas] => 7 ) list() can only be expected to work properly if you use it with an array that has numerical indices, eg, a non-associative array. Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/#findComment-708737 Share on other sites More sharing options...
ted_chou12 Posted December 7, 2008 Author Share Posted December 7, 2008 Thanks Mark, I think those I do mean variables. For flyhoney's suggestion, i think uve got something that i was wishing to know, I found the error that when $email and $temail are equal, the associative array somehow eliminates one of them, so the ending array is weird in result, so they have been made unique in associative array, is it then possible to make them non unique??? Thanks, Link to comment https://forums.phpfreaks.com/topic/135953-array-out-of-order/#findComment-708740 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.