这是我的数据库结果
Array ([0] => Array ( [shopname] => Shop name [fueltype] => Pertol [amount] => 1000 )
[1] => Array ( [shopname] => dfsdfsd [fueltype] => Pertol [amount] => 54456 )
[2] => Array ( [shopname] => dfsdfsd [fueltype] => Disel [amount] => 54456 )
)
我需要像
这样的结果[["Shop name", "Pertol", 1000],["dfsdfsd", "Pertol", 54456],["Shop name", "Disel", 54456]]
怎么这样,我不知道?
答案 0 :(得分:2)
$mapped = array_map('array_values', $input_array); // apply filter so we dont get the keys
$json = json_encode($mapped);
答案 1 :(得分:2)
array_map()
以及array_values()
将适合您: -
<?php
$array = Array ( '0' => Array ( 'shopname' => 'Shop name','fueltype' => 'Pertol','amount' => 1000 ),
'1' => Array ( 'shopname' => 'dfsdfsd' ,'fueltype' => 'Pertol','amount' => 54456 ),
'2' => Array ( 'shopname' => 'dfsdfsd','fueltype' => 'Disel','amount' => 54456 )
);
$values_data_only = array_map('array_values', $array);
$desire_result = json_encode($values_data_only);
echo $desire_result;
?>
输出: - https://eval.in/395344
同样可以通过简单的foreach()
: -
<?php
$array = Array ( '0' => Array ( 'shopname' => 'Shop name','fueltype' => 'Pertol','amount' => 1000 ),
'1' => Array ( 'shopname' => 'dfsdfsd' ,'fueltype' => 'Pertol','amount' => 54456 ),
'2' => Array ( 'shopname' => 'dfsdfsd','fueltype' => 'Disel','amount' => 54456 )
);
$new_array = array();
foreach ($array as $k=> $arr){
$new_array[$k][] = $arr['shopname'];
$new_array[$k][] = $arr['fueltype'];
$new_array[$k][] = $arr['amount'];
}
echo "<pre/>";print_r($new_array);
$desired_result_2 = json_encode($new_array);
echo $desired_result_2;
?>
输出: - https://eval.in/395354
答案 2 :(得分:0)
试试:
$input = array( /* your input data */ );
$output = array();
foreach ($input as $data) {
$output[] = array_values($data);
}