Jump to content

us2rn4m2

Members
  • Posts

    36
  • Joined

  • Last visited

Profile Information

  • Gender
    Male
  • Location
    France

us2rn4m2's Achievements

Newbie

Newbie (1/5)

1

Reputation

  1. It's a little bit heavy... $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <Physical> <Catalog> <EquipmentSpec> <Reference>MAT_48985</Reference> <Type>Duplexeur</Type> <Characteristic><CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>0</CharacteristicValue></Characteristic> </EquipmentSpec> <EquipmentSpec> <Reference>MAT_6890</Reference> <Type>Node</Type> <Characteristic><CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>2</CharacteristicValue></Characteristic> </EquipmentSpec> <EquipmentSpec> <Reference>MAT_50</Reference> <Type>Single</Type> <Characteristic><CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>5</CharacteristicValue></Characteristic> </EquipmentSpec> </Catalog> <Installed> <Equipment> <Id>26</Id><Reference>MAT_4</Reference> <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>160</CharacteristicValue></Characteristic> </Equipment> <Equipment> <Id>27</Id><Reference>MAT_6890</Reference> <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>140</CharacteristicValue></Characteristic> </Equipment> <Equipment> <Id>28</Id><Reference>MAT_50</Reference> <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>180</CharacteristicValue></Characteristic> </Equipment> </Installed> </Physical> XML; $e = new SimpleXMLElement($xml); $values = array( 'Type' => array(), 'Id' => array(), 'Height' => array() ); foreach ($e->Catalog->EquipmentSpec as $equipmentSpec) { $values['Type'][] = (string) $equipmentSpec->Type; } foreach ($e->Installed->Equipment as $equipment) { $values['Id'][] = (string) $equipment->Id; foreach ($equipment->Characteristic as $characteristic) { if ($characteristic->CharacteristicName == 'Height') { $values['Height'][] = (string) $characteristic->CharacteristicValue; } } } $sql = array(); foreach($values as $array) { foreach($array as $k => $v) { if(!array_key_exists($k, $sql)) { $sql[$k] = "INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('" . $values['Type'][$k] . "','" . $values['Id'][$k] . "', '" . $values['Height'][$k] . "')"; // $req = new requete($site->db, $sql[$k]); echo $sql[$k] . '<br>'; /* output: INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Duplexeur','26', '160') INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Node','27', '140') INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Single','28', '180') */ } } } print_r($sql); /* Array ( [0] => INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Duplexeur','26', '160') [1] => INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Node','27', '140') [2] => INSERT INTO machines (`Type`,`Id`,`Height`) VALUES ('Single','28', '180') ) */
  2. replace foreach ($xml->xpath('Physical') as $spec) by foreach ($xml->xpath('//Catalog') as $spec)
  3. Screenshot: http://img4.hostingpics.net/pics/186791NetbeansThemeWelcome.png Download:https://www.box.com/s/x3fy59klxrbvcv3zwnjz
  4. // Movie foreach($array as $k => $v) { if(is_array($v) && array_key_exists('Movie', $v)) { echo $v['Movie']; } } echo $array['APIParameters']['Movie']; // MovieName foreach($array['MovieReviews'] as $k => $v) { echo $v['MovieName']; } echo $array['MovieReviews'][0]['MovieName'];
  5. echo $feed->sizes->size[5]['id']; // 38
  6. update // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } $bunch = array(); // add this line // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; }
  7. $xml = simplexml_load_file('http://applesold.com/assets/cache/tumblrposts.txt'); $tag = null; $bunch = array(); echo '<ul>'; foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') { $bunch[$i] = '<li><b>infographic</b></li>'; $tag = true; } else { $bunch[$i] = '<li>non-infographic</li>'; } $i++; } // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; } } } /** Result: non-infographic non-infographic non-infographic non-infographic infographic non-infographic non-infographic non-infographic non-infographic non-infographic non-infographic YES, has an infographic tag */
  8. // ! Different output with PHP 5.3 and 5.4 // PHP 5.4 if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') { echo '<pre>'; print_r($post->tag); /* SimpleXMLElement Object ( [0] => real estate [1] => vets [2] => Veterans Affairs [3] => VA [4] => home loans [5] => mortgage info [6] => video [7] => Frank Garay [8] => Brian Stevens [9] => buying a house ) SimpleXMLElement Object ( [0] => real estate [1] => dream home [2] => buying a hosue [3] => neighbours [4] => compatibility [5] => lifestyle [6] => Frank Garay [7] => Brian Stevens ) SimpleXMLElement Object ( [0] => real estate [1] => news [2] => hoopla [3] => mortgage rates [4] => video [5] => Frank Garay [6] => Brian Stevens [7] => hype ) SimpleXMLElement Object ( [0] => green [1] => lifestyle [2] => energy conservation [3] => NEST [4] => personal finance [5] => real estate [6] => home systems [7] => Energy Star ) SimpleXMLElement Object ( [0] => real estate [1] => buying a house [2] => internet [3] => www [4] => infographic [5] => home search ) */ // PHP 5.3 echo '<pre>'; print_r($post->tag); /* SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => green ) SimpleXMLElement Object ( [0] => real estate ) */
  9. $xml = ' <m:ad xmlns:m="http://www.w3c.org/soap"> <title><![CDATA[TITLE]]></title> <phone>123456789</phone> <attributeGroup> <attribute id="14" name="A1">40</attribute> <attribute id="15" name="A2">50</attribute> </attributeGroup> </m:ad>'; $reader = new XMLReader(); $reader->xml($xml); $id = null; $name = null; while($reader->read() ) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "attribute") { $id = $reader->getAttribute('id'); $name = $reader->getAttribute('name'); printf("id=%s, name=%s\n", $id, $name); } if(isset($id) && isset($name) && $reader->nodeType == XMLReader::TEXT) { printf("value = %s", $reader->value); echo '<br />'; } } /** Result: id=14, name=A1 value = 40 id=15, name=A2 value = 50 */ 2nd method $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $values, $index); xml_parser_free($p); foreach($values as $v) { if(array_key_exists('attributes', $v)) { $attributes = $v['attributes']; if(array_key_exists('ID', $attributes) && array_key_exists('NAME', $attributes)) { echo 'id = ' . $attributes['ID'] . ', '; echo 'name = ' . $attributes['NAME'] . ', '; echo 'value ' . $v['value'] . ' <br />'; } } } /** Result id = 14, name = A1, value 40 id = 15, name = A2, value 50 */
  10. foreach($feed->posts->post as $post) { if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') { // print_r($post->tag); echo '<b>YES</b>'; } else { echo 'no'; } $i++; } } else { // ... } }
  11. Your questions associate to your code are not very clear... So I show you the method that I use to check the presence of "certain tag". // check the presence of 'post' if(property_exists($xml, 'posts')) { echo 'post'; } else { // ... } // check the presence of 'regular-title' if(property_exists($xml->posts->post[0], 'regular-title')) { echo 'regular-title'; } else { // ... } // check the presence of 'tag' foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { echo 'tag'; } else { // ... } }
  12. <?php foreach($xml->posts->post as $post) { foreach($post->tag as $tag) { if ($tag == "infographic") { echo "INFOGRAPHIC<br />" ; } else { echo "non-INFOGRAPHIC<br />"; } } }
  13. $xml = ' <ul id="0" class="nestedsortable"> <li id="29"> <div class="itemrow">Home</div> <ul id="29"></ul> </li> <li id="37"> <div class="itemrow">Page 1</div> <ul id="37"> <li id="30"> <div class="itemrow">Page 3</div> <ul id="30"></ul> </li> </ul> </li> <li id="36"> <div class="itemrow">Page 2</div> <ul id="36"></ul> </li> <li id="31"> <div class="itemrow">Page 4</div> <ul id="31"></ul> </li> </ul>'; $array = array(); $id = null; // Iterate the array with some conditions function iterate($a) { global $array; foreach ($a as $k) { if (is_array($k)) { if (array_key_exists('tag', $k) && $k['tag'] == 'UL') { if (array_key_exists('level', $k)) { if (array_key_exists('attributes', $k) && array_key_exists('ID', $k['attributes'])) { if ($k['level'] == 3) { // 3 represent <ul id="29"> || <ul id="37"> || <ul id="36"> || <ul id="31"> $array[0][] = $k['attributes']['ID']; $id = $k['attributes']['ID']; } if ($k['level'] == 5) { // 5 represent <ul id="30"> $array[$id][] = $k['attributes']['ID']; } } } } iterate($k); } } return $array; } // Parse XML data into an array structure - http://php.net/manua...into-struct.php $parser = xml_parser_create(); xml_parse_into_struct($parser, $xml, $values, $index); xml_parser_free($parser); $item = iterate($values); // echo '<pre>'; // print_r($values); // echo '<pre>'; echo '<pre>'; print_r($item); /** Result: Array ( [0] => Array ( [0] => 29 [1] => 37 [2] => 36 [3] => 31 ) [37] => Array ( [0] => 30 ) ) */
  14. I'm not a troller. It was just interresting for me to defends my opinion because I don't forget when I start (alone wihout a teacher) to learn PHP. And before to 'close my mouth', 'No !' I'm not a lier I know Zend: http://forums.zend.c...hp?f=69&t=77938 http://forums.zend.c...p?f=69&t=104538 http://forums.zend.c...hp?f=69&t=72553
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.