473,225 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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?

Jul 17 '05 #1
4 1860
thecrow <ca**********@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.

Jul 17 '05 #2
thecrow <ca**********@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.

Jul 17 '05 #3
"thecrow" <ca**********@hotmail.com> wrote in message
news:11*********************@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.
Jul 17 '05 #4
> 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
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Noam Raphael | last post by:
Hello, Many times I find myself asking for a slice of a specific length, and writing something like l. This happens both in interactive use and when writing Python programs, where I have to write...
19
by: David Abrahams | last post by:
Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this result very surprising, and would just like to see the...
29
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I...
1
by: Antoon Pardon | last post by:
I'm for the moment writing two classes. A table, which is like a list, but can start at any integer. A tree which is like a dictionary, but will iterate over the keys in sorted order. The...
1
by: meridian | last post by:
If, like me, you're always forgetting which way around your list/seq slices need to go then worry no more. Just put my handy "slice lookupper" (TM) ) on a (large!) PostIt beside your screen and,...
3
slightlybefuddled
by: slightlybefuddled | last post by:
(Exporting ImageReady slices as CSS rather than tables) apparently means it'll work just fine in Firefox, but do wacky stuff in IE? Can anyone help me figure out why on earth the slices are not...
31
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the...
5
by: NuberSteve | last post by:
I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a world map. I basically wanted a map that would...
3
by: David | last post by:
That xpairs() generator is nice, but it's not the best possible code What do you mean by best possible? Most efficient? Most readable? And why don't you use islice? eg: def xpairs(seq):...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.