我有一个这样的数组:
Array (
[0] => 10060127
[1] => 10065127
[2] => 10070127
[3] => 10075127
)
我想根据数组中的值添加关联数组。例如找到10070127和一个包含各种其他信息的关联数组。类似的东西:
[1] => 10065127 => Time : 10:00
Date : 16/12/2014
Count : 1
如何识别正确位置并将这些项目推送到此阵列?
答案 0 :(得分:0)
你可以试试这个:
$dataArray = array(
0 => 10060127,
1 => 10065127,
2 => 10070127,
3 => 10075127
);
$toAdd = array(
1 => array(
10065127 => array("Time" => '10:00',
"Date" => '16/12/2014',
"Count" => '1'
)
),
2 => array(
10070127 => array("Time" => '17:25',
"Date" => '11/12/2014',
"Count" => '95'
)
)
);
foreach ($toAdd as $subArray) {
$toSearch = key($subArray);
$pos = array_search($toSearch, $dataArray);
if ($pos !== false) {
unset($dataArray[$pos]);
$dataArray[$toSearch] = $subArray[$toSearch];
}
}
var_dump ($dataArray);
输出将是:
array
0 => int 10060127
3 => int 10075127
10065127 =>
array
'Time' => string '10:00' (length=5)
'Date' => string '16/12/2014' (length=10)
'Count' => string '1' (length=1)
10070127 =>
array
'Time' => string '17:25' (length=5)
'Date' => string '11/12/2014' (length=10)
'Count' => string '95' (length=2)
答案 1 :(得分:0)
类似的东西:
$id = '10070127';
$array[array_search($id, $array)] = array($id=>array(
'time'=>'10:00',
'Date'=>'16/12/2014',
'Count'=>1));