Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-28-2005, 09:09 AM Array?
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
How do i echo the word 'Weapons' from this array?
PHP Code:
$shops = array("No such shop" => array(), "Weapons" => array(), "Food" => array(), "Toys" => array());
$shops[0]["type"] = "none";
$shops[1]["type"] = "weapon";
$shops[2]["type"] = "food";
$shops[3]["type"] = "toy";
$shops[0]["description"] = "Sorry this shop does not exist!";
$shops[1]["description"] = "Teach your pet(s) the art of self defence";
$shops[2]["description"] = "Yum!";
$shops[3]["description"] = "Every pet loves a new toy"
i tried
PHP Code:
echo $shops[1]; 
but it didn't work, is there a way, or is the array wrong?
timsquash5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-28-2005, 10:41 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
A couple of things here. You are trying to declare your $shops array as associative given the index names "No such shop," "Weapons," etc. But then you make assignments using integer indices -- treating it as a sequential array. Try this code:

PHP Code:
<?php

$shops 
= array("No such shop" => array(), "Weapons" => array(), "Food" => array(), "Toys" => array()); 

$shops["No such shop"]["type"] = "none"
$shops["Weapons"]["type"] = "weapon"
$shops["Food"]["type"] = "food"
$shops["Toys"]["type"] = "toy"

$shops["No such shop"]["description"] = "Sorry this shop does not exist!"
$shops["Weapons"]["description"] = "Teach your pet(s) the art of self defence"
$shops["Food"]["description"] = "Yum!"
$shops["Toys"]["description"] = "Every pet loves a new toy"

?>
Now, you are not storing the term "Weapons" anywhere -- it is the string key to an associative array within $shops. To print it, you must assign it to a value of the array, not the key. For example, we are assigning "weapon" to $shops["Weapons"]["type"], so you could print it using that variable:

PHP Code:
print $shops["Weapons"]["type"]; 
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-28-2005, 11:53 AM
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
how would i echo the word 'Weapons' in this example?

PHP Code:
<?php 

$shops 
= array("none" => array(), "Weapons" => array(), "none" => array(), "none" => array()); 

$shops["No such shop"]["type"] = "none"
$shops["Weapons"]["type"] = "none"
$shops["Food"]["type"] = "none"
$shops["Toys"]["type"] = "none"

$shops["No such shop"]["description"] = "none"
$shops["Weapons"]["description"] = "none"
$shops["Food"]["description"] = "none"
$shops["Toys"]["description"] = "none"

?>
timsquash5 is offline
Reply With Quote
View Public Profile
 
Old 03-28-2005, 12:34 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
I think he wants 'Weapons' and not 'weapon' in this case.

You can get the keys of an array using array_keys():
PHP Code:
print_r(array_keys($shops)); 
will result in:

Code:
Array (
  0=> "No such Shop",
  1=> "Weapons",
  2=> "Food",
  3=> "Toys"
)
So you can get Weapons from array_keys($shops)[1], but that doesn't really help, since if you knew the number was 1 you might as well just write "Weapons" on its own.

Since you are identifying your shops by name, without knowing in advance that you want the Weapons shop, you haven't got anything else with which to lookup the name.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-28-2005, 01:08 PM
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
i get the error

PHP Code:
Parse errorparse errorunexpected '[' 
when using print_r(array_keys($shops)[1]);
timsquash5 is offline
Reply With Quote
View Public Profile
 
Old 03-29-2005, 05:56 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
The print_r() was just meant to show what was in the array. Drop it and use echo or print:

PHP Code:
echo array_keys($shop)[1]; 
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-29-2005, 06:59 AM
Gaffer Sports's Avatar
Ultra Talker

Posts: 397
Name: Steve
Location: Scotland
Trades: 1
Quote:
Originally Posted by 0beron
The print_r() was just meant to show what was in the array. Drop it and use echo or print:

PHP Code:
echo array_keys($shop)[1]; 

Should you not be using this code:

PHP Code:
echo array_keys($shop[1]); 
Steve.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
Gaffer Sports is offline
Reply With Quote
View Public Profile Visit Gaffer Sports's homepage!
 
Old 03-29-2005, 07:53 AM
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
With your way Oberon i get the same error, and with Gaffer Sports' it echos the word 'Array'.
timsquash5 is offline
Reply With Quote
View Public Profile
 
Old 03-29-2005, 08:25 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
ok try
PHP Code:
$keys array_keys($shop);

echo 
$keys[1]; 
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-30-2005, 12:10 PM
simptech's Avatar
Skilled Talker

Posts: 81
Location: Cape Coral, Florida, United States
Trades: 0
had to get all the way to the last post to see if that correction was going to be made

Oberon's example will get you "Weapons"
__________________

Please login or register to view this content. Registration is FREE

FREE PHP scripts for your website!
simptech is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Array?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.43132 seconds with 12 queries