Basically, I have multiple files containing loads of PHP variables, and i'm trying to create a way to read them in to a subarray for each file with each variable being in the array.
<?php
$data = Array();
$getData = glob('siteData/*.vars.php', GLOB_BRACE);
foreach($getData as $tempData) {
$dataKey = str_replace(".vars.php", "", str_replace("siteData/", "", $tempData));
$data[$dataKey] = Array();
//foreach varaible in file
//$data[$dataKey][$varaibleName] = $variable
}
?>
The bit commented out is the bit I need help with as I have no idea how to do it.
Please help, TIA.
After a bit of help, the answering code is as follows:
<?php
$data = Array();
$getData = glob('siteData/*.vars.php', GLOB_BRACE);
foreach($getData as $tempData) {
$dataKey = str_replace(".vars.php", "", str_replace("siteData/", "", $tempData));
$data[$dataKey] = Array();
$dataFile = file_get_contents($tempData);
preg_match_all('/\$[A-Za-z0-9-_]+\s?=\s?["\'].+["\'];/', $dataFile, $dataVars);
$dataVars = $dataVars[0];
foreach($dataVars as $variable) {
$variable = explode("=", $variable);
$varaibleName = ltrim(trim($variable[0]), "$");
$variable = trim(trim(rtrim(trim($variable[1]), ";"), "'"), '"');
$data[$dataKey][$varaibleName] = $variable;
}
}
print_r($data);
?>
Related
I tried write a php script for taking 1 main file from server and read this file, explode it(with ":" character) and keep it a array then I write this variables in array to a txt file each new line. Then I can read this file line by line but I can't open any file with fopen($variable, 'r');. My variable is; $variable = $array[1]."txt";.
My codes;
<?php
$file = file("toplist.txt");
$countLine = count($file);
$userMain = array();
$userMain[0] = "Top List";
$userNames = array();
$userNames[0] = "SampleName";
for ($i=1;$i<$countLine;$i++){
$user = explode (":",$file[$i],-1);
$userMain[$i] = $user[0];
echo $userMain[$i]."<br>"; //Test echo
}
$totalLn = count($userMain);
echo $totalLn; //Echo total line.
$myFile = $userMain[1].".txt";
$fileAA = fopen($myFile,'r');
while($line = fgets($fileAA))
$data[] = $line;
fclose($fileAA);
for ($counter = 0; $counter <= 5 ; $counter++ )
{
echo "<i>".$data[$counter]."</i><br />";
}
?>
My toplist.txt file;
toplist:
54df3a11-3ea0-37c4-8ec4-0fdd45f2e069: 211
and I have a file with a 54df3a11-3ea0-37c4-8ec4-0fdd45f2e069.txt named.
And 54df3a11-3ea0-37c4-8ec4-0fdd45f2e069.txt file contents;
name : SampleName123
destination : SampleDestination
SampleContent : SampleContent
I need the name line and just SampleName123.
$contents = file_get_contents($array[1]."txt");
$rows = explode("\n",$contents);
$user = [];
foreach($rows as $row){
$parts = explode(" : ",$row);
$user[$parts[0]] = $parts[1];
}
After this parsing you can access user as an array.
$user['name']
And you can do the listing as well:
$file = file($user['name'].".txt"); //Reads entire file into an array
foreach($file as $row){
echo "<i>".$row."</i><br />";
}
PS: It's working but need to use trim() function for taken file names from .txt file
If the file content is always the same take the first row and use substr($str, 0, 7); // Outputs: SampleName123
I'm tring to get all content from this xml: https://api.eveonline.com/eve/SkillTree.xml.aspx
To save it on a MySQL DB.
But there are some data missing...
Could any1 that understand PHP, Array() and XML help me, please?
This is my code to get the content:
<?php
$filename = 'https://api.eveonline.com/eve/SkillTree.xml.aspx';
$xmlbalance = simplexml_load_file($filename);
$skills = array();
for ($x=0;$x<sizeOf($xmlbalance->result->rowset->row);$x++) {
$groupName = $xmlbalance->result->rowset->row[$x]->attributes()->groupName;
$groupID = $xmlbalance->result->rowset->row[$x]->attributes()->groupID;
for ($y=0;$y<sizeOf($xmlbalance->result->rowset->row[$x]->rowset->row);$y++) {
$skills[$x]["skillID"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->attributes()->typeID;
$skills[$x]["skillName"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->attributes()->typeName;
$skills[$x]["skillDesc"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->description;
$skills[$x]["skillRank"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rank;
$skills[$x]["skillPrimaryAtr"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->requiredAttributes->primaryAttribute;
$skills[$x]["skillSecondAtr"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->requiredAttributes->secondaryAttribute;
$o = 0;
for ($z=0;$z<sizeOf($xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row);$z++) {
if ($xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->attributes()->name == "requiredSkills") {
$skills[$x]["requiredSkills"]["".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row[$z]->attributes()->typeID] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row[$z]->attributes()->skillLevel;
$o++;
}
}
}
}
echo '<pre>'; print_r($skills); echo '</pre>';
?>
If you go to the original XML (link), at line 452, you will see:
<row groupName="Spaceship Command" groupID="257">
And that isn't show in my array (link)...
That is one thing that i found that is missing...
I think that probally have more content that is missing too..
Why? How to fix it, please?
Thank you!!!
You will only get a total of sizeof($xmlbalance->result->rowset->row) records. Because, in your 2nd for loop, you are basically storing your result in the same array element that is $skills[$x].
Try this (I also higly encourage you to be as lazy as you can when you write code - by lazy I mean, avoid repeating / rewriting the same code over and over if possible) :
$filename = 'https://api.eveonline.com/eve/SkillTree.xml.aspx';
$xmlbalance = simplexml_load_file($filename);
$skills = array();
foreach ($xmlbalance->result->rowset->row as $row)
{
$groupName = $row->attributes()->groupName;
$groupID = $row->attributes()->groupID;
foreach ($row->rowset->row as $subRow)
{
$skill['skillID'] = (string) $subRow->attributes()->typeID;
$skill['skillName'] = (string) $subRow->attributes()->typeName;
$skill['skillDesc'] = (string) $subRow->description;
$skill['skillRank'] = (string) $subRow->rank;
$skill['skillPrimaryAtr'] = (string) $subRow->requiredAttributes->primaryAttribute;
$skill['skillSecondAtr'] = (string) $subRow->requiredAttributes->secondaryAttribute;
foreach ($subRow->rowset as $subSubRowset)
{
if ($subSubRowset->attributes()->name == 'requiredSkills')
{
foreach ($subSubRowset->row as $requiredSkill)
{
$skill['requiredSkills'][(string) $requiredSkill->attributes()->typeID] = (string) $requiredSkill['skillLevel'];
}
}
}
$skills[] = $skill;
}
}
print_r($skills);
I am using this code to list all files inside a directory which works perfectly
<?php
$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude);
foreach ($cssfiles as $cssfile) {
$filename = "http://example.com/lessons/css/".$cssfiles[$cssfile];
outputtags($filename,true,true);
}
?>
However, with this code nothing is shown on the webpage. I can't figure out why
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfiles[$htmlfile];
outputtags($filename,true,true);
}
?>
Try this:
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfile;
outputtags($filename,true,true);
}
?>
$htmlfiles[$htmlfile] should not be set and should not work.
You need to use $htmlfile inside foreach loop instead of $htmlfiles[$htmlfile], and it works with any other variable's name
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfile;
outputtags($filename,true,true);
}
?>
I know the title of my question may be confusing, but I'm not quite sure how to explain what I'm trying to do concisely.
I am trying to loop through an array of CSVs and load the data into variables with differing names. In my example below, instead of $foo_data it would be $MSFT_data, $AAPL_data, and $FB_data in each loop through the $stocks array.
$stocks = array($msft, $aapl, $fb);
foreach ($stocks as $stock) {
$fh = fopen($stock, 'r');
$header = fgetcsv($fh);
$foo_data = array();
while ($line = fgetcsv($fh)) {
$foo_data[] = array_combine($header, $line);
}
fclose($fh);
}
Please let me know if you need more information.
There are two problems. The first is that you cannot get the variable name, so the script has no way to know that there is an $msft, $aapl, $fb, so you need to pass the name along with the array. The second is that you need variable variables.
Try
$stocks = array('MSFT' => $msft, 'AAPL' => $aapl, 'FB' => $fb);
foreach ($stocks as $key=>$stock) {
$fh = fopen($stock, 'r');
$header = fgetcsv($fh);
$varname = $key . '_data';
$$varname = array(); //the double $$ will set the var content as variable ($MSFT_data)
while ($line = fgetcsv($fh)) {
${$varname}[] = array_combine($header, $line);
//the {} are needed to let PHP know that $varname is the name of the variable and not $varname[].
}
fclose($fh);
}
$MSFT_data = $foo_data[0];
$AAPL_data = $foo_data[1];
$FB_data = $foo_data[2];
How does that work for you?
I'm trying to get some data from a HTML
$xdata = simplexml_import_dom($doc);
$datas = $xdata->xpath("//*[#class='proglist']");
$aData = array();
foreach($datas as $data)
{
$rightdatas = $data->xpath("*[#class='progright']");
$rt = $rightdatas[0];
print_r($rt);
$content = $rt->xpath("*[#class='progrighthead']");
print_r($content );
}
If I'm printing out the content of the $rt than the progrighthead class is there, but the $content variable is empty. Why?
Why do I receiving the same result for the following syntax?
$xdata = simplexml_import_dom($doc);
$datas = $xdata->xpath("//*[#class='proglist']");
$aData = array();
foreach($datas as $data)
{
$rightdatas = $data->xpath("*[#class='progright']");
$rt = $rightdatas[0];
print_r($rt);
$content = $rt->xpath("*[#class='progrighthead']");
}
and
$datas = $xdata->xpath("//*[#class='progrighthead']");
progrighthead is not a child of progright, but a descendant. Use
$rt->xpath(".//*[#class='progrighthead']");
Putting // at the beginning means searching from the root, not from the current element.