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'],
----------------------
----------------------
)
)
)
);
Related
I am beginner to laravel. I am trying to access array input from my API request.
For postman, I have array as key called file_name_list and its value like ["m_profile.png","aa_image.jpg","new_pan.jpg"]. I want to access that array in my controller. That values should go into 3 seperate variables like
$profile = m_profile.png
$aadhar = aa_image.jpg
$pan = new_pan.jpg
For that I am trying to use replace and explode functions in controller.
$filenamelist1 = Str::replaceArray(array(' ','"', '[',']'),[""], $request->file_name_list);
$filename_str = explode(",",$filenamelist1);
After this I want to store values from explode array to 3 variables as mentioned above using for loop
But I am facing problems like in Str::replaceArray 2 parameter should be array and for explode 2 parameter should be string.
How should I use replace and explode to get required result? please guide. Thanks in advance.
You can use list method like below:
list( $profile , $adhar, $pan) = $request->file_name_list;
Check the Docs
If Your array size fixed then you assign that by using list
list( $profile,$adhar,$pan) = $request->file_name_list;
<?php
$value = '["m_profile.png","aa_image.jpg","new_pan.jpg"]';
$value = str_replace("[","",$value); // clean [
$value = str_replace("]","",$value); // clean ]
$arrayValues = explode('","',$value); // explode with ","
print_r($arrayValues);
output
Array (
[0] => "m_profile.png
[1] => aa_image.jpg
[2] => new_pan.jpg"
)
Replace " when you access the values
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 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.
I need to store this data structure in php array:
Movie has id, name and show_date. Each of show_dates have show_times. I need to dynamically in cilcle fill this array with data from my data source. When i do like this:
$Movie = array();
$Movie[0]['id']=10;
$Movie[0]['name']='Some Name';
$Movie[0]['date'][0]='12.12.12';
$Movie[0]['date'][0]['time'][0]='12:23:00'; //there it throws error
$Movie[0]['date'][0]['time'][1]='15:23:00';
Could you help me with this issuse ?
You're trying to do array access on a string.
Change to:
$Movie[0]['date'] = array();
$Movie[0]['date'][] = array( // shorthand push notation
"date" => "12.12.12",
"times" => array("12:23:00", "15:23:00")
);
// .. etc
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.