-4

I want to push new data in array which each value of them.

$array = array("menu1" => "101", "menu2" => "201"); 
array_push($array, "menu3" => "301");

But I got an error syntax.

And if I use like this :

$array = array("menu1" => "101", "menu2" => "201"); 
array_push($array, "menu3", "301");

result is : Array ( [menu1]=>101  [menu2]=>201   [0]=>menu3  [1]=>301 )

My hope the result is : Array ( [menu1]=>101  [menu2]=>201   [menu3]=>301 )

I want push new [menu3]=>'301' but I dont know how. Please help me, the answer will be appreciate

2
  • array_push() doesn't provide the functionality you want. But there are other ways to work with arrays. Have you read the documentation? Commented Feb 11, 2016 at 8:18
  • use array_merge Commented Oct 8, 2016 at 19:04

1 Answer 1

0

You can use

$array["menu3"] = "301"

as for array_push

array_push() treats array as a stack, and pushes the passed variables onto the end of array

so for associative arrays is a no match

another suitable function for what you want but it requires an array argument is array_merge

$result = array_merge(array("one" => "1"), array("two" => "2"));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.