I am a bit new to PHP, and I am having some trouble converting a csv file into a multidimensional array, where each dimension has a name. I need to two distinct named array so that I can use them in a preg_replace statement like the below:
$expand_abbrev=preg_replace($contracted_form , $expanded_form , $sentence);
the file content are as follows:
contracted_form,expanded_form
'/a./','in dates ante'
'/abbrev./','abbreviation of'
'/Abbrev./','abbreviations'
'/Abd./','Aberdeen'
'/Aberd./','Aberdeen'
'/Aberdeensh./','Aberdeenshire'
This is so far what I have come up with, but it does not achieve the desired output.
$abbrev_list=file_get_contents('files/abbreviations.text');
$test=str_getcsv($abbrev_list, ",");
$expand_abbrev=preg_replace($contracted_form, $expanded_form, $sentence);
Can anyone help me out with this please ? I have been trying so many times but so far no success.
---- Please allow me to make a clarification, because it seems I might have mislead you. I would like to process a csv file which has two two values per line contracted_form and expanded_form. These two values per line are separated by a comma.
I am not sure how best to approach it, I was thinking perhaps splitting the each line into two arrays like for example each contracted_form is stored in the contracted_from array and each expanded_form is stored in the expanded_form array.
So that preg_replace can replace any instance of contracted_from, encountered in a sentence with its corresponding expanded_form. So for example the following sentence:
Hi sir, I live in a flat in Abd.
So preg_replace(Adb. , Aberdeen, Hi sir, I live in a flat in Abd.) would result in the below.
Hi sir, I live in a flat in Aberdeen.
So from what I understood you want to put each line of the csv into a multi dimensional array, if I understood you wrong please correct me.
Also bare in mind the algorithm below does not take into csv headers into account.
// The Multidimensional Array
$array = array();
$handle = fopen("file.csv", "r");
// If csv if empty, this will not run
while((($data = fgetcsv($handle, null, ',')) !== false)) {
// The "magic" happen here.
temp = array();
$temp["contrated_form"] = $data[0];
$temp["expanded_form"] = $data[1];
array_push($array, $temp);
}
For example if your csv file had the following:
data1,data2
data3,data4
data5,data6
The result of the above algorithm would be:
Array (
[0] => Array (
[contrated_form] => data1
[expanded_form] => data2
)
[1] => Array (
[contrated_form] => data3
[expanded_form] => data4
)
[2] => Array (
[contrated_form] => data5
[expanded_form] => data6
)
)
Tips:
str_getcsv does not know that the first row of your csv file contains your column names.
str_getcsv does not need "," in the statement str_getcsv($abbrev_list, ",") because , is default separator.
Statement $expand_abbrev=... has no idea what you're arguments mean.
You must write a foreach to create array $contracted_form using $test.
You must write a foreach to create array $expanded_form using $test.
Related
asking for insert multiple lines in one mysql column (with something like line break like show didn't mean inserting data in multiple rows have multiple variables contain different urls like
$var_one = "http://example.com";
$var_two = "http://example.org";
$var_two = "http://example.net";
want to store these values in one mysql column ever value should be in new line refer image describe better
asking for insert multiple lines in one mysql column (with something like line break like didn't mean inserting data in multiple rows
If you trying in php, then you can serialize the data and store as single column in DB.
While retriving data, we can unserialize and get the data as array. Find below example.
<?php
$var = array();
$var[0] = "http://example.com";
$var[1] = "http://example.org";
$var = array("1","2","3");
print_r($var); // Array ( [0] => http://example.com [1] => http://example.org )
$b=serialize($var);
echo $b; // a:2:{i:0;s:18:"http://example.com";i:1;s:18:"http://example.org";}
$c=unserialize($b);
print_r($c); // Array ( [0] => http://example.com [1] => http://example.org )
Hi I'm using a simple import to CSV script that brings information into an array from a CSV file.
The sample output of the print_r($data) statement is:
Array ( [0] => Array ( [Email,Name,"Message Number","Date Added", ...etc ]) => email#email,com,myname,1001,"10/27/16 9:54pm EDT",... etc ) 1 => Array ( [Email,Name,"Message Number","Date Added",
print_r($data[0])
shows Array ( [Email,Name,"Message Number","Date Added",
I'm trying to isolate the variables but each time I get a null result on the quoted values.
$csv="members.csv";
$importer = new CsvImporter($csv,true);
$data = $importer->get();
echo "<pre>";
$n=0;
$messageNumber = $data[$n]['Message Number'];
What am I doing wrong? It should return '1001' in that variable.
Note: I'm using the class CsvImporter on this page in PhP FgetFSV
Again, what I'm looking for is the definition to isolate an individual variable in an easy way.
Thank you!
Perhaps someone has a simpler way to read a CSV into a PHP Array than using the above class?
In the example given in the php docs for CsvImporter it's changing the default delimiter to \t. Try add ',' as the 3rd param for the class:
$importer = new CsvImporter($csv,true, ',');
Hope this helps!
$csv="members.csv";
$importer = new CsvImporter($csv,true, ',');
$data = $importer->get();
echo "<pre>";
$n=0;
$messageNumber = $data[$n]['Message Number'];
print $messageNumber;
Returns the correct '1001' as expected.
The solution is marked above.Thanks to Ross. Simply changing the $importer = new CsvImporter($csv,true, ','); solved the entire puzzle.
I am generating an php array and then converting it to a xml playlist. converting array to xml is no problem, but i am having problem generating the array. I am getting data from DB and want to use it. My code is like bellow-
$songs2008 = get_data("musics", "where year='2008'");
$mysongs = array();
foreach($songs2008 as $k1=>$v1){
$entry = array(
"url"=>"songs/main_songs/".$v1[file_name],
"songname"=>$v1[song_title],
"artist"=>$v1[artist]
);
array_push($mysongs, $entry);
}
and the array is -
$array = array(
"settings"=>array(
"width"=>"316",
"songs"=>array(
"albumArt"=>array(
"url"=>"songs/2008.jpg",
"entries"=>array(
"entry"=>$mysongs['0'],
"entry"=>$mysongs['1'],
"entry"=>$mysongs['2'],
----------------------
----------------------
)
)
)
);
Its not working at the entries. the array key is same(entry); so only one showing. is there any solution? any other way to do it? please help.
It's because you can't use 'entry' as an index more than once. Try:
"entries" => $mysongs
#see: http://php.net/manual/de/language.types.array.php for further reading an php arrays
But be carefull, you'll propably be running into some problems creating your xml code when using this. For I would bet that you are using the array keys as tag names!
Don't name your array key for "entry"
$array = array(
"settings"=>array(
"width"=>"316",
"songs"=>array(
"albumArt"=>array(
"url"=>"songs/2008.jpg",
"entries"=>array(
$mysongs['0'],
$mysongs['1'],
$mysongs['2'],
----------------------
----------------------
)
)
)
);
E.g:
PHP code:strong text
// $files would be stored into mysql db and will retrieve as original
$files = array('1' => '1,2,4,5', '5' => '4,5,7,23,56','8' => '45,23,56,67');
And the database table file's allFile[char]
'1' => '1,2,4,5', '5' => '4,5,7,23,56','8' => '45,23,56,67'
I thought I must convert the array data into string and then store into database,but implode only got the array value without key.So, should I use the foreach loop to get the key/value into string?
foreach ($files as $key => $value){
$str .= $key.'=>'.$value.','; // remove the last comma of the $str
}
and when I retrieve $str form database, I get:
$files = array($str);
So, am I do the right way ?!
Thank you very much!!
json_encode/json_decode are a popular choice here, as it means the database content is still readable without PHP. In my quick tests, storing as JSON is also more efficient.
$string = serialize($array);
$array = unserialize($string);
http://php.net/manual/ru/function.serialize.php
http://php.net/manual/ru/function.unserialize.php
I have a script that is using the google charts API and the gChart wrapper.
I have an array that when dumped looks like this:
$values = implode(',', array_values($backup));
var_dump($values);
string(12) "8526,567,833"
I want to use the array like this:
$piChart = new gPieChart();
$piChart->addDataSet(array($values));
I would have thought this would have looked like this:
$piChart->addDataSet(array(8526,567,833));
Howerver when I run the code it creates a chart with only the first value.
Now when I hardcode the values in instead I get each value in the chart.
Does anyone know why it's acting this way?
Jonesy
I think
$piChart->addDataSet(array_values($backup));
// or just: $piChart->addDataSet($backup); depends on $backup
should do it.
$values only contains a string. So if you do array($values), you create an array with one element:
$values = "8526,567,833";
print_r(array($values));
gives
Array
(
[0] => 8526,567,833
)
array(8526,567,833) would be the same as array_values($backup) or maybe even just $backup, that depends on the $backup array.
Looks like you want to use $backup instead of $values as $values is the imploded string... and since 8526,567,833 isn't a valid number, it parses 8526 and leaves the rest alone.