473,397 Members | 2,033 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,397 software developers and data experts.

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 1515
On 16 Jun, 14:45, Jeff <jeff@spam_me_not.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.dewrote:
.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_not.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.com> , 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****@larseighner.com
Countdown: 217 days to go.
Jun 27 '08 #6
Lars Eighner wrote:
In our last episode, <Mb******************************@earthlink.com> , 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
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
1
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. ...
10
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 .... ...
33
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...
2
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...
24
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...
23
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...
7
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. ...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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.