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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
"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. |
|
|||
|
> 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 |