How to Insert elment into array through FOR loop.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anky666
Forum Newbie
Posts: 9
Joined: Fri Mar 31, 2006 3:00 am

How to Insert elment into array through FOR loop.

Post by anky666 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 

I need to develop polish notation calculator to convert prefix expression to postfix expression. Thus, i need to imake array, and insert element through for loop. I do not knw how to do that. Please help, i tried this code, but it does npot work, because, it takes only the lastest value:

Code: Select all

while($d<2)
	{
		if($str[$s]=='1'||$str[$s]=='2'||$str[$s]=='3'||$str[$s]=='4')
		{
			$operand =array($d => $str[$s]);
			$s++;
		 }
		$d++;
	}
PLEASE HELP.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

This might be a bit simplier:

Code: Select all

for($d=0; $d<2; $d++) 
{ 
if($str[$s]=='1'||$str[$s]=='2'||$str[$s]=='3'||$str[$s]=='4') { 
$operand =array($d => $str[$s]); 
$s+1; 
} 
}
anky666
Forum Newbie
Posts: 9
Joined: Fri Mar 31, 2006 3:00 am

Post by anky666 »

thanx a lot for the speedy reply. But this is not working, as it saves only the last value. It is overwritting the value.

Like if first value is 1
second value is 2

then only 2 is saved, 1 is overwritten.
when I write

echo $operand[0];

it does not give me any value.

I need help, so that I can store both these values.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

can you post the whole code that this snip you gave us includes...
anky666
Forum Newbie
Posts: 9
Joined: Fri Mar 31, 2006 3:00 am

Post by anky666 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


PLEASE HELP

Code: Select all

$str =  $_POST['ans'];
	$len= strlen($str);
	$i=0;
	$s=0;
	$d=0;
	$k=0;
	while($i<$len)
	{
		if ($str[$i]=='+'||$str[$i]=='*'||$str[$i]=='/'||$str[$i]=='-')
		{
			
			$operator = array($k => $str[$i]);
			echo $operator[$k];
			$s++;
			$k++;
		}
		$i++;
	}
$e=$s+2;
		while($d<2)
	{
		if($str[$s]=='1'||$str[$s]=='2'||$str[$s]=='3'||$str[$s]=='4'||)
		{
			$operand =array($d => $str[$s]);
			echo $operand[$d];
			$s++;
			
		}
		$d++;
	}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply