473,578 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array index ordering

Lets say we have an array:

$array[]='apples';
$array[]='orange';
$array[]='lemon';

echo $array[1]; // orange
Now, lets say we want to insert "grape", between apples and orange. How
do we do that?

I have some memory of seeing a php shortcut to do this, but I can't find
it now.

Also, I see there are a number of custom php functions for sorting
(kudos on some of these). Is there a way to write your own sorting function?

Jeff
Jun 27 '08 #1
6 1524
On 16 Jun, 14:45, Jeff <jeff@spam_me_n ot.comwrote:
* *Lets say we have an array:

$array[]='apples';
$array[]='orange';
$array[]='lemon';

echo $array[1]; // orange

Now, lets say we want to insert "grape", between apples and orange. How
do we do that?

I have some memory of seeing a php shortcut to do this, but I can't find
it now.

* *Also, I see there are a number of custom php functions for sorting
(kudos on some of these). Is there a way to write your own sorting function?

* *Jeff
You are looking for array_splice()
http://uk2.php.net/manual/en/function.array-splice.php
But you could have scanned the list of array functions in the manual
just as easily as I could.
Jun 27 '08 #2
..oO(Jeff)
Lets say we have an array:

$array[]='apples';
$array[]='orange';
$array[]='lemon';

echo $array[1]; // orange
Now, lets say we want to insert "grape", between apples and orange. How
do we do that?
You just add it like above and then sort the array.
>I have some memory of seeing a php shortcut to do this, but I can't find
it now.

Also, I see there are a number of custom php functions for sorting
(kudos on some of these). Is there a way to write your own sorting function?
usort()

Micha
Jun 27 '08 #3
On 16 Jun, 15:30, Michael Fesser <neti...@gmx.de wrote:
.oO(Jeff)
* Lets say we have an array:
$array[]='apples';
$array[]='orange';
$array[]='lemon';
echo $array[1]; // orange
Now, lets say we want to insert "grape", between apples and orange. How
do we do that?

You just add it like above and then sort the array.
If he did what you poposed, he'd get
apples
grape
lemon
orange

But he wanted
apples
grape
orange
lemon

So array_splice is the way to do it.
Jun 27 '08 #4
Captain Paralytic wrote:
On 16 Jun, 14:45, Jeff <jeff@spam_me_n ot.comwrote:
> Lets say we have an array:

$array[]='apples';
$array[]='orange';
$array[]='lemon';

echo $array[1]; // orange

Now, lets say we want to insert "grape", between apples and orange. How
do we do that?

I have some memory of seeing a php shortcut to do this, but I can't find
it now.

Also, I see there are a number of custom php functions for sorting
(kudos on some of these). Is there a way to write your own sorting function?

Jeff

You are looking for array_splice()
http://uk2.php.net/manual/en/function.array-splice.php
But you could have scanned the list of array functions in the manual
just as easily as I could.
Yes, I see splice with a 0 length.

I have a hard time with the manual.

If I start at the index page (http://www.php.net/manual/en/) -type ->
array

I get this page:

http://www.php.net/manual/en/language.types.array.php

Which has none of the array functions.

If I go: index->functions, I can't find the internal function list. I
know it's there but I can't find it.

The keyword search works fine, but I don't know the keywords. I
suppose I'll have to bookmark the appropriate pages as I find them! I
just don't seem to be able to figure out how to navigate the manual.

I don't wish to annoy you with easy questions, my apologies.

Jeff
Jun 27 '08 #5
In our last episode, <Mb************ *************** ***@earthlink.c om>, the
lovely and talented Jeff broadcast on comp.lang.php:
I have a hard time with the manual.
If I start at the index page (http://www.php.net/manual/en/) -type ->
array
What exactly do you mean by "->"? The link to function index is at the
bottom of the contents. It is alphabetical and that makes the array
functions not too far down.
--
Lars Eighner <http://larseighner.com/us****@larseigh ner.com
Countdown: 217 days to go.
Jun 27 '08 #6
Lars Eighner wrote:
In our last episode, <Mb************ *************** ***@earthlink.c om>, the
lovely and talented Jeff broadcast on comp.lang.php:
>I have a hard time with the manual.
>If I start at the index page (http://www.php.net/manual/en/) -type ->
array

What exactly do you mean by "->"?
Choose / select , in other words, go to.

The link to function index is at the
bottom of the contents.
Thanks, I see it now! Last link in the appendix! That will make life
easier!

Jeff
It is alphabetical and that makes the array
functions not too far down.

Jun 27 '08 #7

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

Similar topics

19
4120
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
1
8363
by: Raptor | last post by:
Hi, I'm quite new to MySQL and quite impressed by its feature set. I've also been looking at Interbase and it has a feature that allows a multidimensional array to be stored in a single field. Does MySQL also have this feature? I was thinking about using MySQLs GIS data types but it only supports a two dimensional point, where as I need...
10
9071
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX}; RWCString MESSAGES; <--------------------- MESSAGES = "MESSAGE_A";
33
3380
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a 4-byte value. Can I use the arithmatic shift operators to hide the endian-ness of the underlying processor when assembling a native 4-byte value...
2
46985
by: wlevine | last post by:
Can someone give me an example of how to construct an array of objects? for example, I have: function objFacility(id,name,adr,city,state,zip) { this.id = id this.name=name this.adr=adr this.city=city this.state=state this.zip=zip
24
3421
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
23
9208
by: JDT | last post by:
Hi, It seems that using floats as the first tupelo for an STL map (shown below) can cause problems but I don't remember what the problems were. Is it common practice to use a structure like below? I would appreciate if you can share your experience using such a structure. Thanks. std::map<float, intm; JD
7
12286
by: harvey | last post by:
What is the easiest way to get both the last item name and last item value from an associative array? I can get the value with array_pop() but can't see anything similar for the item's name. Thanks
9
7593
by: fgh.vbn.rty | last post by:
I am frequently using maps like the following: map<string, map<int, vector< pair<int, int m1; map<int, map<string, map<int, int m2; This can be a little difficult to maintain if another coder doesn't really know the what the indexes stand for, the ordering of the indexes, etc., Is there a better way to define multiple index maps? Of...
0
7778
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...
0
8290
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...
1
7874
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...
0
8148
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...
0
6522
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...
1
5664
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...
0
5342
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...
0
3798
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.