array key and value php code example
Example 1: get key by value array php
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array);
$key = array_search('red', $array);
?>
Example 2: array key value php
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Example 3: assign $variable of key value pair array to multiple variables php
$healthStructureData = [
'product' => $product,
'website_data' => $website_data,
'total_keywords' => $total_keywords,
'certificate_status' => $certificate_status,
'total_user_websites' => $total_user_websites,
];
foreach ($healthStructureData as $key => $value) {
$$key = $value;
}
Example 4: php variables as keys in arrays
$id = 1;
$age = 2;
$sort = "id";
$Key = $$sort;
$arr = array($Key => 'string');
print_r($arr);