如何正确使用array_push作为另一个数组的键和值

问题描述:

So I am using this code to push new user IDs into an array but I get

Warning: array_push() expects at least 2 parameters, one given

$post_likes = array(
"Some key" => array(
             'date' => date("d/m/Y"), 
             'IP' => get_client_ip())
             ); 
$new_likes = array(
             'date' => date("d/m/Y"), 
             'IP' => get_client_ip());
array_push($post_likes[$current_user->ID] = $new_likes);

The code works. It pushes new key with array value into the previous array. But still I get that warning. What am I missing?

所以我使用这段代码将新的用户ID推送到一个数组中,但是我得到了 p> \ n

警告:array_push()需要至少2个参数,一个给定 p> blockquote>

  $ post_likes = array(
“ 一些关键的“=>数组(
'日期'=>日期(”d / m / Y“),
'IP'=> get_client_ip())
);  
 $ new_likes = array(
'date'=> date(“d / m / Y”),
'IP'=> get_client_ip()); 
array_push($ post_likes [$ current_user->  ID] = $ new_likes); 
  code>  pre> 
 
 

代码有效。 它将具有数组值的新键推送到前一个数组中。 但我还是得到了警告。 我错过了什么? p> div>

Instead of using array_push() you can directly do like this-

$post_likes[$current_user->ID] = $new_likes;

A sample hardcoded example:- https://eval.in/1000261

Set your new variable $new_likes as like this:

array_push($post_likes[$current_user->ID] = $new_likes,$new_likes);   //expects at least 2 parameters

More info : http://php.net/manual/en/function.array-push.php

Updated:

I am proffering and suggesting to use @Magnus Eriksson's answer though you have accepted my answer.

So, just use as like below:

$post_likes[$current_user->ID] = $new_likes;