SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Am I using in_array correctly?

    Hi Sitepointers,

    I am trying to get unique items into an array from an XML file.

    However my code is puching every item into the array, Where am I going wrong?

    Many thanks

    PHP Code:
    <?php

    $xml 
    simplexml_load_file('mercers.xml');

    $property_distinct_beds = array();
    foreach(
    $xml->property as $property) {
        
    $property_bedrooms $property->beds;
        if (!
    in_array($property_bedrooms$property_distinct_bedsTRUE)) {
            
    array_push($property_distinct_beds$property_bedrooms);
        }
    }


    foreach (
    $property_distinct_beds as $distinct_beds) {
        echo 
    $distinct_beds.'<br />';
    }

    ?>
    The files are located:
    http://www.spanishproperty.es/mobile/xml_test.php
    http://www.spanishproperty.es/mobile/mercers.xml

  2. #2
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    8
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Probably a type issue. Try casting your property_bedrooms to int type.

  3. #3
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Surely since it's the same variable type going into the array and we are comparing the same type using in_array, it should work.

    In any case I tried the following and it still doesn't work.

    PHP Code:
    $xml simplexml_load_file('mercers.xml');

    $property_distinct_beds = array();
    foreach(
    $xml->property as $property) {
        
    $property_bedrooms = (int) trim($property->beds);
        if (
    in_array($property_bedrooms$property_distinct_beds)) {
            
    array_push($property_distinct_beds$property_bedrooms);
        }
    }


    foreach (
    $property_distinct_beds as $distinct_beds) {
        echo 
    $distinct_beds.'<br />';


  4. #4
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    8
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why did you remove negation(!) on if sentence? If you add it, it works properly.

  5. #5
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it doesn't

    PHP Code:
    <?php

    $xml 
    simplexml_load_file('mercers.xml');
    $property_distinct_beds = array();
    foreach(
    $xml->property as $property) {
        
    $property_bedrooms $property->beds;
        if (!
    in_array($property_bedrooms$property_distinct_beds)) {
            
    array_push($property_distinct_beds$property_bedrooms);
        }
    }


    foreach (
    $property_distinct_beds as $distinct_beds) {
        echo 
    $distinct_beds.'<br />';
    }

    ?>

  6. #6
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    8
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I used this:

    Code PHP:
     <?php
     
    $xml = simplexml_load_file('mercers.xml');
     
    $property_distinct_beds = array();
    foreach($xml->property as $property) {
        $property_bedrooms = (int) trim($property->beds);
        if (!in_array($property_bedrooms, $property_distinct_beds)) {
            array_push($property_distinct_beds, $property_bedrooms);
        }
    }
     
     
    foreach ($property_distinct_beds as $distinct_beds) {
        echo $distinct_beds.'<br />';
    }  
     
    ?>

    This produces the following output:
    2
    3
    4
    5
    1
    6
    7
    0

    Is this not what you intended?

  7. #7
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.

    Yes that works.

    That is what was intended.

    My mistake.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •