Page 1 of 1

Storing one variable characters into an array

Posted: Fri Jan 28, 2011 12:37 pm
by shehan31
Hello everyone;
I have two problems.
1)I have a system wich has a form. when the user enter a message from the web interface it will be saved with a variable. Ex: $Customername = $_POST['Customer_name']; . What i want to know is a way to save this into an array of charcters by using Ex: $Customername. then it will be compared with another set of characters.
2) this is very much related to the above question.I am trying to compare two arrays. it works but it dose not echo out the order according to the variable $array1;
seems to me it is brinting according to the alphebatical order. I want to stop that and want to print the result according to the $array1.
the output is : BBCD. but it has to be BDCB.

Code: Select all

<?php
$array1=array('A','C','B','A');
$array = array('A'=>'B','B'=>'C','C'=>'D');

foreach($array as $key=>$value){
	for($i=0;$i<count($array1);$i++){
		if($array1[$i]==$key){
	      
			echo"$value";
		}
	}
}
	
?>

Re: Storing one variable characters into an array

Posted: Fri Jan 28, 2011 6:49 pm
by MindOverBody
Ok, fisrt of all, to split word into array, each letter to be one array element you can use str_split( $string ) function.
If i understanded you correctly, this should be your solution:

Code: Select all

<?php
$array1 = array('A','C','B','A');
$array = array('A'=>'B','B'=>'C','C'=>'D');

foreach($array1 as $arrayOneElement){
     echo $array[$arrayOneElement];     	
}
?>

Re: Storing one variable characters into an array

Posted: Fri Jan 28, 2011 11:52 pm
by shehan31
looks like something is missing?????
MindOverBody wrote:Ok, fisrt of all, to split word into array, each letter to be one array element you can use str_split( $string ) function.
If i understanded you correctly, this should be your solution:

Code: Select all

<?php
$array1 = array('A','C','B','A');
$array = array('A'=>'B','B'=>'C','C'=>'D');

foreach($array1 as $arrayOneElement){
     echo $array[$arrayOneElement];     	
}
?>