2
    $rss_tags = array(
    'drawNo',
    'drawTime',
    'result',
    );
  $rss_item_tag = 'draw';
  $rss_url = "http://applications.opap.gr/DrawsRestServices/kino/drawDate/21-12-2014.xml";

  $rssfeed = rss_to_array($rss_item_tag,$rss_tags,$rss_url);


  echo '<pre>';
  print_r($rssfeed);

/*$total=1;
  foreach($rssfeed as $item)
        {
          if($total<750){
          echo '<div><h1>Κλήρωση: '.$item['drawNo']. '  Αριθμοί: ' .$item['result'].'</h1></div>';
          $total++;
          }
        }
*/

  function rss_to_array($tag, $array, $url) {
    $doc = new DOMdocument();
    $doc->load($url);
    $rss_array = array();
    $items = array();
    foreach($doc->getElementsByTagName($tag) AS $node) { //se auth thn epanalhpsh epanalamvenete toses fores oses oi klhrwseis tis hmeras
      foreach($array AS $key => $value) { //$array einai ta rss tags
          if($value=="result"){
            for($i=1;$i<=20;$i++){$items["result"] = $node->getElementsByTagName("result")->item(0)->nodeValue;}
          }
          $items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;

      }
      array_push($rss_array, $items);
    }
    return $rss_array;
  }

Hello phpers, this is my code to read some xml information but i have a big big big problem.

Firstly check the xml source: http://applications.opap.gr/DrawsRestServices/kino/drawDate/21-12-2014.xml

The structure is like that:

<draws>
  <draw>
     <drawNo>
     <drawTime>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>
     <result>

and my code returns only the first entry of the but i want to get both 20 entries could someone help me?

arispapapro
  • 319
  • 1
  • 3
  • 10

1 Answers1

0

I'm not sure what exactly do you want to output, but maybe this will suit your purpose.

The code below converts xml to array.

<?php
$xmlstring = file_get_contents("http://applications.opap.gr/DrawsRestServices/kino/drawDate/21-12-2014.xml");

// converts $xmlstring to multidimensional array
$arrayMD = json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);

echo "<pre>";
var_dump($arrayMD);
echo "</pre>";

// converts multidimensional array to one-dimensional array
// in other words: "flattens" a multidimensional array
$array = new RecursiveIteratorIterator(new RecursiveArrayIterator($arrayMD));
foreach($array as $v) {
  echo $v . "<br>";
}
?>

The output would be as follows:

array(1) {
  ["draw"]=>
  array(157) {
    [0]=>
    array(3) {
      ["drawNo"]=>
      string(6) "475781"
      ["drawTime"]=>
      string(25) "2014-12-21T09:00:00+02:00"
      ["result"]=>
      array(20) {
        [0]=>
        string(1) "6"
        [1]=>
        string(1) "7"
        [2]=>
        string(2) "12"
        [3]=>
        string(2) "13"
        [4]=>
        string(2) "15"
        [5]=>
        string(2) "16"
...

Reference:

Update:

To access some specific element of the multidimensional array:

$someVar = $arrayMD["draw"][0]["result"][7];
echo $someVar; // outputs: 12

Additionally, have a look at the list function (assigns variables as if they were an array) or extract (imports variables into the current symbol table from an array).

Or the easiest way is just to use several foreach to output them, sum the numbers, select every fifth "result" value of every "draw" or anything else. (This depends on what are you going to do with those values next.)

Community
  • 1
  • 1
Dmytro Dzyubak
  • 1,562
  • 2
  • 22
  • 35
  • Dmytro could u tell me how i can put all these [0]=> string(1) "6" [1]=> string(1) "7" [2]=> string(2) "12" [3]=> string(2) "13" [4]=> string(2) "15" [5]=> string(2) "16" into variables so i can use them? – arispapapro Jan 05 '15 at 00:41
  • echo $array['draw'][$k]['result'][$i] this is the answer thank you dmytro – arispapapro Jan 05 '15 at 15:18