I have a simple table with 2 colums: ID and Name. It looks like this:
License_ID License_Name
5 xxx
8 yyy
13 zzz
I want to write this table as an array, and I want to be able to find the license_id when license_name is provided. How can I write this array?
Edit: I can write the array the same as the answer nickb provided,
$array = array(
'xxx' => 5,
'yyy' => 8,
'zzz' => 13
);
but I want to have the license_id and license_name as key instead of using value of license_name as key.
Form an array with keys corresponding to the license name and values corresponding to the license ID, like so:
To omit the table and use a global variable, use the same technique:
Now, to lookup the license ID, all you need is:
Edit: Here’s an alternative way to represent the way in a more clear manner:
Then, to find the license_id when license_name is provided, loop to find your result:
OR, and even more direct way to prevent looping is to modify the above technique to have each sub-array contain the license_name as its key, like so:
Now, to find the license_id, you can do it directly once you know the license_name: