Page 1 of 1

replacing the new array element problem ???

Posted: Sat Jan 06, 2007 7:16 am
by PHPycho
hello frens of devnetwork...............
i am getting problem in replacing the array element................
actually i have multidimensional array which is stored in session variable.............
suppose i had

Code: Select all

$_SESSION['sess_var'] = array(array(id1,name1,price1),array(id2,name2,price2),..array(idn,namen,pricen));
i want to know
1> how to replace the array block whose id=n with the new

Code: Select all

array block array(id_new,name_new,price_new)
i would be very greatful if anybody helps me in this case...........
Thanks in advance to all of you..............genius over there........

Posted: Sat Jan 06, 2007 7:31 am
by Oren
Does the new array have to be in the same position as the old one?
Anyway: http://us2.php.net/manual/en/ref.array.php

Edit: To be more specific, you can use array_map() to do that :P

Posted: Sat Jan 06, 2007 7:39 am
by Kieran Huggins
Flattery will get you everywhere!

Code: Select all

$ra = array(array('id1','name1','price1'),array('id2','name2','price2'),array('idn','namen','pricen'));

$new_ra = array('id_new','name_new','price_new');

for($i=0;$i<count($ra);$i++){
	if($ra[$i][0]=='id1') $ra[$i]=$new_ra;
}
By the way, does your username mean you're "pychotic"? Image

Posted: Sat Jan 06, 2007 7:40 am
by PHPycho
thanks for the reply
it doesnt matter ...whether the new array is the same position or new position ..what the main thing is ...the old array should be replaced by the new one.........how s that possible ...pls help me ???

Posted: Sat Jan 06, 2007 7:46 am
by Oren
I edited my previous post, read it. Kieran Huggins's way should be fine too, one thing though...

Wrong way:

Code: Select all

for($i=0; $i < count($array); $i++)
Right way:

Code: Select all

for($i=0, $size = count($array); $i < $size; $i++)
There is no need to check the size of the array over and over again - the result is the same each time anyway :P

Posted: Sat Jan 06, 2007 7:48 am
by Kieran Huggins
Oren wrote:I edited my previous post, read it. Kieran Huggins's way should be fine too, one thing though...

Wrong way:

Code: Select all

for($i=0; $i < count($array); $i++)
Right way:

Code: Select all

for($i=0, $size = count($array); $i < $size; $i++)
There is no need to check the size of the array over and over again - the result is the same each time anyway :P
Nice tip! Thanks!

Posted: Sat Jan 06, 2007 7:51 am
by Oren
No problem dude :wink:

Posted: Sat Jan 06, 2007 4:15 pm
by PHPycho
Thanks a lot dude...........
My problem got solved....
Actually i didnt got the point

Wrong way:

Code: Select all

for($i=0; $i < count($array); $i++)
Right way:

Code: Select all

for($i=0, $size = count($array); $i < $size; $i++)
What makes the difference......can u please clearify the question huh?
Anyway hats off for helping dudes....

Posted: Sat Jan 06, 2007 7:26 pm
by feyd
Often, it's the speed issue.

Unless you're constantly modifying the array with more (or less) elements, there's no reason to continually ask PHP to get the number of elements.

Please don't use AOL Speak.