-1

I have created empty array and want to push all values and its key to the new created array but I am getting error array_push() expects at least 2 parameters I know array_push needs two parameter and here I am passing only one but what I ant is all keys and values to be pushed directly to array

//   Here 'userid' is just text or can say sample key and 
 //  Here $userid is getting from table so expected output

 //   'userid'=>$userid

$temp = array();

  array_push($temp['userid'] = $userid);
Anonymous
  • 1,074
  • 3
  • 13
  • 37

2 Answers2

2

To push to an array using a key - value pair, you do not need to use array_push.

array_push expects the array, and the value (no key) that you're pushing.

To push to an array using a key - value pair simply do :

$temp['user_id'] = $userid;
cmprogram
  • 1,854
  • 2
  • 13
  • 25
1

To use array_push you must give it the original array (modified by reference) and the new entry (value) for it. In this case since you have a key as well, you'd need to merge your arrays:

$temp = array_merge($temp, ['userid' => $userid]);

However, you can use simple array syntax instead to achieve the same thing:

$temp = array();
$temp['userid'] = $userid;
scrowler
  • 24,273
  • 9
  • 60
  • 92