FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Inserting into associative array
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Inserting into associative array [message #169903] Wed, 29 September 2010 19:31 Go to next message
Jonathan Stein is currently offline  Jonathan Stein
Messages: 43
Registered: September 2010
Karma: 0
Member
Hi.

Let's say I have an array like
array(
'fruit' => 'banana',
'color' => 'yellow'
)

Now I want to insert
'shape' => 'round'
between 'fruit' and 'color'. I would think that PHP had a simple
solution for that, but I haven't found it... My solution is to loop
through the original array and build a new one.

Does anyone have a better solution?

Regards

Jonathan
Re: Inserting into associative array [message #169904 is a reply to message #169903] Wed, 29 September 2010 20:05 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Jonathan Stein)

> Let's say I have an array like
> array(
> 'fruit' => 'banana',
> 'color' => 'yellow'
> )
>
> Now I want to insert
> 'shape' => 'round'
> between 'fruit' and 'color'. I would think that PHP had a simple
> solution for that, but I haven't found it... My solution is to loop
> through the original array and build a new one.
>
> Does anyone have a better solution?

Nope. But maybe you could explain why you need such a special order,
which can't be achieved with the given array sorting functions.

Micha
Re: Inserting into associative array [message #169905 is a reply to message #169903] Wed, 29 September 2010 21:07 Go to previous messageGo to next message
Hamish Campbell is currently offline  Hamish Campbell
Messages: 15
Registered: September 2010
Karma: 0
Junior Member
On Sep 30, 8:31 am, Jonathan Stein <jst...@image.dk> wrote:
> Hi.
>
> Let's say I have an array like
>    array(
>      'fruit' => 'banana',
>      'color' => 'yellow'
>    )
>
> Now I want to insert
>    'shape' => 'round'
> between 'fruit' and 'color'. I would think that PHP had a simple
> solution for that, but I haven't found it... My solution is to loop
> through the original array and build a new one.
>
> Does anyone have a better solution?
>
>    Regards
>
>      Jonathan

<?php
function array_insert(&$arr, $data, $pos) {
$array = array_slice($arr, 0, $pos) + $data +
array_slice($arr, $pos);
}

$a = array('a' => 'apple', 'b' => 'banana', 'o' => 'orange');
array_insert($a, array('p' => 'pear'), 2);

print_r($a);
// OUTPUT:
// Array ( [a] => apple [b] => banana [p] => pear [o] => orange )
?>

Regards

Hamish
Re: Inserting into associative array [message #169926 is a reply to message #169904] Thu, 30 September 2010 12:12 Go to previous messageGo to next message
Jonathan Stein is currently offline  Jonathan Stein
Messages: 43
Registered: September 2010
Karma: 0
Member
Den 29-09-2010 22:05, Michael Fesser skrev:

> Nope. But maybe you could explain why you need such a special order,
> which can't be achieved with the given array sorting functions.

In a Drupal module, I need to modify an array, that is later used to
build an input form - and the input fields will appear in the same order
as the elements in this associative array.

Regards

Jonathan
Re: Inserting into associative array [message #169928 is a reply to message #169905] Thu, 30 September 2010 12:17 Go to previous messageGo to next message
Jonathan Stein is currently offline  Jonathan Stein
Messages: 43
Registered: September 2010
Karma: 0
Member
Den 29-09-2010 23:07, Hamish Campbell skrev:

> function array_insert(&$arr, $data, $pos) {

Thank you for your suggestion. If the number of elements is fixed, it's
a bit more elegant than looping through the array.

Regards

Jonathan
Re: Inserting into associative array [message #169995 is a reply to message #169903] Mon, 04 October 2010 03:25 Go to previous message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Wed, 29 Sep 2010 21:31:54 +0200, Jonathan Stein wrote:
> Hi.
>
> Let's say I have an array like
> array(
> 'fruit' => 'banana',
> 'color' => 'yellow'
> )
>
> Now I want to insert
> 'shape' => 'round'
> between 'fruit' and 'color'. I would think that PHP had a simple
> solution for that, but I haven't found it... My solution is to loop
> through the original array and build a new one.
>
> Does anyone have a better solution?

Why do you care where it is? It's an associative array. You access it by
keys.

--
"Meet it is I set it down
That one may smile, and smile, and be a villain"
Re: Inserting into associative array [message #169996 is a reply to message #169926] Mon, 04 October 2010 03:25 Go to previous message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Thu, 30 Sep 2010 14:12:48 +0200, Jonathan Stein wrote:
> Den 29-09-2010 22:05, Michael Fesser skrev:
>
>> Nope. But maybe you could explain why you need such a special order,
>> which can't be achieved with the given array sorting functions.
>
> In a Drupal module, I need to modify an array, that is later used to
> build an input form - and the input fields will appear in the same order
> as the elements in this associative array.

Ah. If you can arrange your keys to be sortable, you might find ksort()
handy. If you want them in an arbitrary order, you're going be doing a
lot more work...

--
The Write Many, Read Never drive. For those people that don't know
their system has a /dev/null already.
-- Rik Steenwinkel, singing the praises of 8mm Exabytes
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PHP 5.3.3 Crashes on Win7
Next Topic: Dot in array key name
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 16:49:08 GMT 2024

Total time taken to generate the page: 0.03148 seconds