Storing one variable characters into an array

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Storing one variable characters into an array

Post 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";
		}
	}
}
	
?>
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: Storing one variable characters into an array

Post 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];     	
}
?>
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: Storing one variable characters into an array

Post 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];     	
}
?>
Post Reply