TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Getting name of array (http://www.talkphp.com/general/4635-getting-name-array.html)

benton 06-28-2009 02:26 AM

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?

sketchMedia 06-28-2009 02:33 AM

PHP Code:

array_search($key$myArray); 

should do it, haven't tested though.

Wildhoney 06-28-2009 03:01 AM

This should do it for you, amigo:

php Code:
foreach ($myArray as $key => $value)
{
    echo $key . ': ' . $value['id'] . ' - '.$value['text'] . '</br>';
}

sketchMedia 06-28-2009 12:19 PM

Quote:

Originally Posted by Wildhoney (Post 26227)
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 :(

benton 06-28-2009 08:05 PM

Quote:

Originally Posted by Wildhoney (Post 26227)
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?

Salathe 06-28-2009 09:17 PM

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

benton 07-01-2009 03:03 AM

Thanks for the help. I was able to get it working with the revised code.


All times are GMT. The time now is 10:53 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0