php


Extract cell value from table based on another cells value


HTML file:
http://www.arifoorum.com/test/html.htm
I got this html contents with simplehtmldom library:
array(66) {
[0]=>
array(14) {
[0]=>
string(4) "Item"
[1]=>
string(11) "Date, time:"
[2]=>
string(8) "mõõdikud"
[3]=>
string(6) "Name 2"
[4]=>
string(6) "Name 3"
[5]=>
string(9) "Meter ID:"
[6]=>
string(6) "V_HeEn"
[7]=>
string(6) "U_HeEn"
[8]=>
string(3) "V_V"
[9]=>
string(3) "U_V"
[10]=>
string(6) "V_InTe"
[11]=>
string(6) "U_InTe"
[12]=>
string(6) "V_OuTe"
[13]=>
string(6) "U_OuTe"
}
[1]=>
array(14) {
[0]=>
string(1) "1"
[1]=>
string(19) "24.01.2013 22:23:33"
[2]=>
string(9) "Meter 002"
[3]=>
string(6) " "
[4]=>
string(6) " "
[5]=>
string(8) "40380040"
[6]=>
string(6) " "
[7]=>
string(6) " "
[8]=>
string(6) " "
[9]=>
string(6) " "
[10]=>
string(6) " "
[11]=>
string(6) " "
[12]=>
string(6) " "
[13]=>
string(6) " "
}
[2]=>
...
}
}
Full output: http://www.arifoorum.com/test/test.php
How do I get certain element from that array?
For example: lets say i want value where mõõdikud = 01 and name 2 = külm (that should be 72,114) .
Thanks
This could be useful for other users, so I made a little function that gets the value of a cell from a table, based on values of other cells (conditions):
function getCellValue(DOMElement $table, $cellName = null, array $conditions = array()){
// get all table rows
$trs = $table->getElementsByTagName('tr');
// assume first TR is the table header
$head = $trs->item(0);
// find cell names and their index
$keys = array();
foreach($head->childNodes as $th)
if(!($th instanceof DomText))
$keys[] = trim($th->nodeValue);
if($invalidKeys = array_diff(array_keys($conditions), $keys))
throw new Exception(sprintf('Non-extistent key(s) in table: ', implode(', ', $invalidKeys)));
// find the row that meets all conditions
$targetRow = null;
foreach($table->childNodes as $tr){
// internal counter because we can't rely on DOM index
$idx = 0;
foreach($tr->childNodes as $td){
if($td instanceof DomText)
continue;
$value = trim($td->nodeValue);
// check if all conditions match
if(array_key_exists($keys[$idx], $conditions))
$targetRow = ($value != $conditions[$keys[$idx]]) ? null : $tr;
$idx++;
}
// stop if we found a match
if($targetRow)
break;
}
if(!$targetRow)
throw new Exception('No row matches your conditions');
// build an array with row cells
$values = array();
$idx = 0;
foreach($targetRow->childNodes as $td)
if(!($td instanceof DomText))
$values[$keys[$idx++]] = trim($td->nodeValue);
// return the cell value if a specific cell was requested
if($cellName !== null)
return isset($values[$cellName]) ? $values[$cellName] : null;
// otherwise return all values from the matched row
return $values;
}
It uses DomDocument because the question wasn't tagged as simplehtmldom
#OP: in your case you would use it like:
$html = file_get_contents('http://www.arifoorum.com/test/html.htm');
$dom = new DomDocument();
$doc->preserveWhiteSpace = false;
$dom->loadHtml($html);
$table = $dom->getElementsByTagName('table')->item(0);
print getCellValue($table, 'V_V', array(
'mõõdikud' => '01',
'Name 2' => 'külm',
));

Related Links

How to list last 20 results from my database? [duplicate]
Class not found Die and Dump environment variable keys
Array to string conversion is not working in opencart?
Server Error 500 - Ubuntu
Internal Server Error - Is it my syntax?
Submitting bootstrap form via Ajax passes wrong parameters
I use change event but I when I put value to the select option on course the major does not append data..
Display posts on homepage - Custom PHP CMS
How to get specific substring digit from string using php? [duplicate]
How to get featured image from post
How to catch 401 response in Laravel/Lumen?
Remove the top level array and merge sub array into one
PHP GET Variable [duplicate]
JQuery AJAX is encoding API Token for Laravel Application, results in 401 Unauthorized
ShareThis webservice on Drupal 7 site has stopped working (images not appearing)
Wecoomerce custom query to filter products loop

Categories

HOME
spring-data
user-interface
memory
xcode
yahoo-weather-api
bots
tcsh
stacktrace.js
braintree
ant
adobe
mongoid
mathematical-optimization
openfoam
migrate
multi-step
jpa-2.0
uima
fosuserbundle
bluemix-mobile-services
hdl
generator
pymongo
chronicle-queue
ipa
running-object-table
dnsmasq
guice
tilemill
autodesk-model-derivative
instructions
argv
commonsware-cwac
quick-nimble
seafile-server
ms-media-foundation
assertions
recovery
text-decorations
google-now
reset
system-on-chip
coverage.py
elasticsearch-aggregation
doctrine-extensions
django-static-precompiler
karabiner
solr-query-syntax
sigsegv
webvtt
popen
gmaps.js
.when
d3v4
ndk-build
mathjs
system.web.optimization
msbuild-4.0
jenkins-workflow
powerpoint-2013
gnucash
polymerfire
voronoi
apache-commons-digester
blank-line
shoes
deque
scala-swing
tidyr
thoughtworks-go
cakephp-2.2
inequality
ecos
accountmanager
pre
cloo
discovery
disjoint-union
google-code-prettify
pacman
itunes-sdk
image-scanner
series-40
algol68
kolite
vows
printqueue
ios-4.2
chunking
ninject-interception
cassini
activestate
urchin
aspbutton
iphone-sdk-3.1.3
downloadfile

Resources

Encrypt Message