473,411 Members | 2,085 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,411 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 1867
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
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.