Answer the question
In order to leave comments, you need to log in
How to make an array value a key in php?
There is an array like
$arr = [
'Model' => 'OPPO',
'Price' => '300$',
'Year' => '2019',
];
$arr = [
'OPPO' =>[
'Price' => '300$',
'Year' => '2019',
]
];
$arr = [
'Model' => 'OPPO',
'Price' => '300$',
'Year' => '2019',
];
$item = [];
foreach ($arr as $key => $value)
{
if ($key == 'Model')
{
$item[($value)] = [];
continue;
}
else
{
$item[($key)] = $value;
}
var_dump($item);
}
Answer the question
In order to leave comments, you need to log in
$arr = [
'Model' => 'OPPO',
'Price' => '300$',
'Year' => '2019',
];
$key = array_shift($arr);
$new = [$key => $arr];
echo '<pre>' . print_r($new, true) . '</pre>';
/*Array
(
[OPPO] => Array
(
[Price] => 300$
[Year] => 2019
)
)*/
$key = 'Model';
$result = [];
if (key_exist($key, $arr)) {
$keyVal = $arr[$key];
$result[$keyVal] = $arr;
unset($result[$keyVal][$key]);
}
use the native array_flip function, don't write a bike
https://www.php.net/manual/ru/function.array-flip.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question