1

From an XML-file

<items>
    <item type="blue" />
    <item type="red" />
    <item type="blue" />
    <item type="red" />
    <item type="blue" />
    <item type="red" />
    <item type="blue" />
    <item type="red" />
    <item type="blue" />
    <item type="red" />
    <item type="blue" />
    <item type="red" />
</items>

I get the following array by SimpleXML:

object(SimpleXMLElement)#1 (1) { 
["item"]=> array(12) { 

    [0]=> object(SimpleXMLElement)#2 (1) { 
        ["@attributes"]=> array(1) { 
            ["type"]=> string(4) "blue" 
        } 
    } 

    [1]=> object(SimpleXMLElement)#3 (1) { 
        ["@attributes"]=> array(1) { 
            ["type"]=> string(3) "red" 
        } 
    } 

    [2]=> object(SimpleXMLElement)#4 (1) { … }
    [3]=> object(SimpleXMLElement)#5 (1) { … }
    [4]=> object(SimpleXMLElement)#6 (1) { … }
    [5]=> object(SimpleXMLElement)#7 (1) { … }
    [6]=> object(SimpleXMLElement)#8 (1) { … }
    [7]=> object(SimpleXMLElement)#9 (1) { … }
    [8]=> object(SimpleXMLElement)#10 (1) { … }
    [9]=> object(SimpleXMLElement)#11 (1) { … }
    [10]=> object(SimpleXMLElement)#12 (1) { … }
    [10]=> object(SimpleXMLElement)#12 (1) { … }
    [11]=> object(SimpleXMLElement)#13 (1) { … }
} }

I want to loop through all items and choose the first three with the type of blue:

1) Iter through all items
2) Check whether type == 'blue'
3) If yes, append the whole item-structure to another array or echo something

I do this alot with Python, where I can navigate in the parsed object (a dict array) by knowing nothing more than the basic structure of the XML. But in PHP I don't understand how the elements in the array are addressed.

When I look at tutorials or other questions here, it seems to me that there is no path-system like xpath. I haven't found a basix example like mine.

Helen
  • 76
  • 1
  • 5
  • This is a duplicate of: [SimpleXML: Selecting Elements Which Have A Certain Attribute Value](https://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-have-a-certain-attribute-value) – Octavio Galindo Sep 24 '17 at 06:10

1 Answers1

1

You Can do like this

$finalArray = [];
foreach($var['items'] as $item)
{
 if($item['@attributes']['type'] === 'blue')
   {
      $finalArray[] = $item;
   }
//check if you have 3 items already on the new array so that it will stop the loop
if(count($finalArray) == 3){
 return $finalArray;
}

}

pinoyCoder
  • 940
  • 7
  • 20
  • alternatively, you could use something like `for($i = 0; $i < $count && count($finalArray) < 3; $i++)` that would make the loop more compact. I'm also not a fan of having an unexpected `return` mid-function, especially a loop, imho `break` would be more appropriate. – William Perron Sep 11 '17 at 17:58
  • But i think that would slow down the process, since it will again loop just to check the items of the array – pinoyCoder Sep 11 '17 at 18:04
  • you're right, `count()` can be a pretty expensive function, however in this case it is negligible since we know for a fact that it will always start out empty, and stop at a small amount – William Perron Sep 11 '17 at 18:11
  • Did you run it? There is no 'items' in the array, but the problem before is $var. – Helen Sep 12 '17 at 02:09
  • hi elen the $var i have used is to assume that the result of your code that your are storing the result on it after you parse the xml file or string. You can change to anything you want. – pinoyCoder Sep 12 '17 at 11:02
  • Thanks! I get it working when I convert it with $obj = json_decode(json_encode($var),true); The SimpleXMLElement is tricky. – Helen Sep 13 '17 at 00:41
  • HI @Helen that's great, but you could use the solution which I wrote without using json_decode. – pinoyCoder Sep 13 '17 at 08:16
  • Thanks, pinoyCoder! I am using it now in this case and in general. It simplifies the array dramatically and makes it easier to handle. – Helen Sep 19 '17 at 04:14
  • that's great. Please mark it as the answer so that this thread will be helpful for others. – pinoyCoder Sep 19 '17 at 13:02