replacing the new array element problem ???

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
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

replacing the new array element problem ???

Post 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........
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
Last edited by Kieran Huggins on Sat Jan 06, 2007 7:42 am, edited 1 time in total.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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 ???
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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!
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

No problem dude :wink:
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply