我在php上有一个数组数组,例如:
array(253) {
[0]=>
array(6) {
[0]=>
string(10) "2018/03/07"
[1]=>
float(175.03)
[2]=>
float(31686450)
[3]=>
float(174.94)
[4]=>
float(175.85)
[5]=>
float(174.27)
}
[1]=>
array(6) {
[0]=>
string(10) "2018/03/06"
[1]=>
float(176.67)
[2]=>
float(23273160)
[3]=>
float(177.91)
[4]=>
float(178.25)
[5]=>
float(176.13)
}
[2]=>
array(6) {
[0]=>
string(10) "2018/03/05"
[1]=>
float(176.82)
[2]=>
float(27825140)
[3]=>
float(175.21)
[4]=>
float(177.74)
[5]=>
float(174.52)
}
[3]=>
array(6) {
[0]=>
string(10) "2018/03/02"
[1]=>
float(176.21)
[2]=>
float(38426060)
[3]=>
float(172.8)
[4]=>
float(176.3)
[5]=>
float(172.45)
}
对于每个子数组,我想用键e替换0,1,2,3:
[0] => Array
(
[2] => Array
(
["date"] => 2018/03/05
["price"] => 176.82
["volume"] => 27825140
["open"] => 175.21
["close"] => 177.74
["yyy"] => 174.52
)
我尝试使用:
$array1 = array(
"a" => "first",
"b" => "second",
"c" => "something",
"d" => "something",
"e" => "something",
"f" => "something",
);
print_r(array_fill_keys($array1, $new_array));
但它刚刚回归:
[0] => Array
(
[0] => 2018/03/07
[1] => 175.03
[2] => 31686450
[3] => 174.94
[4] => 175.85
[5] => 174.27
)
[1] => Array
(
[0] => 2018/03/06
[1] => 176.67
[2] => 23273160
[3] => 177.91
[4] => 178.25
[5] => 176.13
)
[2] => Array
(
[0] => 2018/03/05
[1] => 176.82
[2] => 27825140
[3] => 175.21
[4] => 177.74
[5] => 174.52
)
[3] => Array
(
[0] => 2018/03/02
[1] => 176.21
[2] => 38426060
[3] => 172.8
[4] => 176.3
[5] => 172.45
)
任何想法如何解决?
对于每个子数组,我想添加上述键,以便我可以通过$ array [0] [" price"];
检索它............................................... .................................................. .................................................. ....... .................................................. .................................................. .................................................. .... .................................................. .................................................. .................................................. ....
答案 0 :(得分:0)
你可以这样做:
<?php
$myArray = array(array('string', number_val), array('string_2', number_val_2));
foreach ($myArray as $k => $v)
{
$myArray[$k][$v]['my_key'] = $v[4] # change to correct array index to match desired key
}
答案 1 :(得分:0)
遍历数组并将所需的键与每组值组合:
$output = [];
foreach ($array as $a) {
$output[] = array_combine($keys, $a);
}
array_map()
答案 2 :(得分:0)
让我们调用你的第一个数组 $ mainArray ,然后代码将是:
$res= [];
foreach ($mainArray as $a) {
$res[] = array_combine($array1 , $a);
}
答案 3 :(得分:0)
您需要获取子数组。循环或地图:
$array = array_map(function($v) {
return array_combine(["date","price","volume","open","close","yyy"], $v);
}, $array);