I am getting tr of tables and then in loop i want get text of all td fields, look here:
<?
$lines = $xpath->query("//table[#id='cab_table'] //tr");
var_dump($lines);// Give me object(DOMNodeList)#11 (1) { ["length"]=> int(6) }
for( $i = 0; $i < count($lines); $i++) {
if($i != 0){
$tds = $xpath->query('//td', $lines[$i]);
$result[$i - 1]['number'] = trim($tds->item(0)->nodeValue);
$result[$i - 1]['volume'] = trim($tds->item(1)->nodeValue);
$result[$i - 1]['sum'] = trim($tds->item(2)->nodeValue);
}
}
var_dump($result); //Give me NULL
die();
?>
Why i get NULL?
Now i have:
$lines = $xpath->query("//table[#id='cab_table'] //tr");
foreach($lines as $line) {
$tds = $xpath->query('//td', $line);
$count = $tds->length;
for($i=0; $i<$count; $i++){
echo $tds->item($i)->nodeValue.'<br>';
//echo $i.'<br>';
}
}
But i want make the next for each tr at loop $result[0] = td[0]; $result[1] = td[1]; $result[2] = td[2]; Can you advise me?
->query() returns a DOMNodeList object. It can be count()ed and foreach()ed, but you can't USE it as an array as you are.
$tds = $xpath->query('//td', $lines[$i]);
^^^^^^^^^^---incorrect
Try
$lines = $xpath->query("//table[#id='cab_table'] //tr");
foreach($lines as $line) {
$tds = $xpath->query('//td', $line);
...
}
instead.
foreach($lines as $line) {
for($j=0; $j<=3; $j++) {
$tds_{$j} = $xpath->query('//td['.$j.']', $line);
$tds_{$j} = $xpath->query('//td['.$j.']', $line);
$tds_{$j} = $xpath->query('//td['.$j.']', $line);
$count = $tds_{$j}->length;
for($i=0; $i<$count; $i++){
$this->result['number'][] = $tds_{$j}->item($i)->nodeValue;
$this->result['volume'][] = $tds_{$j}->item($i)->nodeValue;
$this->result['code'][] = $tds_{$j}->item($i)->nodeValue;
$this->result['start_date'][] = $startDate;
$this->result['end_date'][] = $endDate;
}
}
}
Related
While printing column and row from an array it's showing blank not exporting any data
$inn_table = "";
if ($result['qualification']['dd1'] != "") {
$dd1 = explode(",", $result['qualification']['dd1']);
}
if ($result['qualification']['dd2'] != "") {
$dd2 = explode(",", $result['qualification']['dd2']);
}
for ($i = 0; $i < count($labels); $i++) {
if ($result['qualification']['dd1'] != "") {
echo '<pre>First';
print_r($dd1); ** //echo arrays**
for ($j = 0; $j < count($dd1); $j++) {
$temp = explode(">", $dd1[$j]);
if ($temp[0] == $labels[$i]) {
$name = explode(">", $dd1[$j]);
echo '<pre>First ';
print_r($name[1]); ** //echo names**
}
}
}
}
Here is my Array structure
and Here names which i am trying to export from Array
So i only trying to export names from that array please help
foreach($labels as $ind => $label) {
$index = $ind + 2;
$letter = range('A', 'Z')[$ind2];
$val = explode('>', $data);
$objPHPExcel->getActiveSheet()->setCellValue($letter . $index, $val[1]);
}
i have csv file available and i want to find value
Ex:
sku qty
11111 2 22222 4 333333 6
then i want to qty of 22222 sku
how can i will achieve it ?
my code is like below way ...
$handle = fopen("test.csv", "r");
//rewind($handle);
$count = 0;
while (($data = fgetcsv($handle, 0, ";")) !== false) {
$count++;
if ($count == 1) {
continue;
}
for ($i = 0, $j = count($data); $i < $j; $i++) {
$insert_csv = array();
$insert_csv['sku'] = $data[0];
$insert_csv['qty'] = !empty($data[1]) ? $data[1] : 0;
}
}
fclose($handle);
and my csv file output like this way ...
$file = file("test.csv");
for ($i = 2, $size = sizeof($file); $i < $size; $i++) {
$line = explode(';', $file[$i]);
if (trim($line[0]) === '22222') {
$answer = trim($line[1]);
continue;
}
}
I have to convert a PHP script in Powershell script.
I have current PHP script:
<?php
// read text file
$File = fopen("file.txt", "r") or die("Unable to open file!");
$typePers = array("Personale Viaggiante","Personale Interno");
$excludeFields = array("IT0006","IT0017","IT0077","Personale Viaggiante","Personale Interno","IT0105");
$emptyValue = "NaN";
$rowfinal = "";
// Output one line until end-of-file
while(!feof($File)) {
$row = explode(";",fgets($File));
$substring = "IT0006;IT0017;";
// left string
$lstr = "";
// right string
$rstr = "";
$type = array_intersect($row, $typePers);
$keys = array_keys($type);
if($keys == null) $type = $emptyValue;
else $type = $row[$keys[0]];
$j = 0;
for($i=0; $i<count($row); $i++){
if(in_array($row[$i],$excludeFields)){
unset($row[$i]);
$j++;
}
}
for($i=0; $i<22;$i++)$lstr .= $row[$i].";";
foreach($row as $k => $v)
if($k > 22)if(!empty($v))$rstr .= $v.";";
$rstr = substr($rstr,0,-1);
$substring .= $type;
$substring .= ";IT0077;NaN;IT0105;";
$rowfinal .= $lstr.$substring.$rstr;
}
$file = 'test.txt';
// Write the contents back to the file
file_put_contents($file, $rowfinal);
fclose($sapFile);
?>
I started writing this script in Powershell, but it is incomplete.
$File = Get-Content "file.txt"
$FilePath = "dump.txt"
$typePers = #("Personale Viaggiante","Personale Interno")
$excludeFields = #("IT0006","IT0017","IT0077","Personale Viaggiante","IT0105","Personale Interno")
$emptyValue = "NaN"
$rowfinal = ""
foreach($line in $File) {
[System.Collections.ArrayList]$row = $line -split ";"
$substring = "IT0006;IT0017;"
$lstr = ""
$rstr = ""
$ityp = -1;
for($i=0; $i -lt $row.Count; $i++){
if($typePers -contains $row[$i] ){
$ityp = $i
}
}
if($ityp -eq "-1"){
$type = "NaN"
}else{
$type = $row[$ityp]
}
$j = 0;
for($i=0; $i -lt $row.Count; $i++){
if($excludeFields -contains $row[$i]){
$row.RemoveAt($i)
$j++;
}
}
for($i=0; $i -lt 22;$i++) {
$lstr += $row[$i]+";"
}
$k = 0;
ForEach($v in $row) {
if($k -gt 22) {
if($v -ne ""){
$rstr += $v+";"
}
}
$k++
}
$rstr = $rstr.Substring(0,$rstr.Length-1)
$substring += $type
$substring += ";IT0077;NaN;IT0105;";
$rowfinal += $lstr+$substring+$rstr+"`r`n"
}
$rowfinal | Out-File -FilePath $FilePath
I have only problem with Remove method.
When I have IT0006 value in array, I have IT0105 duplicated...
To end, I have to add header columns for CSV file.
For example, I would to define an array with header:
$header = #('column1','column2',...,'column_n');
and add header at the file to export in CSV.
i have the following code
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
foreach ($contents as $key => $itemline)
{
}
foreach ($fnamearray as $key2 => $fname)
{
echo ($fname);
echo ($itemline);
}
what i want to do is to have the first line of each file echo so the output looks like
fname[0},itemline[0],fname[1],itemline[1]
what i am getting with the following is just this
fname[0],fname[1],fname[2].... ect
h
Assuming the indexes will always match:
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('/home/b1396hos/public_html/ofwgkta.co.uk/dd_folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
for($i = 0; $i < count($contents); $i++)
{
echo $fnamearray[$i];
echo $contents[$i];
}
Since both arrays are simple, consecutive numeric indexed arrays, you can just use a for loop:
$l = max(count($fnamedata),count($contents));
for($i=0; $i<$l; $i++) {
$itemline = $contents[$i];
$fname = $fnamearray[$i];
// do stuff
}
I want to export a row in CSV file to JSON file with require : 1 line in CSV export 1 file JSON. I success to export file,but can't filter the row that i want to export. Can anyone help me?
<?php
header('Content-type: application/json');
$feed = 'jsondata_palpad.csv';
$keys = array();
$newArray = array();
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
$data = csvToArray($feed, ',');
$count = count($data) - 1;
$labels = array_shift($data);
foreach ($labels as $label) {
$keys[] = $label;
}
$keys[] = 'id';
for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
//Xuat file JSON
for ($i = 0; $i < 5; $i++) {
$json = json_encode($newArray[$i]);
echo $json . "<br />\n";
$filename = $data[$i][1] . ".json";
echo $filename . "<br />\n";
//echo "title[" . $data[$i][4] . "]";
$handle = fopen("./" . $filename, "w");
fwrite($handle, $json);
fclose($handle);
}
?>
If recommend = 2,it will export line whre id=2.
I want to export id,product_id,title,outline to JSON file.This is sample JSON file:
http://img839.imageshack.us/img839/5277/21527799.png
This is CSV file :
http://img690.imageshack.us/img690/1526/43004273.png
You are looping through all 6 lines of the csv file and saving them to the JSON file:
for ($i = 0; $i < 5; $i++) {
...
}
If you know what row number it is you want, don't loop through every row - just call the code inside the loop for that row. For example, if you wanted the 2nd row:
$row_we_want = 1;
$json = json_encode($newArray[$row_we_want]);
$filename = $data[$row_we_want][1] . ".json";
$handle = fopen("./" . $filename, "w");
fwrite($handle, $json);
fclose($handle);
If you are unsure about which row it is and need to check each one, you could do so in the loop:
for ($i = 0; $i < 5; $i++) {
if (some_condition) {
$json = json_encode($newArray[$i]);
$filename = $i][1] . ".json";
$handle = fopen("./" . $filename, "w");
fwrite($handle, $json);
fclose($handle);
}
}
Also it might be worth replacing 5 in your loop with the length of the array using the count function if it is not always a fixed length.