PHPLover PHPLover - 3 months ago 9
PHP Question

How to explicitly define an associative array with key-value pairs in PHP?

I'm having an array called

$enquiries_labels
. It's a static array. So, I've defined it as follows:

$enquiries_labels = array("New Enquiry","Retail Enquiry","Feedback","Payment Query","Package Query","Test Query");


But instead of this I want to define an associative array explicitly with keys starting from 0 and the associated values should be in above sequence. Can anyone help me in defining such associative array with key-value pairs explicitly?

Answer

Generate the array like this.

$enquiries_labels = array("Key1" => "New Enquiry", "Key2" => "Retail Enquiry","Key3" =>  "Feedback", "Key4" => "Payment Query","Key5" => "Package Query", "Key6" => "Test Query");

Write down Key you want here.

$enquiries_labels = array("New Enquiry","Retail Enquiry","Feedback","Payment Query","Package Query","Test Query");

This array will generate key from 0 onward & you will get keys and values like this.

foreach ($enquiries_labels as $key => $value) {
   echo $key;
   echo $value;
}