Connecting Tech Pros Worldwide Forums | Help | Site Map

How to do slices in PHP?

thecrow
Guest
 
Posts: n/a
#1: Jul 17 '05
I am trying to figure out how to use hash slices in PHP. (Assign
certain values of an associative array into variables, not dependent on
loops or internal order of the associative array).

In Perl, it would be accomplished like this:

%areas = ("atlanta" => "404", "miami" => "305", "manhattan" =>
"212","Washington DC"=> "202");
print @areas{'atlanta','manhattan'};

I hoped PHP would support something like:

list($atl, $mi) = $areas['atlanta','miami'];

But it seems the parser didn't agree with me.

I looked into array_slice, but that depends on the sequence of the
array, which isn't a safe way to program.

Does anybody have any more ideas before I have to resort to writing a
function?


Daniel Tryba
Guest
 
Posts: n/a
#2: Jul 17 '05

re: How to do slices in PHP?


thecrow <carltonbrown@hotmail.com> wrote:[color=blue]
> I am trying to figure out how to use hash slices in PHP. (Assign
> certain values of an associative array into variables, not dependent on
> loops or internal order of the associative array).
>
> In Perl, it would be accomplished like this:
>
> %areas = ("atlanta" => "404", "miami" => "305", "manhattan" =>
> "212","Washington DC"=> "202");
> print @areas{'atlanta','manhattan'};[/color]

So what is the result of these funny character?
[color=blue]
> I hoped PHP would support something like:
>
> list($atl, $mi) = $areas['atlanta','miami'];[/color]

Did you mean:
$atl=$areas['atlanta'];
$mi=$areas['miami'];

http://nl2.php.net/manual/en/functio...sect-assoc.php
might help you if it wasn't in CVS only.

Daniel Tryba
Guest
 
Posts: n/a
#3: Jul 17 '05

re: How to do slices in PHP?


thecrow <carltonbrown@hotmail.com> wrote:[color=blue]
> I am trying to figure out how to use hash slices in PHP. (Assign
> certain values of an associative array into variables, not dependent on
> loops or internal order of the associative array).
>
> In Perl, it would be accomplished like this:
>
> %areas = ("atlanta" => "404", "miami" => "305", "manhattan" =>
> "212","Washington DC"=> "202");
> print @areas{'atlanta','manhattan'};[/color]

So what is the result of these funny character?
[color=blue]
> I hoped PHP would support something like:
>
> list($atl, $mi) = $areas['atlanta','miami'];[/color]

Did you mean:
$atl=$areas['atlanta'];
$mi=$areas['miami'];

http://nl2.php.net/manual/en/functio...sect-assoc.php
might help you if it wasn't in CVS only.

Chung Leong
Guest
 
Posts: n/a
#4: Jul 17 '05

re: How to do slices in PHP?


"thecrow" <carltonbrown@hotmail.com> wrote in message
news:1111201992.895328.79640@o13g2000cwo.googlegro ups.com...[color=blue]
> I am trying to figure out how to use hash slices in PHP. (Assign
> certain values of an associative array into variables, not dependent on
> loops or internal order of the associative array).
>
> In Perl, it would be accomplished like this:
>
> %areas = ("atlanta" => "404", "miami" => "305", "manhattan" =>
> "212","Washington DC"=> "202");
> print @areas{'atlanta','manhattan'};
>
> I hoped PHP would support something like:
>
> list($atl, $mi) = $areas['atlanta','miami'];
>
> But it seems the parser didn't agree with me.
>
> I looked into array_slice, but that depends on the sequence of the
> array, which isn't a safe way to program.
>
> Does anybody have any more ideas before I have to resort to writing a
> function?[/color]

Nope. You will have to write it yourself.


*/hack/*
Guest
 
Posts: n/a
#5: Jul 17 '05

re: How to do slices in PHP?


> I hoped PHP would support something like:[color=blue]
>
> list($atl, $mi) = $areas['atlanta','miami'];
>
> But it seems the parser didn't agree with me.
>
> I looked into array_slice, but that depends on the sequence of the
> array, which isn't a safe way to program.[/color]

try this:

$areas = array('atlanta' => 404, 'miami' => 305, 'manhattan' => 212,
'Washington DC' => 202);

list($atl, $mi) = array($areas['atlanta'], $areas['miami']);

print "Atlanta: $atl, Miami: $mi";

-Wil


Closed Thread