Search code examples
phparrayscurlxpathsimple-html-dom

how to make an array xpath queries?


how to make an array like this, by using xpath???? Please help me I am currently attempting to extract values from an array that was generated by an SIMPLEXML/XPATH Query. I have had no success with my attempts if anyone can take look would be greatly appreciated.

 <ol class="font_8 timeline-delivered">
  <li class="timelineDate">28 Aug 2021</li>
    <li class="Timeline-event">
        <div class="A">AA</div>
        <div class="b">BB</div>
        <div class="C">CC</div>
    </li>
    <li class="timelineDate">29 Aug 2021</li>
    <li class="Timeline-event">
        <div class="A">AA</div>
        <div class="b">BB</div>
    </li>
    <li class="timelineDate">30 Aug 2021</li>
    <li class="Timeline-event">
        <div class="A">AA</div>
        <div class="b">BB</div>
        <div class="C">CC</div>
    </li>
    <li class="timelineDate">31 Aug 2021</li>
    <li class="Timeline-event">
        <div class="A">AA</div>
    </li>
 </ol>

Does anyone else have experience with this behavior and relative xpath queries in php?

$tabledata = $xpath->query('//ol/li[contains(@class, "timelineDate")   or contains(@class, "Timeline-even")]');

foreach($tabledata as $row  => $tr) {

  foreach($tr->childNodes as $td) {
     foreach($td->childNodes as $td) {
       $ShipmentDetail[$row][] = preg_replace('~[\r\n]+~', '', trim($td->textContent));
    }
}
  $ShipmentDetail[$row] = array_values(array_filter($ShipmentDetail[$row]));

}
echo '<pre>';
print_r($ShipmentDetail);

result array

    Array
    (
        [0] => Array
            (
                [0] => 28 Aug 2021
            )
        [1] => Array
            (
                [0] => AA
                [1] => BB
                [2] => CC
            )
        [2] => Array
            (
                [0] => 29 Aug 2021
            )

        [3] => Array
            (
                [0] => AA
                [1] => BB
            )

        [4] => Array
            (
                [0] => 30 Aug 2021
            )

        [5] => Array
            (
                [0] => AA
                [1] => BB
                [2] => CC
            )
        [6] => Array
            (
                [0] => 31 Aug 2021
            )

        [7] => Array
            (
                [0] => AA

            )
    )

I want to be like this

    Array
    (
        [0] => Array
            (
                [0] => 28 Aug 2021
                [1] => AA
                [2] => BB
                [3] => CC
            )
        [1] => Array
            (
                [0] => 29 Aug 2021
                [1] => AA
                [2] => BB
            )
        [2] => Array
            (
                [0] => 30 Aug 2021
                [1] => AA
                [2] => BB
                [3] => CC
            )
        [3] => Array
            (
                [0] => 31 Aug 2021
                [1] => AA
            )
    )

Solution

  • This will return exactly the same result as you want:

    $ShipmentDetail = array();
    
    $DOM = new DOMDocument();
    $DOM->loadHTML($html);
    $xpath = new DOMXPath($DOM);
    $event_nodes = $xpath->query('//li[@class="Timeline-event"]');
    foreach ($event_nodes as $event_node) {
        $event = array();
        $event_date = $xpath->evaluate('string(./preceding-sibling::li[1]/text())', $event_node);
        $event[] = $event_date;
        $details = $xpath->query('./div', $event_node);
        foreach ($details as $detail_node) {
            $detail = $xpath->evaluate('string(./text())', $detail_node);
            $event[] = $detail;
        }
        $ShipmentDetail[] = $event;
    
    }