我有下面给出的数组
Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => App key [value] => wlvii0k1bdm5lk8 )
Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => App secret [value] => eui9d7p62qvxt78 )
Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => Accesstoken [value] => IwSMaEYuDMAAAAAAAAAAC5jf8SyEXQUdf65xCrzFDbqrtlVyPQoJ7OBV1BRyHqqp )
Array ( [id] => 2 [businessunit_id] => 2 [service_name] => s3 [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => AWS_ACCESS_KEY_ID [value] => xxx )
Array ( [id] => 2 [businessunit_id] => 2 [service_name] => s3 [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt [attribute] => AWS_SECRET_ACCESS_KE [value] => xxxxxx )
我希望它转换如下
Array ( [id] => 1 [businessunit_id] => 1 [service_name] => dropbox [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt array([App key] =>wlvii0k1bdm5lk8, [App secret] =>eui9d7p62qvxt78, [Accesstoken] =>IwSMaEYuDMAAAAAAAAAAC5jf8SyEXQUdf65xCrzFDbqrtlVyPQoJ7OBV1BRyHqqp ))
Array ( [id] => 2 [businessunit_id] => 2 [service_name] => s3 [file_path] => /home/john/Desktop/GoogleDrive/alfresco_details.txt array([AWS_ACCESS_KEY_ID] => xxx, [AWS_SECRET_ACCESS_KE] =>xxxxxx) )
无法获得预期的结果。你能不能请一些人帮帮我
这是我的PHP代码:
while($row = $result->fetch_assoc()) {
$curid = $row['id'];
//$pastid = $curid;
echo '<pre>';
//print_r($row);
if($i == 0){
$newarray[$i]['id'] = $row['id'];
$newarray[$i]['businessunit_id'] = $row['businessunit_id'];
$newarray[$i]['file_path'] = $row['file_path'];
$newarray[$i]['service_name'] = $row['service_name'];
$newarray[$i][$row['attribute']] = $row['value'];
} else {
if($newarray[$i-1]['id'] == $curid){
$newarray[$i-1][$row['attribute']] = $row['value'];
$newarray[$i]['id'] = $row['id'];
} else{
$newarray[$i]['id'] = $row['id'];
$newarray[$i]['businessunit_id'] = $row['businessunit_id'];
$newarray[$i]['file_path'] = $row['file_path'];
$newarray[$i]['service_name'] = $row['service_name'];
$newarray[$i][$row['attribute']] = $row['value'];
}
}
$i++;
}
答案 0 :(得分:0)
基本上,您正在尝试对数组进行区分,并将diff结果与三个数组的交叉结合起来。
//untested code sample
function reformatArrays($arrays)
{
$diff = [];
$a = reset($arrays);
$inter = [];
foreach($array as $k => $v)
{
$diff[] = array_diff($a, $v);
$inter = array_intersect($a, $v);
$a = $v;
}
return $inter + $diff;
}
答案 1 :(得分:0)
试试这个,其中$ origin是你当前的数组:
$result = array();
foreach($origin as $a)
{
if(!isset($result[$a['id']]))
{
$result[$a['id']] = array('id' => $a['id'], 'businessunit_id' => $a['businessunit_id'], 'service_name' => $a['service_name'], 'file_path' => $a['file_path'] );
}
$result[$a['id']]['appvalues'][$a['attribute']] = $a['value'];
}