In PHP I create an array of key value pairs like this, how to I get "mykey"?
$arr = array("mykey"=>"myvalue");
I know the code below will work, but I am interested to know if there is a language construct in PHP that allows me to this in a easier fashion.
foreach($arr as $key=>$value){
$result = $key;
break;
}
echo $result;
Just Use key if you dealing with one element and array_keys with arrays with multiple values.
Example
$arr = array("mykey"=>"myvalue");
var_dump(key($arr));
Or
var_dump(array_keys($arr));
Output
string 'mykey' (length=5)
and
array
0 => string 'mykey' (length=5)
try this
you can use array_keys function in php
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
you need to use array_keys, here's the manual
Related
Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?
The array_intersect PHP function compares array elements with === so I do not get the expected result.
For example, I want this code :
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).
Any solution ?
Thank you in advance
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
<?php
function to_lower_and_without_tildes($str,$encoding="UTF-8") {
$str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
return $str;
}
function compare_function($a,$b) {
return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);
output:
Array
(
[a] => gréen
[0] => red
)
Assume that I have two arrays as follow:
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
How to check that the two arrays are exactly the same in most efficient and proper way and it doesn't care the keynames of the *$array2.
I want to make a function which should return true if values are exactly the same, and false if any of the ones are different both in value(s) and number of elements.
Thanks for your time and reading.
In the simplest case you can just use array_diff. It ignores the keys in your second array, but also the order of the values. It would return an empty set if the arrays are equal:
if (count(array_diff($array1, $array2)) == 0) {
// equal
You could also compare the arrays directly, after stripping keys from the second:
if ($array1 == array_values($array2)) {
That would additionally compare the order of contained values.
array_values($array1) === array_values($array2)
Assuming that arrays have same order.
Try this
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
$array2 = array_values($array2);
echo $array1 == $array2 ? 'true' : 'false';
array_diff will do the job for you:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(empty($result)){
// arrays contain the same values!
}
?>
Create a class containing an array and make that class implement the Comparable interface, for example http://php.net/manual/language.oop5.interfaces.php#69467
like this:
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(count($result) == 0)
{
.......
}
?>
You can easily get an array value by its key like so: $value = array[$key] but what if I have the value and I want its key. What's the best way to get it?
You could use array_search() to find the first matching key.
From the manual:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
You can use the array_keys function for that.
Example:
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
This will get the key from the array for value blue
$arr = array('mango', 'orange', 'banana');
$a = array_flip($arr);
$key = $a['orange'];
No really easy way. Loop through the keys until you find array[$key] == $value
If you do this often, create a reverse array/hash that maps values back to keys. Keep in mind multiple keys may map to a single value.
Your array values can be duplicates so it wont give you exact keys. However the way i think is fine is like iterate over and read the keys
I want to create separate variables from the key of an array where variable is the key and the content is the value of that key in the array
extract($array);
Example
<?php
$array = array('a' => 'abc', 'b' => 'def');
extract($array);
var_dump($a, $b);
// string(3) "abc"
// string(3) "def"
See it.
using this way
$data=array();
$data["value1"]=3;
$data["value2"]=4;
$three = $data["value1"];
$four = $data["value2"];
Use the extract() function for this.
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array);
which will give:
$color = 'blue'
$size = 'medium'
$shape = 'sphere'
use
extract($array);
PHP's extract function will do exactly that.
You can easily get an array value by its key like so: $value = array[$key] but what if I have the value and I want its key. What's the best way to get it?
You could use array_search() to find the first matching key.
From the manual:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
You can use the array_keys function for that.
Example:
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
This will get the key from the array for value blue
$arr = array('mango', 'orange', 'banana');
$a = array_flip($arr);
$key = $a['orange'];
No really easy way. Loop through the keys until you find array[$key] == $value
If you do this often, create a reverse array/hash that maps values back to keys. Keep in mind multiple keys may map to a single value.
Your array values can be duplicates so it wont give you exact keys. However the way i think is fine is like iterate over and read the keys