TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-28-2009, 02:26 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Getting name of array

Given a multi array like this
Code:
 Array
(
    [warranty15] => Array
        (
            [id] => 15 year
            [text] => 15 Years
        )

    [warranty20] => Array
        (
            [id] => 20 year
            [text] => 20 Years
        )

    [warranty25] => Array
        (
            [id] => 25 year
            [text] => 25 Years
        )
}
If I run this code, it prints out the contents of each array.
PHP Code:
    foreach ($myArray as $key)
    {
      echo 
$key['id'].' - '.$key['text'].'<br>';
    } 
But I need to get the name of those arrays, like warranty15 and warranty20. Would someone please explain how to do that?
benton is offline  
Reply With Quote
Old 06-28-2009, 02:33 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
array_search($key$myArray); 
should do it, haven't tested though.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-28-2009, 03:01 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This should do it for you, amigo:

php Code:
foreach ($myArray as $key => $value)
{
    echo $key . ': ' . $value['id'] . ' - '.$value['text'] . '</br>';
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-28-2009, 12:19 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
This should do it for you, amigo:

php Code:
foreach ($myArray as $key => $value)
{
    echo $key . ': ' . $value['id'] . ' - '.$value['text'] . '</br>';
}
grrr why the hell did I not think of that, such a basic thing!
*sketchmedia facepalms himself*
guess i've been doing too much JS lately :(
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-28-2009, 08:05 PM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
This should do it for you, amigo:
php Code:
foreach ($myArray as $key => $value)
{
    echo $key . ': ' . $value['id'] . ' - '.$value['text'] . '</br>';
}
That doesn't seem to do it but maybe I missing sometihng obvious. It I run the above, the output is
Quote:
warranty - -
So it got the array name but not the contents of that array. I then thought to try this:

PHP Code:
    foreach ($myArray as $key)
    {
      echo 
$key.'<br>';
      foreach (
$key as $value)
      echo 
$key ' - '$value['id'].' - '.$value['text'].'<br>';
    } 
But it doesn't get the name of the array. The output is
Quote:
Array
Array - 15 year - 15 Years
Array - 20 year - 20 Years
Array - 25 year - 25 Years
Is there a way to have both the array name and its contents printed?
benton is offline  
Reply With Quote
Old 06-28-2009, 09:17 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Your example array, in the first post, must not be the exact array that your code is using else Wildhoney's snippet would have worked.

If the array is modified to what I think you might actually be using (based on the post just before this one) then the following code should help:
PHP Code:
$array = array
(
    
'warranty' => array
    (
        
'warranty15' => array('id' => '15 year''text' => '15 Years'),
        
'warranty20' => array('id' => '20 year''text' => '20 Years'),
        
'warranty25' => array('id' => '25 year''text' => '25 Years')
    )
);

foreach (
$array as $label => $warranties)
{
    
// "warranty"
    
echo $label."\n";
    foreach (
$warranties as $key => $warranty)
    {
        
// e.g. "warranty15 - 15 year - 15 Years<br>"
        
echo $key.' - '.$warranty['id'].' - '.$warranty['text']."\n";
    }

Which outputs:
warranty
warranty15 - 15 year - 15 Years
warranty20 - 20 year - 20 Years
warranty25 - 25 year - 25 Years
Salathe is offline  
Reply With Quote
Old 07-01-2009, 03:03 AM   #7 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thanks for the help. I was able to get it working with the revised code.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
How to search array benton General 3 03-29-2009 12:31 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 08:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design