Jump to content

array() out of order...


ted_chou12

Recommended Posts

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.