473,700 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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","Washingt on DC"=> "202");
print @areas{'atlanta ','manhattan'};

I hoped PHP would support something like:

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

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 1879
thecrow <ca**********@h otmail.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","Washingt on 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','miam i'];


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**********@h otmail.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","Washingt on 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','miam i'];


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**********@h otmail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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","Washingt on DC"=> "202");
print @areas{'atlanta ','manhattan'};

I hoped PHP would support something like:

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

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','miam i'];

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
1700
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 an expression twice (or use a temporary variable). Wouldn't it be nice if the Python grammar had supported this frequent use? My idea is that the expression above might be expressed as l.
19
2597
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 rules written down somewhere. Thanks,
29
3400
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 wonder why it is not implemented for tuples. George
1
1315
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 problem is that I would like to implemet slices but, that seems to be impossible with how slices are implemented now.
1
1394
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, Hey Presto! no more tediously typing a 1-9 seq into your interpreter and then getting a slice just to check what you get.. (Yes you. You know you do that !) ... Cheers Steve x = '0123456789' x 0123456789 x
3
2414
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 showing up in their proper places in IE(7)? The button bar I created is perfectly set up in Firefox, but when I preview in IE, not so much. The slices have weird spaces in between. Why.....? Any help greatly appreciated. Thx.
31
1718
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 first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example: +---+---+---+---+---+ | H | e | l | p | A |
5
3833
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 show various countries appearing to be depressed when "moused-over". To keep it simple at first, I just decided to try two countries. After copying the HTML and JavaScript codes generated by ImageReady into the page I wanted to insert the map into,...
3
1167
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): len_seq = len(seq) for i, e1 in enumerate(seq):
0
9206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9066
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8916
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7802
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2383
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2022
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.