我需要将一个数组组织成一个字符串。
我有这个数组:
array(27) {
[0]=>
array(3) {
["tp__string"]=>
string(3) "AA,"
["cost"]=>
string(16) "515.771314999996"
["count"]=>
int(47)
}
[1]=>
array(3) {
["tp__string"]=>
string(3) "BB,"
["cost"]=>
string(11) "2718.860891"
["count"]=>
int(281)
}
[2]=>
array(3) {
["tp__string"]=>
string(3) "CC,"
["cost"]=>
string(16) "619.105467999996"
["count"]=>
int(44)
}
[3]=>
array(3) {
["tp__string"]=>
string(3) "DD,"
["cost"]=>
string(16) "2088.84192300001"
["count"]=>
int(131)
}
[4]=>
array(3) {
["tp__string"]=>
string(3) "EE,"
["cost"]=>
string(12) "12124.710324"
["count"]=>
int(955)
}
[5]=>
array(3) {
["tp__string"]=>
string(3) "BB,"
["cost"]=>
string(10) "1543.73578"
["count"]=>
int(164)
}
[6]=>
array(3) {
["tp__string"]=>
string(3) "CC,"
["cost"]=>
string(16) "319.932651999999"
["count"]=>
int(26)
}
这就是我需要组织数据的方式
所以我必须创建一些字符串:echo '515.771314999996', '47', 'NULL','NULL','NULL','NULL';
- 第二行的例子。
密钥可以更改,也可以更改列数和行数 提取数据的最简单,最快捷的方法是什么?
修改
要创建第一行,我有:
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
else {$return[$key] = $value;}
}
return $return;
}
function unset_num_keys($array){
$array_out = array();
foreach($array AS $k => $v)
{
if(is_array($v))
{
$array_out[$k] = unset_num_keys($v);
}
elseif(!is_numeric($k))
{
$array_out[$k] = $v;
}
}
return $array_out;
}
$arr = (unset_num_keys($aaa));
$tmp = array();
foreach($arr as $x){
array_push($tmp, (array_keys($x)));
}
$titles = (array_unique(array_flatten($tmp)));
$first_value = reset($titles);
$firstColum = array();
foreach($titles as $title){
foreach($arr as $a){
array_push($firstColum, $a[$first_value]);
}
}
$first_line = "['x', ";
end($arr);
$total = key($arr);
$i = 0;
$firlsColum_clean= (array_unique(array_flatten($firstColum)));
foreach ($firlsColum_clean as $first) {
$new = str_replace(',', '', $first);
if ($i == $total) {
$first_line .= $new;
} else {
$first_line .= $new . ', ';
}
$i += 1;
}
$first_line .= "],";
但是我很难理解下一行的逻辑。
答案 0 :(得分:1)
使用foreach和implode解析数组,然后编写代码来组织结果字符串
答案 1 :(得分:0)
[How create an array from the output of an array printed with print_r? 检查链接,你会找到你的解决方案,在这里我发现了类似你的问题..
答案 2 :(得分:0)
您可以将此代码用于上述解决方案
$array1=array();
foreach($array as $key=>$value)
{
//put the value into X field
echo $value['cost'];
if(in-array($value['tp_string'],$array1))
{
//put the value of tp_strings to corrosponding column and all other put null value
echo $value['tp_strings'];
}
else
{
array_push($value['tp_string'],$array1);
//create new coloumn and put value into new column and all other put null value
echo $value['tp_strings'];
}
}
答案 3 :(得分:0)
$ar = array() // your data here
$result = array() // this is what we will use later
$head = array() // this is your column headers
// make your data make sense
foreach($ar as $item) {
$result[$item['cost']][$item['tp_string']]=$item['count'];
if(!in_array($item['tp_string'], $head)) $head[] = $item['tp_string'];
}
// sort the columns
sort($head);
// print your headers
print 'X';
foreach($head as $col) print ",{$col}";
// print your data
foreach($result as $cost=>$data) {
print PHP_EOL;
print $cost;
// print the row data
foreach($head as $col) {
print ',' . (isset($data[$col])) ? $data[$col] : "NULL");
}
}