Cannot create a new array key index

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
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

Cannot create a new array key index

Post by jayreed21 »

I am using the following code to iterate through a list of items:

Code: Select all

foreach ( $Cart["cart_content"] as $item)
		{
		$customerID = $item["customerID"];
  		if ($customerID == 0)
    		{
           $sellerName="Company";
        }
  		else
    		{
    		   $sellerName= getSellerNameFromID($customerID);
    		}
    $item["seller"]= $sellerName;
		}
Is there any reason why the sellerNames would not get added to $Cart["cart_content"]["seller"]?

THanks
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

$item is just a copy. You need (works for both php4 and php5):

Code: Select all

foreach ( $Cart["cart_content"] as $key=>$item)
...
//$item["seller"]= $sellerName; -- no
$Cart['cart_content'][$key]['seller'] = $sellerName;
Learn to use single quotes whenever possible btw ;)
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

Post by jayreed21 »

Thanks Mordred,

That got it. I am not sure why, but the foreach loop has always been confusing to me. I do like using them better than for loops for messing with arrays.

Cheers!


J
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

Mordred wrote:Learn to use single quotes whenever possible btw ;)
what does that mean?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

mentor wrote:
Mordred wrote:Learn to use single quotes whenever possible btw ;)
what does that mean?
If you won't use variable substitution ("This $var will be substituted"), it is good to 'train' yourself to use single quotes: 'Single quotes for static strings'. It is by no means fatal to use double quotes, but there is significant performance gain in single quotes, if such things matter to you.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Mordred wrote:It is by no means fatal to use double quotes, but there is significant performance gain in single quotes, if such things matter to you.
This was correct years ago (although the "performance gain" never was "significant"), modern versions of php generate exactly the same opcode for echo 'XYZ' and echo "XYZ".
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Oops, my bad, I apologise.

stereofrog is correct, for simple strings single and double quotes behave with an insignificant difference.
http://www.php.lt/benchmark/phpbench.php

I was left with the (wrong) impression that the php parser is used for double-quoted strings, while apparently this is done only if the string contains unescaped $-s.
Post Reply