Reply to Re: insert an element in numbered array

Your name:

Reply:


Posted by Andy Hassall on 06/01/05 15:42

On Wed, 01 Jun 2005 13:29:41 +0200, somaboy mx <nosuch@fakemail.fk> wrote:

>Say I have a multidim array:
>
>$array = array();
>
>$array[0]['name'] = 'Kevin';
>$array[0]['age'] = 23;
[snip]

>and I have a seperate array:
>
>$person = array();
>$person['name'] = 'Dana';
>$person['age'] = 33;
>
>now I want to insert the $person array into the $array in such a way
>that it ends up at index 1 and the subsequent elements get pushed up and
>renumbered, so $array[1] becomes $array[2] and so on.
>
>Does php have a built-in function that does this?

Yes, array_splice.

<pre>
<?php
$array = array();

$array[0]['name'] = 'Kevin';
$array[0]['age'] = 23;

$array[1]['name'] = 'Julia';
$array[1]['age'] = 31;

$array[2]['name'] = 'Bob';
$array[2]['age'] = 26;

$array[3]['name'] = 'Drew';
$array[3]['age'] = 37;

$person = array();
$person['name'] = 'Dana';
$person['age'] = 33;

print_r($array);
print_r($person);

array_splice($array, 1, 0, array($person));

print_r($array);
?>
</pre>

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация