What does => mean in php? [duplicate] - php

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
I'm learning about arrays and am not certain what => means or does.

Assignment of a value to a named key.
http://www.php.net/manual/en/language.operators.assignment.php
$example = array('color' => 'blue');
echo $example['color']; //prints blue

I believe it is just syntax for writing array literals that behave like maps / dictionaries.
$mydict = array('hello' => 'world');
The above is an array with one element. But instead if indexing that element with an integer index, you use the key 'hello':
echo $mydict['hello']

Related

How to create a function finding the last key of an array using value PHP in different ways [duplicate]

This question already has answers here:
PHP - Get key name of array value
(9 answers)
Closed 4 years ago.
How to get the Cat by using the value Nap in array
["Dog" => "Bite", "Cat" => "Nap"]
and i want to get Cat using value Nap
$key = array_search('Nap', $array);
What is not clear in your question is if you already know the entry is the last entry, or if you're searching for a value that is somewhere in your array.
If you have PHP 7.3, and you know it's the last entry, you can use array_key_last() (see http://php.net/manual/en/function.array-key-last.php )
$key = array_search('Nap', $array); (as mentionned by #Sanu0786 )

Get nth key of associative php array [duplicate]

This question already has answers here:
Accessing an associative array by integer index in PHP
(6 answers)
Closed 7 years ago.
I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array.
Example:
$array = array('customer' => 'Joe', 'phone' => '555-555-5555');
What I'm building is super-dynamic, so I do NOT know the second entry will be 'phone'. Is there an easy way to grab it?
In short, (I know it doesn't work, but...) I'm looking for something functionally equivalent to: key($array[1]);
array_keys produces a numerical array of an array's keys.
$keys = array_keys($array);
$key = $keys[1];
If you're using PHP 5.4 or above, you can use a short-hand notation:
$key = array_keys($array)[1];

How do PHP arrays actually work? [duplicate]

This question already has answers here:
Are PHP Associative Arrays ordered?
(4 answers)
Closed 8 years ago.
I'm learning PHP and I've got a question that's bothering me. PHP arrays seem to be hashmaps internally. If you give an array a key and value, it almost certainly has to put the key through some sort of hashing function before placing it in an actual array, right? Why then, if I give an array a series of keys and values and then dump these to screen, does PHP maintain the order in which I entered the values?
for instance:
$arr = array();
$arr[1] = 'one';
$arr[3] = 'three';
$arr[2] = 'two';
foreach($arr as $key => $val)
echo "$key => $val<br>"
would render "1 => one, 2 => two, 3 => three" in a typical hashmap, but instead I get "1 => one, 3 => three, 2 => two." Which to me means that there have to be both and order and a key being maintained in whatever datatype this actually is.
Thanks in advance for any explanation.
You are correct about the array being stored as a hash table or ordered map. Basically, everything in PHP is a hash table.
See here: Understanding PHP's internal array implementation

Understanding array output [duplicate]

This question already has answers here:
Characters allowed in php array keys?
(11 answers)
Closed 8 years ago.
I am studying PHP and came across a question like this:
What is the output of the following array?
Code:
$a = array(0.001 => 'b', .1 => 'c');
print_r($a);
The answer is 0 => 'c' - now I know array keys can't be numbers but wouldn't that throw an error? Why is the first element overwritten?
From the documentation on arrays:
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
and, as Alex points out below:
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

What is this operator "=>"? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
Reference - What does this symbol mean in PHP?
I see this symbol in a lot of PHP code.
I can't figure out what it means or what it does..
Is it really an operator ?
Do you mean =>? If so, it's for initializing array keys (or indexes if you prefer). Like this:
$values = array(
'foo' => 'bar'
);
This will initiazlie an array with a key named foo with a value of bar.
Read more on about arrays at php.net.
http://php.net/manual/en/language.operators.php

Categories