-1

I have an array called $eduAdd and I'm adding a string to the beginning of it like this:

array_unshift($eduAdd, $userId);

This is the result:

Array
(
    [0] => 3
    [fieldId] => Array
        (
            [0] => 4
        )

    [educationTitle] => Array
        (
            [0] => g
        )

    [educationDegree] => Array
        (
            [0] => g
        )

)

How can I rename [0] => 3 to ['userId'] => 3

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • http://stackoverflow.com/questions/5783750/php-add-item-to-beginning-of-associative-array – Dave Mar 29 '13 at 22:03

2 Answers2

2

Instead of using array_unshift just use $eduAdd['userId'] = $userId in the first place;

Musa
  • 96,336
  • 17
  • 118
  • 137
1

If it needs to be the first element (although pointless in an array), try using array_merge() -

$userId['userId'] = $userId
$eduAdd = array_merge($userId,$eduAdd);

phpfiddle - http://phpfiddle.org/main/code/1m0-yfn

Sean
  • 12,443
  • 3
  • 29
  • 47