Say I have an array like this:
$result=array("1", "2", "34");
And I have another array:
$keys=array("id", "price", "day");
As it is, $result
has numeric keys for each of its values. I want to add to those values associative keys, using the values in $keys
as such, but without deleting the numeric keys. That is, I want the result to be:
array(6)
{ [0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(2) "34"
["id"]=> string(1) "1"
["price"]=> string(1) "2"
["day"]=> string(2) "34"
}
I know that I could write a itty-bitty function to do this by hand, but is there any built-in function, among the myriad ones in PHP, to do this automatically? (My PHP knowledge is a bit rusty).
You can do it like this:
$result=array("1", "2", "34");
$keys = array("id", "price", "day");
$array = $result + array_combine($keys, $result);
If you considered using array_merge
, I recommend this thread: Array_merge versus +
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.