Trying to extract KEYS out of associative array
Okay...
I have setup an associative array (thru an HTML checkbox form).
All the keys are text, all the values are 1 or null (checked or not).
I need a way to dump all the KEYS of this array into a single variable.
Example:
$airports = array
(
"LGW" => "",
"LHR" => "1",
"STN" => "",
"YYZ" => "1",
"MIA" => ""
);
How do I defing $good as "LHR YYZ" ?
(used a space as a delimiter, but it can be a comma or /, etc.)
I did a bunch of dogpile searching... still haven't been able to figure
it out. I DID find a function that worked, but it only returned the
FIRST instance of a "1". Then it stopped. I need to traverse ALL of
the array.
Thanks!
Mark :)
Re: Trying to extract KEYS out of associative array
easier than you'd think...
<?
function filterAirports($value, $key, &$selected)
{
if (!$value){ return; }
$selected[] = $key;
}
$airports = array(
'LGW' => '' ,
'LHR' => '1' ,
'STN' => '' ,
'YYZ' => '1' ,
'MIA' => ''
);
$selectedAirports = array();
array_walk($airports, 'filterAirports', $selectedAirports);
$selectedAirports = implode(' ', $selectedAirports);
print_r($selectedAirports);
?>
happy flying...and thanks for not yelling this time. :)
"Me :)" <not [at] this.time> wrote in message
news:6sh823164tdfvv4ccaq67rqsltts9c2ip0 [at] 4ax.com...
| Okay...
|
| I have setup an associative array (thru an HTML checkbox form).
| All the keys are text, all the values are 1 or null (checked or not).
|
| I need a way to dump all the KEYS of this array into a single variable.
|
|
| Example:
|
| $airports = array
| (
| "LGW" => "",
| "LHR" => "1",
| "STN" => "",
| "YYZ" => "1",
| "MIA" => ""
| );
|
|
| How do I defing $good as "LHR YYZ" ?
| (used a space as a delimiter, but it can be a comma or /, etc.)
|
|
| I did a bunch of dogpile searching... still haven't been able to figure
| it out. I DID find a function that worked, but it only returned the
| FIRST instance of a "1". Then it stopped. I need to traverse ALL of
| the array.
|
|
|
|
| Thanks!
| Mark :)
Re: Trying to extract KEYS out of associative array
sorry...my bad. you need to pass $selectedAirports byref. i'm glad i ran the
code to test it...that was just top-o-the-head so i felt i should at least
give it a go for syntax and the like. change the following to:
array_walk($airports, 'filterAirports', &$selectedAirports);
and all should be well.
"Steve" <no.one [at] example.com> wrote in message
news:1PYUh.1033$PA6.110 [at] newsfe04.lga...
| easier than you'd think...
|
| <?
| function filterAirports($value, $key, &$selected)
| {
| if (!$value){ return; }
| $selected[] = $key;
| }
| $airports = array(
| 'LGW' => '' ,
| 'LHR' => '1' ,
| 'STN' => '' ,
| 'YYZ' => '1' ,
| 'MIA' => ''
| );
| $selectedAirports = array();
| array_walk($airports, 'filterAirports', $selectedAirports);
| $selectedAirports = implode(' ', $selectedAirports);
| print_r($selectedAirports);
| ?>
|
| happy flying...and thanks for not yelling this time. :)
|
|
| "Me :)" <not [at] this.time> wrote in message
| news:6sh823164tdfvv4ccaq67rqsltts9c2ip0 [at] 4ax.com...
|| Okay...
||
|| I have setup an associative array (thru an HTML checkbox form).
|| All the keys are text, all the values are 1 or null (checked or not).
||
|| I need a way to dump all the KEYS of this array into a single variable.
||
||
|| Example:
||
|| $airports = array
|| (
|| "LGW" => "",
|| "LHR" => "1",
|| "STN" => "",
|| "YYZ" => "1",
|| "MIA" => ""
|| );
||
||
|| How do I defing $good as "LHR YYZ" ?
|| (used a space as a delimiter, but it can be a comma or /, etc.)
||
||
|| I did a bunch of dogpile searching... still haven't been able to figure
|| it out. I DID find a function that worked, but it only returned the
|| FIRST instance of a "1". Then it stopped. I need to traverse ALL of
|| the array.
||
||
||
||
|| Thanks!
|| Mark :)
|
|
Re: Trying to extract KEYS out of associative array
On Apr 17, 12:11 am, "Me :)" <n... [at] this.time> wrote:
> Okay...
>
> I have setup an associative array (thru an HTML checkbox form).
> All the keys are text, all the values are 1 or null (checked or not).
>
> I need a way to dump all the KEYS of this array into a single variable.
>
> Example:
>
> $airports = array
> (
> "LGW" => "",
> "LHR" => "1",
> "STN" => "",
> "YYZ" => "1",
> "MIA" => ""
> );
>
> How do I defing $good as "LHR YYZ" ?
> (used a space as a delimiter, but it can be a comma or /, etc.)
>
> I did a bunch of dogpile searching... still haven't been able to figure
> it out. I DID find a function that worked, but it only returned the
> FIRST instance of a "1". Then it stopped. I need to traverse ALL of
> the array.
>
> Thanks!
> Mark :)
How did you get the array above? If the box isn't checked the value
shouldn't be set at all (in $_GET or $_POST). If that were the case
and your array looked like this:
$airports = array("LGW" => "", "LHR" => "1", "YYZ" => "1");
Then it's really easy:
implode(' ', array_keys($airports);
Re: Trying to extract KEYS out of associative array
| How did you get the array above? If the box isn't checked the value
| shouldn't be set at all (in $_GET or $_POST). If that were the case
| and your array looked like this:
|
| $airports = array("LGW" => "", "LHR" => "1", "YYZ" => "1");
|
| Then it's really easy:
|
| implode(' ', array_keys($airports);
follow directions!!!
that would print out the keys regardless of the values, i.e. LGW would print
when it shouldn't.
how did he get the array above? probably like a good boi, he doesn't just
grab shit out of $_POST...he'd define his inputs thusly:
$airports = array(
'LGW' => '' ,
'LHR' => '' ,
'STN' => '' ,
'YYZ' => '' ,
'MIA' => ''
);
he'd then be efficient:
function setValues($value, $key, &$airports)
{
if (!in_array($key, array_keys($airports)){ continue; }
$airports[$key] = $value;
}
array_walk($_POST, 'setValues', &$airports);
which would now give you the array you are questioning. i'd imagine that he
(and you) would only want to present/read the pertainent code...which is
what he supplied.
either way, you solution doesn't work. sorry.