我有一个看起来像这样的数组
"name" => array:3 [
1 => "Hello"
4 => "Test"
21 => "Test2"
]
"runkm" => array:3 [
1 => "100.00"
4 => "1000.00"
21 => "2000.00"
]
"active" => array:3 [
1 => "1"
4 => "0"
21 => "0"
]
我可以以某种方式将匹配的键与PHP函数组合在一起,以便数组看起来像这样
1 => array:3 [
name => "Hello"
runkm => "100.00"
active => "1"
]
4 => array:3 [
name => "Test"
runkm => "1000.00"
active => "0"
]
21 => array:3 [
name => "Test2"
runkm => "2000.00"
active => "0"
]
编辑:感谢所有答案的人。我真正想要的是一个PHP内置功能,我可能应该更清楚。
答案 0 :(得分:1)
$newArr=array();
foreach($array1 as $key => $value){
$newArr[$key]=>array(
'name' =>$value[$key];
'runkm' =>$array2[$key];
'active'=>$array3[$key];
);
}
这是你如何制作一个新阵列,然后打印$ newArr并检查你得到你想要的是什么?祝你好运!
答案 1 :(得分:0)
<?php
$resultarr = array();
for($i=0;$i<count($sourcearr['name']);$i++) {
$resultarr[] = array('name'=>$sourcearr['name'][$i], 'runkm'=>$sourcearr['runkm'][$i], 'active'=>$sourcearr['active'][$i]);
}
答案 2 :(得分:0)
这很有效。而且,不使用硬编码密钥。
<?php
$arr = [
"name" => [
1 => "Hello",
4 => "Test",
21 => "Test2"
],
"runkm" => [
1 => "100.00",
4 => "1000.00",
21 => "2000.00"
],
"active" => [
1 => "1",
4 => "0",
21 => "0"
]
];
// Pass the array to this function
function extractData($arr){
$newarr = array();
foreach ($arr as $key => $value) {
foreach($value as $k => $v) {
if(!isset($newarr[$k]))
$newarr[$k] = array();
$newarr[$k][$key] = $v;
}
}
return $newarr;
}
print_r(extractData($arr));
?>
答案 3 :(得分:0)
我不确定在PHP中是否有一个函数可以执行此操作
<table cellspacing="0">
<tr>
<th>Compulsory Third party* (thisis the minimum insurance cover required in Kenya)</th>
<th>Third party with fire and Theft</th>
<th>Comprehensive Motor Insurance</th>
</tr>
<tr>
<tbody>
<tr >
<td>3rdpartyproperty damage due to your motor vehicle’s involvement</td>
<td>Includes all Compulsory Third party cover plus:</td>
<td>Includes all Compulsory; Fire and Theft plus:-</td>
</tr>
<tr style="background: #70ad47; color:white;" >
<td>3rdParty personal injuries</td>
<td><div class="list">Theft</div></td>
<td><div class="list">Malicious Damage</div></td>
</tr>
<tr >
<td></td>
<td><div class="list">Damage due to Fire</div></td>
<td><div class="list">Accidental Damage to your Vehicle</div></td>
</tr>
</tbody>
</table>
这样做只是循环遍历每个数组及其值,并将其分配给另一个数组,即 $ nArr 。我希望它有所帮助。