-1

In the following xml file "catalog", there are 5 variables for each book id.

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella</author>
      <title>XML Developer</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
   </book>
   <book id="bk102">
      <author>Ralls</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
   </book>
   <book id="bk103">
      <author>Corets</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
    </book>
</catalog>

How to get the json array for a particular Book id (i.e book id ="102") in php. I searched the web but could'not find the soluation in required manner. Can any body help me in this please? the xml file: www.mywebsite.com/catlog.xml

$data = json_decode($json, true);  
  foreach ( $data['catlog'] as $row ) {  
                        if ($row['book id'] =='102') { 
                          // do the work here
                   }
         }
ourguru dev
  • 23
  • 1
  • 5

2 Answers2

0

You cannot actually do that. The better thing you can do is parse it into simplexml then iterate through the objects then just do a counter if the particular object has already the attribute of "102"

0

What I would recommend is debugging, with print_r to find out if the key value 'book id' is there or not. It will help traverse the JSON structure later on as well.

    foreach ( $data['catalog'] as $row ) {  
        if ($row['book id'] == '102') {
            // do the work        
        } else {
            // for debugging
            echo print_r($row, true);
        }
    }

I also just noticed you spelled catalog wrong... $data['catlog'] to $data['catalog'];