Newb question about getting keys/values from a single array element
Newb question about getting keys/values from a single array element
am 09.10.2009 03:08:48 von Daevid Vincent
I feel like a total newb asking this, but I'm just having a brain fart or
something...
I'm writing a page where I can either get back a list of items:
Array {
[1233] => "apple",
[6342] => "apricot",
[2345] => "banana",
...
}
where the user then refines it by choosing one single item and a single
element array is returned like this:
Array {
[8575] => "peach",
}
How can I get this $item so I can print it like so:
echo "The ID is $id and the name is $name";
Normally with an array of items, I do a:
foreach ($item as $id => $name) echo...
But that seems overkill for this scenario.
The rub is that I don't know the "id", so I can't use $item[0], and I also
don't have something like $item['name'] to use either.
There's got to be an easy way to extract those.
list($id, $name) = $operator;
Felt like it would work for a minute (wishful thinking).
(I'm too embarrased to even sign my name on this one)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Newb question about getting keys/values from a single array
am 09.10.2009 04:30:22 von Jonathan Tapicer
One possible solution:
$a =3D array(8575 =3D> 'peach');
list($id, $name) =3D array_merge(array_keys($a), array_values($a));
echo "The ID is $id and the name is $name";
?>
Prints: "The ID is 8575 and the name is peach".
Regards,
Jonathan
On Thu, Oct 8, 2009 at 10:08 PM, Daevid Vincent wrote:
> I feel like a total newb asking this, but I'm just having a brain fart or
> something...
>
> I'm writing a page where I can either get back a list of items:
>
> =A0 =A0 =A0 =A0Array {
> =A0 =A0 =A0 =A0 =A0[1233] =3D> "apple",
> =A0 =A0 =A0 =A0 =A0[6342] =3D> "apricot",
> =A0 =A0 =A0 =A0 =A0[2345] =3D> "banana",
> =A0 =A0 =A0 =A0 =A0...
> =A0 =A0 =A0 =A0}
>
> where the user then refines it by choosing one single item and a single
> element array is returned like this:
>
> =A0 =A0 =A0 =A0Array {
> =A0 =A0 =A0 =A0 =A0[8575] =3D> "peach",
> =A0 =A0 =A0 =A0}
>
> How can I get this $item so I can print it like so:
>
> =A0 =A0 =A0 =A0echo "The ID is $id and the name is $name";
>
> Normally with an array of items, I do a:
>
> =A0 =A0 =A0 =A0foreach ($item as $id =3D> $name) echo...
>
> But that seems overkill for this scenario.
>
> The rub is that I don't know the "id", so I can't use $item[0], and I als=
o
> don't have something like $item['name'] to use either.
>
> There's got to be an easy way to extract those.
>
> =A0 =A0 =A0 =A0list($id, $name) =3D $operator;
>
> Felt like it would work for a minute (wishful thinking).
>
>
> (I'm too embarrased to even sign my name on this one)
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Newb question about getting keys/values from a single
am 09.10.2009 04:39:47 von Paul M Foster
On Thu, Oct 08, 2009 at 06:08:48PM -0700, Daevid Vincent wrote:
> I feel like a total newb asking this, but I'm just having a brain fart or
> something...
>
> I'm writing a page where I can either get back a list of items:
>
> Array {
> [1233] => "apple",
> [6342] => "apricot",
> [2345] => "banana",
> ...
> }
>
> where the user then refines it by choosing one single item and a single
> element array is returned like this:
>
> Array {
> [8575] => "peach",
> }
>
> How can I get this $item so I can print it like so:
>
> echo "The ID is $id and the name is $name";
>
> Normally with an array of items, I do a:
>
> foreach ($item as $id => $name) echo...
>
> But that seems overkill for this scenario.
>
> The rub is that I don't know the "id", so I can't use $item[0], and I also
> don't have something like $item['name'] to use either.
>
> There's got to be an easy way to extract those.
>
> list($id, $name) = $operator;
>
> Felt like it would work for a minute (wishful thinking).
If you don't know the index, then you really have no choice but to
iterate through the POST array (if I understand you correctly). One
problem I see is that you're using an integer for your index. The way I
normally do things like this is to provide a prefix for the index. Like
this:
$items = array('fruit_1233' => 'apple',
'fruit_6342' => 'apricot',
'fruit_2345' => 'banana',
'fruit_8575' => 'peach');
Then, out of all the POST members returned, I can pick out the ones
pertaining to fruit by simply looking for ones which have the 'fruit_'
prefix as their index. It tends to be clunky, but it's the only way I've
found to make it work:
$fruits = get_fruits_from_table();
foreach ($fruits as $key => $value) {
$index = 'fruit_' . $key;
if (isset($_POST[$index])) {
echo "The user wants fruit #$key, $value.";
}
}
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Newb question about getting keys/values from a single array element
am 09.10.2009 14:32:09 von Shawn McKenzie
Daevid Vincent wrote:
> I feel like a total newb asking this, but I'm just having a brain fart or
> something...
>
> I'm writing a page where I can either get back a list of items:
>
> Array {
> [1233] => "apple",
> [6342] => "apricot",
> [2345] => "banana",
> ...
> }
>
> where the user then refines it by choosing one single item and a single
> element array is returned like this:
>
> Array {
> [8575] => "peach",
> }
>
> How can I get this $item so I can print it like so:
>
> echo "The ID is $id and the name is $name";
>
> Normally with an array of items, I do a:
>
> foreach ($item as $id => $name) echo...
>
> But that seems overkill for this scenario.
>
> The rub is that I don't know the "id", so I can't use $item[0], and I also
> don't have something like $item['name'] to use either.
>
> There's got to be an easy way to extract those.
>
> list($id, $name) = $operator;
>
> Felt like it would work for a minute (wishful thinking).
>
>
> (I'm too embarrased to even sign my name on this one)
>
$a = array(8575 => 'peach');
$key = key($a);
$value = current($a);
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Newb question about getting keys/values from a singlearray element
am 09.10.2009 16:47:33 von TedD
At 6:08 PM -0700 10/8/09, Daevid Vincent wrote:
>I feel like a total newb asking this, but I'm just having a brain fart or
>something...
>
>I'm writing a page where I can either get back a list of items:
>
> Array {
> [1233] => "apple",
> [6342] => "apricot",
> [2345] => "banana",
> ...
> }
>
>where the user then refines it by choosing one single item and a single
>element array is returned like this:
>
> Array {
> [8575] => "peach",
> }
>
>How can I get this $item so I can print it like so:
>
> echo "The ID is $id and the name is $name";
I'm not sure what you are asking.
How does the user pick from a list of items -- a selection list, a
list of radio boxes, what?
If you are using a post/get to get the values and thus passing only
one value, then why not catenate the id with the value (e.g.,
8575peach) and parse it after receiving?
If you show me a url, then I can get a better understanding of what
you are trying to do.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Newb question about getting keys/values from a single array element
am 09.10.2009 21:46:44 von Daevid Vincent
> -----Original Message-----
> From: Shawn McKenzie [mailto:nospam@mckenzies.net]
> Sent: Friday, October 09, 2009 5:32 AM
> To: Daevid Vincent
> Cc: php-general@lists.php.net
> Subject: Re: Newb question about getting keys/values from a
> single array element
>
> Daevid Vincent wrote:
> > I feel like a total newb asking this, but I'm just having a
> brain fart or
> > something...
> >
> > I'm writing a page where I can either get back a list of items:
> >
> > Array {
> > [1233] => "apple",
> > [6342] => "apricot",
> > [2345] => "banana",
> > ...
> > }
> >
> > where the user then refines it by choosing one single item
> and a single
> > element array is returned like this:
> >
> > Array {
> > [8575] => "peach",
> > }
> >
> > How can I get this $item so I can print it like so:
> >
> > echo "The ID is $id and the name is $name";
> >
> > Normally with an array of items, I do a:
> >
> > foreach ($item as $id => $name) echo...
> >
> > But that seems overkill for this scenario.
> >
> > The rub is that I don't know the "id", so I can't use
> $item[0], and I also
> > don't have something like $item['name'] to use either.
> >
> > There's got to be an easy way to extract those.
> >
> > list($id, $name) = $operator;
> >
> > Felt like it would work for a minute (wishful thinking).
> >
> >
> > (I'm too embarrased to even sign my name on this one)
> >
>
> $a = array(8575 => 'peach');
>
> $key = key($a);
> $value = current($a);
BRILLIANT! That's EXACTLY what I was looking for!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php