I was creating php code to convert the below json to csv
Array
(
[data] => Array
(
[0] => Array
(
[DESC] => bla bal bal
[SOLD] => 0
[contact_no] => 1234
[title] => Hiiiii
[price] => 10900
[big_image] => Array
(
[0] => http://example.com/images/user_adv/14.jpg
[1] => http://example.com/images/user_adv/15.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/14.jpg
[1] => http://example.com/images/user_adv/small/15.jpg
)
[tpe] => user
)
[1] => Array
(
[DESC] => fo fo fof ofof
[SOLD] => 0
[contact_no] => 234522
[title] => Hellooooo sddf
[price] => 0
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
[tpe] => user
)
)
[pis] => 3
[totals] => 23
[curpage] => 1
[total_ads] => 71
)
I've been using the below code to export it to .csv
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
I can convert it fine, but I face a small issue that the sub array which is big_image & small_image is NOT appearing in the output file .csv (the row is empty)
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
By the way, if I replace:
foreach ($json['data'] as $fields) {
with
foreach ($json['data'][0] as $fields) {
I get the link pictures as output, so I need to merge them as one output
foreach ($json['data'] as $fields) {
foreach ($json['data'][0] as $fields2) {
edit :
here the output
edit 2 :
i expect the output something like that
You could make a function to make sure those nested arrays are made flat. You can create a utility function for that, like this:
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
And call it like this:
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
Related
I am looking to convert a text based DAT file into an array in PHP. Normally I would be able to read each line and explode it into an array but this file is different.
[0]
FirstRideOn=43169.5701090972
Laps=4591
LastRideOn=43224.7924173611
Name=Standard 1
Nr=1
ResetDate=0
RunningTime=2481
Runs=233
TranNr=7435191
[1]
FirstRideOn=43149.5406271644
Laps=5528
LastRideOn=43224.7616565972
Name=Standard 2
Nr=2
ResetDate=0
RunningTime=2957
Runs=292
TranNr=8377256
I was hoping to load it into an associative array.
Any feedback or suggestions are greatly appreciated!
It's not clear from your question exactly what format you want the array to be in. So here are two alternatives. First we read the file into an array:
$input= file('somefile.dat', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Then we can process the array to make an associative array. Here's the first option:
$output = array();
foreach ($input as $line) {
if (preg_match('/^\[(\d+)\]$/', $line, $matches)) {
$entry = (int)$matches[1];
$output[$entry] = array();
}
else {
list($key, $value) = explode('=', $line);
$output[$entry][$key] = $value;
}
}
print_r($output);
Which generates this output:
Array
(
[0] => Array
(
[FirstRideOn] => 43169.5701090972
[Laps] => 4591
[LastRideOn] => 43224.7924173611
[Name] => Standard 1
[Nr] => 1
[ResetDate] => 0
[RunningTime] => 2481
[Runs] => 233
[TranNr] => 7435191
)
[1] => Array
(
[FirstRideOn] => 43149.5406271644
[Laps] => 5528
[LastRideOn] => 43224.7616565972
[Name] => Standard 2
[Nr] => 2
[ResetDate] => 0
[RunningTime] => 2957
[Runs] => 292
[TranNr] => 8377256
)
)
Or we can do it this way:
// alternate style
$output = array();
foreach ($input as $line) {
if (preg_match('/^\[(\d+)\]$/', $line, $matches)) {
$entry = (int)$matches[1];
}
else {
list($key, $value) = explode('=', $line);
if (!array_key_exists($key, $output)) $output[$key] = array();
$output[$key][$entry] = $value;
}
}
print_r($output);
which generates this output. The choice is yours!
Array
(
[FirstRideOn] => Array
(
[0] => 43169.5701090972
[1] => 43149.5406271644
)
[Laps] => Array
(
[0] => 4591
[1] => 5528
)
[LastRideOn] => Array
(
[0] => 43224.7924173611
[1] => 43224.7616565972
)
[Name] => Array
(
[0] => Standard 1
[1] => Standard 2
)
[Nr] => Array
(
[0] => 1
[1] => 2
)
[ResetDate] => Array
(
[0] => 0
[1] => 0
)
[RunningTime] => Array
(
[0] => 2481
[1] => 2957
)
[Runs] => Array
(
[0] => 233
[1] => 292
)
[TranNr] => Array
(
[0] => 7435191
[1] => 8377256
)
)
I have two arrays that was converted from csv file.
The first csv line looks like this:
franchise_id,franchise_name,phone,website,email,region_codes;
1,"Abbott, Hackett and O`Conner",1-648-177-9510,auto-service.co/bw-319-x,Lupe-2485#auto-service.co,"36101,36055,36071";
The second csv line looks like this:
postal_code,region_code,city,state,region;
14410,36055,Adams Basin,NY,Monroe;
I converted these lines to arrays like this:
//Region Array
$region_lines = explode(PHP_EOL, $region_mappings_string);
$region_array = array();
foreach ($region_lines as $region_line) {
$region_array[] = str_getcsv($region_line);
}
//Franchise Array
$franchise_lines = explode(PHP_EOL, $franchises_string);
$franchise_array = array();
foreach ($franchise_lines as $franchise_line) {
$franchise_array[] = str_getcsv($franchise_line);
}
After that I got the result like this for Region:
Array
(
[0] => Array
(
[0] => postal_code
[1] => region_code
[2] => city
[3] => state
[4] => region;
)
[111] => Array
(
[0] => 14410
[1] => 36055
[2] => Adams Basin
[3] => NY
[4] => Monroe;
)
[112] => Array
(
[0] => 14617
[1] => 36055
[2] => Rochester
[3] => NY
[4] => Monroe;
)
And for franchise:
Array
(
[0] => Array
(
[0] => franchise_id
[1] => franchise_name
[2] => phone
[3] => website
[4] => email
[5] => region_codes;
)
[1] => Array
(
[0] => 1
[1] => Abbott, Hackett and O`Conner
[2] => 1-648-177-9510
[3] => auto-service.co/bw-319-x
[4] => Lupe-2485#auto-service.co
[5] => 36101,36055,36071;
)
What I need to do is to look for 14410 postal code from the first array, get the region_code and look for this region_code in second array and then output the results in PHP. How can this be done?
Will have to loop over franchises array and run simple comparison
function getDataByRegionCode($code, $franchiseArray) {
foreach ($franchiseArray as $key => $data) {
if(isset($data[5])){
$codes = explode(',',$data[5]);
if(in_array($code,$codes)) return $data;
}
}
return NULL;
}
print_r(getDataByRegionCode(14410,$franchiseArray));
I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}
How can i replace/remove some string in this JSON ?.I think this problem can be solve using str_replace method or preg_replace
but i don't know how to add the regex
Please help me.
here my json
Array
(
[data] => Array
(
[0] => Array
(
[DESC] => bla bal bal
[SOLD] => 0
[contact_no] => 1234
[title] => Hiiiii
[price] => 10900
[big_image] => Array
(
[0] => http://example.com/images/user_adv/14.jpg
[1] => http://example.com/images/user_adv/15.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/14.jpg
[1] => http://example.com/images/user_adv/small/15.jpg
)
[tpe] => user
)
[1] => Array
(
[DESC] => fo fo fof ofof
[SOLD] => 0
[contact_no] => 234522
[title] => Hellooooo sddf
[price] => 0
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
[tpe] => user
)
)
[pis] => 3
[totals] => 23
[curpage] => 1
[total_ads] => 71
)
will i use this function to export the json to csv
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
the above code work fine but each image link is have one column so looks like bad , i need to make each group of pics on one column
I try to add regex to part of link images except the first image url [0] and merge the other with it , that i can putting them together in one column....
so for the experiment i add this to the above code but seems nothing happen
$flat[$k] = str_replace('[1-7] => http', "http", $v);
here i expect the output something like that
....
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
http://example.com/images/user_adv/144.jpg
http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
http://example.com/images/user_adv/small/144.jpg
http://example.com/images/user_adv/small/147.jpg
)
.....
edit this the .csv file output look like this
and I'm looking to be something like that
ok I've fixed by
foreach (
new RecursiveArrayIterator($nonFlat) as $k=>$v) {
$flat[$k] = is_array($v)?implode(" ",$v):$v;
now i got each group of images on one column
thanks :)
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);