0

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?

  • 1
    Check this link. http://php.net/manual/en/function.array-push.php – Virb May 07 '18 at 05:22
  • I checked. Without using array push, it doesn't give me warning. But to use array push with key and value, they wrote this same thing. Also see the ans here - https://stackoverflow.com/questions/2121548/how-to-push-both-value-and-key-into-array used same method. But how come I get a warning? Could you please explain? –  May 07 '18 at 05:26
  • Check my answer below. I think you didn't check properly. – Virb May 07 '18 at 05:30
  • Have you tried just doing `$post_likes[$current_user->ID] = $new_likes;` without the `array_push()` at all? That will add a new key (the user ID) to the array with the `$new_likes` as value. – M. Eriksson May 07 '18 at 05:31
  • Yes I tried and it worked. Check my previous comment. @Magnus Eriksson I just wanted to know why this warning –  May 07 '18 at 05:33
  • Well, the manual explains why (you can't expect a function to work if you don't send in all the required arguments). Anyway, you shouldn't use array_push() here, since that's the wrong function for the job. – M. Eriksson May 07 '18 at 05:37
  • Thank you. Won't use array_push for a single key any more. @Magnus Eriksson –  May 07 '18 at 05:39

2 Answers2

2

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
-1

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;
Virb
  • 1,639
  • 1
  • 16
  • 25
  • 1
    This might work, but it will throw a Notice, since `array_push()` wants the the first argument to be passed as reference (and there for wants an array, not the result of an expression). – M. Eriksson May 07 '18 at 05:36
  • 1
    @Magnus Eriksson Didn't show any notice. But I am still wondering, why! –  May 07 '18 at 05:37
  • 1
    @AsifurRahman - If it doesn't show a notice, it's because your error reporting hides notices. Demo: https://3v4l.org/ot2Xu. This is the _wrong_ way of doing it. You should accept Alivetodies's answer instead, since that's the correct way. – M. Eriksson May 07 '18 at 05:40
  • @Magnus Eriksson Oh didn't try it outside my environment. Thank you. –  May 07 '18 at 05:41