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 10-03-2008, 11:03 AM Array screen output
frofi's Avatar
Extreme Talker

Posts: 236
Location: London
Trades: 0
Hi guys! I just want to ask something very basic. I need to print some array keys on the screen. The only way to print an array I know is print_r(). But that prints the array along with the word Array and all numeric indexes. I just need the keys.
Example:

<?php
$states = array("US" => "Washington","AU" => "Canberra","UK" => "London");
$keys = array_keys($states);
print_r($keys);
?>

This would output:
Array ( [0] => US [1] => AU [2] => UK )

But I want the output to look like this:
US, AU, UK

How can it be done?
__________________
THE FORCE is with me at last! All I need now is some TALKUPATION ;)
frofi is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-03-2008, 11:19 AM Re: Array screen output
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Use a foreach loop to loop through the array

PHP Code:
<?php 
$states 
= array("US" => "Washington","AU" => "Canberra","UK" => "London");
foreach(
$states as $key => $value){
echo 
$key.",";
}
?>
Alternative using array_keys and implode
PHP Code:
<?php 
$states 
= array("US" => "Washington","AU" => "Canberra","UK" => "London");
$keys=array_keys($states);
echo 
implode(",",$keys);
?>

Last edited by maxxximus; 10-03-2008 at 11:27 AM..
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 10-03-2008, 12:11 PM Re: Array screen output
frofi's Avatar
Extreme Talker

Posts: 236
Location: London
Trades: 0
Thank you, that worked great! I’m still learning and I haven’t gotten to creating loops yet. I’m doing arrays now and I couldn’t concentrate unless I knew how to output an array the way I want. I know implode and explode from saving into and reading from a file, but it didn’t occur to me as a solution to my problem.

Thank you again. Added talkupation.
__________________
THE FORCE is with me at last! All I need now is some TALKUPATION ;)
frofi is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Array screen output
 

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