1.

How to add key and value in array in php?

Answer»

In PHP, pushing a VALUE inside an ARRAY automatically creates a numeric key for itself. When you are adding a value-key pair, you actually already have the key, so, pushing a key again does not make sense. You only NEED to set the value of the key inside the array. Here’s an example to correctly PUSH a [air pf value and key inside an array:

$data = array(
   "name" => "Best Interview Questions"
);

array_push($data['name'], 'Best Interview Questions');

Now, here to push “cat” as a key with “wagon” as the value inside the array $data, you need to do this:

$data['name']='Best Interview Questions';



Discussion

No Comment Found