Create multidimensional array from txt file - php

txt file that looks like this:
first item1:second item1:third item1:fourth item1:fifth item1
first item2:second item2:third item2:fourth item2:fifth item2
first item3:second item3:third item3:fourth item3:fifth item3
I want to be able to output each item sperately.
As far as I see it, I need to:
open the file with file()
create a multidimensional array, by first splitting up the lines
use explode() to split it up by :
I have tried various things but I can't get it to work. Anyone knows how to do that?

So you need something like this?
$lines = file('try.txt');
$output = array();
foreach ($lines as $line) {
$output[] = array_map(function($var) {
return rtrim($var, "\r\n");
}, explode(":", $line));
}
var_dump($output);
Output
array
0 =>
array
0 => string 'first item1' (length=11)
1 => string 'second item1' (length=12)
2 => string 'third item1' (length=11)
3 => string 'fourth item1' (length=12)
4 => string 'fifth item1' (length=11)
1 =>
array
0 => string 'first item2' (length=11)
1 => string 'second item2' (length=12)
2 => string 'third item2' (length=11)
3 => string 'fourth item2' (length=12)
4 => string 'fifth item2' (length=11)
2 =>
array
0 => string 'first item3' (length=11)
1 => string 'second item3' (length=12)
2 => string 'third item3' (length=11)
3 => string 'fourth item3' (length=12)
4 => string 'fifth item3' (length=11)

Related

Convert an indexed array obtained from .csv to an associative array

I am reading data from a .csv (which originally comes from an external database) file with fgetcsv. The first line of the .csv file contains the column names, the following rows contain the data.
What I got so far is an array with the column names, and a second array with the rows of of the .csv file (each row as an array).
How can I convert the second array to an associative array with keys being the column names from the first array?
// this is for Mac OS X
ini_set("auto_detect_line_endings", true);
// the name of the file to read
$fileName = 'WebArtikel_V1-modified.csv';
// open file (get handle to file)
$file = fopen($fileName, 'r');
// the array to store the csv's data
$rawData = array();
// the first line of the csv
$header = array();
$i = 0;
while (($line = fgetcsv($file, 4096, ";")) !== FALSE) {
//$line is an array of the csv elements
//print_r($line);
if($i == 0){
$header[] = $line;
} else {
$rawData[] = $line;
}
$i++;
}
fclose($file);
Here is the var_dump($header):
array (size=1)
0 =>
array (size=50)
0 => string 'KHK_EAN' (length=7)
1 => string 'StyleNumber' (length=11)
2 => string 'StyleName' (length=9)
3 => string 'Setname' (length=7)
4 => string 'ColorNumber' (length=11)
5 => string 'ColorName' (length=9)
6 => string 'StyleSize' (length=9)
7 => string 'KHK_Artikel' (length=11)
8 => string 'PriceNew' (length=8)
9 => string 'PriceSale' (length=9)
Note: there is a toplevel array with 1 element which contains another array with the column names.
The $rawData array:
array (size=3604)
0 =>
array (size=50)
0 => string '4055765008989' (length=13)
1 => string '201001001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '2942' (length=4)
5 => string 'Blau/Marine' (length=11)
6 => string '1' (length=1)
7 => string '201001001-2942-1' (length=16)
8 => string '199,9' (length=5)
9 => string '199,9' (length=5)
1 =>
array (size=50)
0 => string '4055765008996' (length=13)
1 => string '201001001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '3924' (length=4)
5 => string 'Beige/Braun' (length=11)
6 => string '1' (length=1)
7 => string '201001001-3924-1' (length=16)
8 => string '199,9' (length=5)
9 => string '199,9' (length=5)
2 =>
array (size=50)
0 => string '4055765009047' (length=13)
1 => string '201002001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '2942' (length=4)
5 => string 'Blau/Marine' (length=11)
6 => string '1' (length=1)
7 => string '201002001-2942-1' (length=16)
8 => string '179,9' (length=5)
9 => string '179,9' (length=5)
Note: same as above, nested arrays, so I can't use array_combine().
Any suggestions?
Is this the same question as here? Copying it below for convenience.
how do i parse a csv file to grab the column names first then the rows that relate to it?
For reading it all at once you can use:
$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
To turn all the rows into a nice associative array you could then apply:
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}

How to display array value separately with selected column?

I want to display array 1 by 1.. 1st array value display thnn 2nd array value
I want to display array data using loop and display data separatelylike 1st array data display then 2nd array data will display. and i want to display data only entrydate n description. with loop count
$svs= $user_posts->getvalue('service', $dsplylimit );
var_dump($svs);
My array is like this
array (size=2)
0 =>
array (size=46)
0 => string '74' (length=2)
'id' => string '74' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 07:32:08' (length=19)
'entrydate' => string '2016-02-08 07:32:08' (length=19)
3 => string '2016-02-08 01:32:08' (length=19)
'date_gmt' => string '2016-02-08 01:32:08' (length=19)
4 => string 'nice email' (length=10)
'description' => string 'nice email' (length=10)
1 =>
array (size=46)
0 => string '75' (length=2)
'id' => string '75' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 11:15:40' (length=19)
'entrydate' => string '2016-02-08 11:15:40' (length=19)
3 => string '2016-02-08 05:15:40' (length=19)
'date_gmt' => string '2016-02-08 05:15:40' (length=19)
4 => string 'hiiiiiii' (length=8)
'description' => string 'hiiiiiii' (length=8)
It isn't very clear what you want to do, but you can probably display the data with a foreach loop. Just loop through each array, and then print out the values from each iteration that you want.
For example for the following array
$data = [
['entrydate' => '2016-01-01', 'description' => 'Some description here'],
['entrydate' => '2016-01-02', 'description' => 'Another description here']
];
You could print out the data like so:
$html = '';
foreach ($data as $array) {
$html .= '<h1>' . htmlspecialchars($array['entrydate']) . '</h1>';
$html .= '<p>' . htmlspecialchars($array['description']) . '</p>';
}
echo $html;
For the following output:
2016-01-01
Some description here
2016-01-02
Another description here
There's a chance I'm not understanding your requirements, if so just let me know, but I'm not 100% sure what it is you're after.

How do I move the first arrays into list of array on the second array?

---first array---
array (size=3)
0 =>
array (size=1)
0 => string ' doctor' (length=47)
1 =>
array (size=1)
0 => string ' Contact: Rob miller' (length=22)
2 =>
array (size=2)
0 => string ' Location: anywhere 23 ' (length=37)
1 => string ' ME 04848' (length=10)
--- second array---
array (size=2)
'notelp' => string '(xxx) xxx-xxxx ' (length=15)
'site' => string 'http://www.example.com/' (length=33)
what I want is:
array (size=2)
'notelp' => string '(xxx) xxx-xxxx ' (length=15)
'site' => string 'http://www.example.com/' (length=33)
'profession' => string ' doctor' (length=5)
'contact' => string ' Contact: Rob miller' (length=22)
'address' => string ' Location: anywhere 23 , ME 04848'
Use Implode to point the third key in second array
$secondArray['profession'] = $first_array[0][0];
$secondArray['contact'] = $first_array[1][0];
$secondArray['address'] = implode(', ', $first_array[2])
You can do the following
$second_array['profession'] = $first_array[0][0];
$second_array['contact'] = $first_array[1][0];
$second_array['address'] = $first_array[2][0];

Split Array at Repeating Object in Array

I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)

php read object of array in array

$values = (object)$arr;
var_dump($values);
var_dump produces these:
object(stdClass)[1]
public 'time' => float 0.002
public 'distance' => float 0.156
public 'code' => string '1' (length=1)
public 'result' =>
array (size=17)
0 => string '2.94053, 101.787, A' (length=24)
1 => string '2.94043, 101.787, A' (length=24)
2 => string '2.9404, 101.787, A' (length=23)
3 => string '2.94029, 101.787, A' (length=24)
4 => string '2.94025, 101.787, A' (length=24)
5 => string '2.9402, 101.787, A' (length=23)
6 => string '2.94016, 101.787, A' (length=24)
7 => string '2.94007, 101.787, A' (length=24)
public 'arrayPosition' =>
array (size=1)
0 =>
array (size=1720)
0 => string '2.93955, 101.788, B' (length=22)
1 => string '2.93951, 101.788, B' (length=22)
2 => string '2.93926, 101.788, B' (length=22)
3 => string '2.93921, 101.788, B' (length=22)
4 => string '2.9392, 101.788, B' (length=21)
5 => string '2.93911, 101.788, B' (length=22)
6 => string '2.93906, 101.789, B' (length=22)
7 => string '2.93896, 101.789, B' (length=22)
How can I read each of the values of result and arrayPosition ?
By using this
echo "<br>".$values->time."<br>";
echo $values->distance."<br>";
echo $values->code."<br>";
echo $values->result."<br>";
echo $values->arrayPosition."<br>";
will fail on result and arrayPosition
Notice: Array to string conversion
to access arrayPosition there are couple of ways if you know the sub index of the element you want you can access it by
$values->arrayPosition[0][index_number]; //you may require the first dimension index if you have more than one element there as well
The other method is using a loop
foreach ($values->arrayPosition as $levelone) {
foreach ($levelone as $key => $leveltwo) {
echo $leveltwo;
}
}
foreach($values as $value)
{
foreach($value->result as $result1)
{
echo($result1);
echo("<br>");
}
foreach($value->arrayPosition as $arrayposition2)
{
foreach($arrayposition2 as $arrayposition_child)
{
echo($arrayposition_child);
echo("<br>");
}
}
}
for accessing those 2 variables, you can:
1)
foreach( $values->result as $row ) {
echo $row;
}
2)
foreach( $values->arrayPosition as $mulRow ) {
foreach( $mulRow as $row ) {
echo $row;
}
}

Categories