Reply
Can someone translate two lines of code for me?
Old 03-08-2007, 07:58 AM Can someone translate two lines of code for me?
jason_bristol's Avatar
Extreme Talker

Posts: 247
Name: Jason Eyermann
Location: england bristol
Could someone translate two lines of code for me please?

I'm in the middle of a teach yourself php book but there are two lines that have not been explained properly. I've been trying to figue it out for about an hour.

PHP Code:
$CatalogFile "catalog.dat";

if (
file_exists($CatalogFile)) {
   
$Data file($catalogFile);
     foreach (
$data as $line) {
      
$lineArray explode(':'$line);
      
$sku trim($lineArray[0]);
      
$CATALOG[$sku] ['desc'] = trim($lineArray[1]);
      
$CATALOG[$sku] ['price'] = trim($lineArray[2]);
     }
} else { 
die(
"Could not find catalog file");

example line for catalog.dat
Code:
101:AA batteries (pack of 2) : 2.99
I understand it all apart from -

PHP Code:
$CATALOG[$sku] ['desc'] = trim($lineArray[1]);
$CATALOG[$sku] ['price'] = trim($lineArray[2]); 
I know it's an associative array. But why [$sku] and ['desc'] for the first and [$sku] and ['price'] for the second? I guess that ['desc'] and ['price'] are the keys.


I can't get my head around it.

thankyou
__________________
My Site/Blog - AspireCreate.com
jason_bristol is offline
Reply With Quote
View Public Profile Visit jason_bristol's homepage!
 
When You Register, These Ads Go Away!
Old 03-08-2007, 09:12 AM Re: Can someone translate two lines of code for me?
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 551
Name: surajit ray
Location: inside the heart of my friends
$lineArray = explode(':', $line);
This line explodes the line with ':'
now $lineArray is an array with three element e.g:101,AA batteries (pack of 2),2.99
$sku = trim($lineArray[0]);
assigns $sku a number, the product id.
Now it creates an associative array $CATALOG with the number:
LIKE,
$CATALOG[101] ['desc'] = AA batteries (pack of 2)
$CATALOG[101] ['price'] = 2.99
This is known as file parsing .
Is it clear?
__________________
Think+, work +, but not HIV + :)
jito is offline
Reply With Quote
View Public Profile
 
Old 03-08-2007, 09:23 AM Re: Can someone translate two lines of code for me?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,320
Name: Keith Marshall
Location: West Hartford, CT
First, its splitting the text line into an array by exploding at the colon. So the line

101:AA batteries (pack of 2) : 2.99

Breaks apart as

[0] => 101
[1] => AA batteries (pack of 2)
[2] => 2.99


So $sku is being assigned the [0] value, and then creates a multidemintional array for the other values.

So the var is as follows:

$CATALOG[101]['desc'] => AA batteries (pack of 2)
$CATALOG[101]['price'] => 2.99
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-08-2007, 09:34 AM Re: Can someone translate two lines of code for me?
jason_bristol's Avatar
Extreme Talker

Posts: 247
Name: Jason Eyermann
Location: england bristol
So $CATALOG gets concatnated with [101]?

if the next code is [202] then of course this means that -

$CATALOG[102]['desc'] => AA batteries (pack of 2)
$CATALOG[102]['price'] => 2.99

so $CATALOG[101] is one array and $CATALOG[102] is a different array? or can you have Numeric and Associative arrays working together?

I hope this make sense.
__________________
My Site/Blog - AspireCreate.com
jason_bristol is offline
Reply With Quote
View Public Profile Visit jason_bristol's homepage!
 
Old 03-08-2007, 09:50 AM Re: Can someone translate two lines of code for me?
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 551
Name: surajit ray
Location: inside the heart of my friends
Quote:
Originally Posted by jason_bristol View Post
So $CATALOG gets concatnated with [101]?

if the next code is [202] then of course this means that -

$CATALOG[102]['desc'] => AA batteries (pack of 2)
$CATALOG[102]['price'] => 2.99

so $CATALOG[101] is one array and $CATALOG[102] is a different array? or can you have Numeric and Associative arrays working together?

I hope this make sense.
yes you can work with numeric and associative array together.
__________________
Think+, work +, but not HIV + :)
jito is offline
Reply With Quote
View Public Profile
 
Old 03-08-2007, 10:12 AM Re: Can someone translate two lines of code for me?
jason_bristol's Avatar
Extreme Talker

Posts: 247
Name: Jason Eyermann
Location: england bristol
thanks! I understand now. talkupation ofcourse added
__________________
My Site/Blog - AspireCreate.com
jason_bristol is offline
Reply With Quote
View Public Profile Visit jason_bristol's homepage!
 
Reply     « Reply to Can someone translate two lines of code for me?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15078 seconds with 12 queries