I need to organize a array I have into a string.
I have this array:
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)
}
This is how i need to have data organized
So I have to create some string like: echo '515.771314999996', '47', 'NULL','NULL','NULL','NULL';
- example of the second line.
The Keys can change, and also the number of columns and rows.
What is the easiest and fastest way to extract the data?
EDIT
To create first line I have this:
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 .= "],";
But I'm having troubles understanding the logic of the next lines.