470,833 Members | 1,405 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

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 1381
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Raptor | last post: by
10 posts views Thread by dof | last post: by
33 posts views Thread by Benjamin M. Stocks | last post: by
2 posts views Thread by wlevine | last post: by
24 posts views Thread by Michael | last post: by
7 posts views Thread by harvey | last post: by
9 posts views Thread by fgh.vbn.rty | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.