SitePoint Sponsor

User Tag List

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

    in_array not working in function

    Hi Sitepointers.

    Call me old school, but I don't think much of the new sitepoint design.

    I am trying to get an array of distinct items using the following code...however the function seems to add everything to the array with it's in the array or not.

    Please help

    PHP Code:


    $xml 
    simplexml_load_file($kyero_xml);



    $property_provinces_array kyero_xml_provinces($xml);
    $output $output.'<p> The kyero feed contains the following distinct provinces</p>';
    foreach (
    $property_provinces_array AS $property_province_name) {
        
    $output $output.$property_province_name.'<br />';
    }


    function 
    kyero_xml_provinces($xml) {
        
    $kyero_xml_provinces = array();
        foreach (
    $xml->property as $Property) {    
            
    $property_province $Property->province;
            if (!
    in_array($property_province$kyero_xml_provinces)) {
                
    array_push($kyero_xml_provinces$property_province);
            }
        }
        return 
    $kyero_xml_provinces;


  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    458
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Is there any chance the array somehow gets a zero in it? I am reading elsewhere that it will always return true if that's the case, though that might be version dependant.
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    try

    Code php:
    $property_province = (string) $Property->province;

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't see offhand how that would be. Did you do a var_dump() or something right after calling the function to be sure?

    And just for fun, here are a couple other ways to get the result you're after.

    Use the value as the key. Then you know for sure only one instance of a value will be in there. This wouldn't work for values which would be invalid for an array key.
    PHP Code:
    function kyero_xml_provinces($xml) {
        
    $list = array();
        foreach (
    $xml->property as $Property) {    
            
    $province $Property->province;
            
    $list$province ] = $province;
        }
        return 
    $list;
        return 
    array_values$list ); // use this line if you don't want the return value to have those keys

    Just build the whole list and uniquify it at the end. This might actually be more efficient than the if in your original code, but I don't know for sure.
    PHP Code:
    function kyero_xml_provinces($xml) {
        
    $list = array();
        foreach (
    $xml->property as $Property) {    
            
    $list[] = $Property->province;
        }
        return 
    array_unique$list );

    - Robert

  5. #5
    Non-Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try out the below code.

    in_array — Checks if a value exists in an array

    It looks like your intention is to check against the index of the array, rather than the value. The values in the array are arrays.

    Try using array_key_exists() instead.

    For example:

    if (array_key_exists($page, $template)) {
    $title = $template[$page]['title'];
    $model = $template[$page]['model'];
    echo "yes";
    }
    else {
    $title = $template['index']['title'];
    $model = $template['index']['model'];
    echo "no";
    }

    Hope it works.


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
  •