-2
array:18 [▼
      "_token" => "D0bWBXif0Dfhe0X5xecliP3NkRKhvYUmjKUzbw2G"
      "domain" => "example.io"
      "promocode" => "123"
      "firstname" => "Jon"
      "lastname" => "123"
      "email" => "e@outlook.com"
      "phonenumber" => "07572070798"
      "address1" => "2 Wayworld"
      "country" => "UK"
      "city" => "Nr"
      "state" => "Bournemouth"
      "postcode" => "dy1 0tj"
      "password2" => "demo10!"
      "securitycode" => "123"
      "card_number" => "123"
      "card_expiry" => "123"
      "card_cvv2" => "s"
      0 => array:1 [▼
        "clientid" => 94
      ]
    ]

I am trying to add the 'clientid=>94 to the first array, not create a new array inside of that array, I have tried array push and can't get it to work. what should I do?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Charlotte Wells
  • 375
  • 1
  • 2
  • 8

3 Answers3

1

You can use Laravel helper for that

// Syntax
$array = array_add($array, 'key', 'value');
// code
$array = array_add($response, 'clientid', '94');

Check array_add() in laravel

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

You are looking for how to flatten your array. You can see here for some methods.

But for your quick need (and in case you don't actually want to flatten everything), you can just do

$firstArray['clientid'] = $firstArray[0]['clientid'];

(Provided that the client id array will always have the index 0)

(Also, $firstArray stands for whatever you call your big array.)

Prince Dorcis
  • 955
  • 7
  • 7
0
// Syntax
$array = array_add($array, 'key', 'value');
// code
$array = array_add($response, 'clientid', '94');

Was the best solution! I just found this Helper right after seeing this :)

Charlotte Wells
  • 375
  • 1
  • 2
  • 8