How to do slices in PHP?

This is a discussion on How to do slices in PHP? within the PHP Language forums, part of the PHP Programming Forums category; I am trying to figure out how to use hash slices in PHP. (Assign certain values of an associative array ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-19-2005
thecrow
 
Posts: n/a
Default How to do slices in PHP?

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?

Reply With Quote
  #2 (permalink)  
Old 03-19-2005
Daniel Tryba
 
Posts: n/a
Default Re: How to do slices in PHP?

thecrow <carltonbrown@hotmail.com> wrote:
> 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'};


So what is the result of these funny character?

> I hoped PHP would support something like:
>
> list($atl, $mi) = $areas['atlanta','miami'];


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.

Reply With Quote
  #3 (permalink)  
Old 03-19-2005
Daniel Tryba
 
Posts: n/a
Default Re: How to do slices in PHP?

thecrow <carltonbrown@hotmail.com> wrote:
> 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'};


So what is the result of these funny character?

> I hoped PHP would support something like:
>
> list($atl, $mi) = $areas['atlanta','miami'];


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.

Reply With Quote
  #4 (permalink)  
Old 03-19-2005
Chung Leong
 
Posts: n/a
Default Re: How to do slices in PHP?

"thecrow" <carltonbrown@hotmail.com> wrote in message
news:1111201992.895328.79640@o13g2000cwo.googlegro ups.com...
> 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?


Nope. You will have to write it yourself.


Reply With Quote
  #5 (permalink)  
Old 03-25-2005
*/hack/*
 
Posts: n/a
Default Re: How to do slices in PHP?

> 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.


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


Reply With Quote
Reply
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0