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.
Use foreach and implode to parse the array then write code to organize the result string
[How create an array from the output of an array printed with print_r? check the link, you will find your solution, here i found the question similar to yours..
You can use this code for above solution
$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'];
}
}
$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");
}
}