0

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

enter image description here

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.

Henrique Ferrolho
  • 912
  • 1
  • 10
  • 30
62009030
  • 347
  • 1
  • 5
  • 20

4 Answers4

1

Use foreach and implode to parse the array then write code to organize the result string

0

[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..

Community
  • 1
  • 1
ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
0

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'];
      }


}
Domain
  • 11,562
  • 3
  • 23
  • 44
  • So create the aaray of tp_strings check first this tp_strings present in array if present go for inserting value into column otherwise insert this tp_strings to array and put new columns add this new tp_strings to new columns..Check it every time. – Domain Jan 21 '16 at 12:27
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");
  }
}
  • Hi @Shaun. There is a problem. I don't know name of keys. Also it display a lot of Notice: Undefined index: – 62009030 Jan 21 '16 at 13:01
  • Also it doesnt print NULL – 62009030 Jan 21 '16 at 13:10
  • I wrote the code on my phone so you may have to debug a little. NULL should be in single quotes instead of double quotes. :-) I also forgot a couple of semicolons. And I'm not sure how I can help if you don't know the keys. You don't know that each array has a cost, a tp_string, and a count? Are those they keys you are don't know? If that's the case, I'm not sure how I can help. – Shaun Frisbee Jan 21 '16 at 13:15
  • i debug it. only one problem now, cost,tp_string may change , need to figure it out how to do it – 62009030 Jan 21 '16 at 14:44