php - print array value to txt -
i write program have array printed in txt
file:
$dataset = array(); $dataset[] = array('a','b','c','d'); $dataset[] = array('a','d','c'); $dataset[] = array('b','c'); $dataset[] = array('a','e','c'); $arrlength=count($dataset); for($x=0;$x<$arrlength;$x++){ file_put_contents ('filename.txt', implode(',',$dataset[$x])."\n", file_append); }
the code above produce txt
file:
a,b,c,d a,d,c b,c a,e,c
but when try to print array db
txt
adopting previous code, doesn't give me desired output. here code:
$result=array(); $key='sub'; foreach($this->click_model->getclickstream() $row) { $result[$row['id_click']]['sub'][]= $row['id_kampanye'].$row['code']; $newresult=array_column($result,$key); file_put_contents ('datasetarray.txt', print_r($newresult, true)); $arrlength=count($newresult); for($x=0;$x<$arrlength;$x++){ file_put_contents ('dataset.txt', implode(',',$newresult[$x])."\n", file_append); } }
the code above give me txt
file below:
17p3 17p3,16visitor 17p3,16visitor 5visitor 17p3,16visitor 5visitor,18p3 17p3,16visitor 5visitor,18p3,15visitor 17p3,16visitor
and array $newresult
is:
array ( [0] => array ( [0] => 17p3 [1] => 16visitor [2] => 15visitor [3] => 17p3 [4] => 5visitor ) [1] => array ( [0] => 5visitor [1] => 18p3 [2] => 15visitor [3] => 5visitor [4] => 18p3 [5] => 10visitor ) [2] => array ( [0] => 15visitor [1] => 15visitor [2] => 15visitor [3] => 15visitor [4] => 15visitor [5] => 15visitor [6] => 15visitor [7] => 15visitor [8] => 15visitor ) }
my expected output:
17p3,16visitor,15visitor,17p3,5visitor 5visitor,18p3,15visitor,5visitor,18p3,10visitor 15visitor,15visitor,15visitor,15visitor,15visitor,15visitor,15visitor,15visitor,15visitor
please me, don't know what's wrong code.
use loop outside foreach loop, work you..
<?php $result=array(); $key='sub'; foreach($this->click_model->getclickstream() $row) { $result[$row['id_click']]['sub'][]= $row['id_kampanye'].$row['code']; $newresult=array_column($result,$key); file_put_contents ('datasetarray.txt', print_r($newresult, true)); } $arrlength=count($newresult); for($x=0;$x<$arrlength;$x++) { file_put_contents ('dataset.txt', implode(',',$newresult[$x])."\n", file_append); } ?>
Comments
Post a Comment