473,750 Members | 2,447 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 1884
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
1708
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
2606
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
3405
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
1316
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
1399
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
2418
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
1725
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
3837
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
1168
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
9001
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9344
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9257
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
8264
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...
1
6810
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3327
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
3
2226
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.