SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    316
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Search and Retrieve specific value from Array

    I have an array output results in this format

    Code:
    Array ( [0] => stdClass Object ( [ID] => 8 [Value] => 7 ) [1] => stdClass Object ( [ID] => 9 [Value] => 8 ) [2] => stdClass Object ( [ID] => 10 [Value] => 7 ) [3] => stdClass Object ( [ID] => 11 [Value] => 7 ) [4] => stdClass Object ( [ID] => 12 [Value] => 6 ) [5] => stdClass Object ( [ID] => 13 [Value] => 9 ) [6] => stdClass Object ( [ID] => 14 [Value] => 5 ) [7] => stdClass Object ( [ID] => 15 [Value] => 7 ) [8] => stdClass Object ( [ID] => 16 [Value] => 7 ) [9] => stdClass Object ( [ID] => 17 [Value] => 3 ) [10] => stdClass Object ( [ID] => 18 [Value] => 5 ) )
    Now I would like to search the array for a specific ID and retrieve its associated value.

    For example say $searchID = 13. So search the arrary for ID 13 and should return the result of Value as 9.

    I have tried multiple solutions but each time results in null value which means I am doing something wrong.

    I can simply print a specific array value like this $outputarray[5]->Value which would output the array to me. But any way to search and retrieve values as per above array?
    Any help is highly appreciated.

    Thanks
    FreelanceNext.com - Freelance Projects / Jobs & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  2. #2
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    316
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ok I found the solution searching the forum and the web. Thanks. The thread may be closed
    FreelanceNext.com - Freelance Projects / Jobs & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,882
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Please post solution for the benefit of others.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  4. #4
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    316
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure I am not sure if I could post solutions from other forums on this forum. If its a yes, I would gladly post the solution
    FreelanceNext.com - Freelance Projects / Jobs & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,882
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Yes please post the solution.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    316
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ok this is the solution that worked for me

    Code:
    $arrayval = here the array comes;
    $id = X the value you wish to search for ID;
    
    foreach($arrayval as $item){
            if ($item->ID == $id)
                return $item->Value;
        }
        return "not found";
    }
    FreelanceNext.com - Freelance Projects / Jobs & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,882
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    I have just been playing and discovered:

    PHP Code:
    <?php 

    $arrayVal 
    = array

      
    8  => 7,
      
    9  => 8,
      
    10 => 7,
      
    11 => 7,
      
    12 => 6,
      
    13 => 9,
      
    14 => 5,
      
    15 => 7,
      
    16 => 7,
      
    17 => 3,
      
    18 => 5,
    );

    $obj2 = new stdClass(); // on-the-fly!
      
    $obj2->{8}  = 7;
      
    $obj2->{9}  = 8;
      
    $obj2->{10} = 7;
      
    $obj2->{11} = 7;
      
    $obj2->{12} = 6;
      
    $obj2->{13} = 9;
      
    $obj2->{14} = 5;
      
    $obj2->{15} = 7;
      
    $obj2->{16} = 7;
      
    $obj2->{17} = 3;
      
    $obj2->{18} = 5;

    $id 13;
    echo 
    '<pre>';
      echo 
    '$arrayVal = ';
        
    print_r($arrayVal); 
        echo 
    'function getIdFromObject( $id, $arrayVal)<br />';
        echo 
    'result = ' .getIdFromObject$id$arrayVal );
        echo 
    '<br /><br /><br />';

      echo 
    '$obj2 = ';
        
    print_r($obj2); 
        echo 
    'getIdFromObject( $id, $obj2 )<br />';
        echo 
    'result = ' .getIdFromObject$id$obj2 );
        echo 
    '<br /><br /><br />';
        
        
    $obj = (object) $arrayVal;
      echo 
    '$obj = ';
        
    print_r($obj); 
        echo 
    'getIdFromObject( $id, $obj  )<br />';
        echo 
    'result = ' .getIdFromObject$id$obj ); 
    echo 
    '</pre>';

    function 
    getIdFromObject$id$objArray )
    {
      
    $result 'cannot find $id = ' .$id;

      
    // accepts both Obj AND arrays 
      
    foreach( $objArray as $key => $value )
      {
        if (
    $key == $id):
          
    $result 'Found $id = ' .$value;
        endif;  
      }
      return 
    $result;
    }

    Output:
    $arrayVal = Array
    (
    [8] => 7
    [9] => 8
    [10] => 7
    [11] => 7
    [12] => 6
    [13] => 9
    [14] => 5
    [15] => 7
    [16] => 7
    [17] => 3
    [18] => 5
    )
    function getIdFromObject( $id, $arrayVal)
    result = Found $id = 9


    $obj2 = stdClass Object
    (
    [8] => 7
    [9] => 8
    [10] => 7
    [11] => 7
    [12] => 6
    [13] => 9
    [14] => 5
    [15] => 7
    [16] => 7
    [17] => 3
    [18] => 5
    )
    getIdFromObject( $id, $obj2 )
    result = Found $id = 9


    $obj = stdClass Object
    (
    [8] => 7
    [9] => 8
    [10] => 7
    [11] => 7
    [12] => 6
    [13] => 9
    [14] => 5
    [15] => 7
    [16] => 7
    [17] => 3
    [18] => 5
    )
    getIdFromObject( $id, $obj )
    result = Found $id = 9
    Last edited by John_Betong; May 10, 2013 at 06:09. Reason: added search array and obj
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  8. #8
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    316
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ok I think @John_Betong would work great as we simply create a function which could handle both the arrays as well as object arrays. Great solution. Thanks for the help
    FreelanceNext.com - Freelance Projects / Jobs & more...
    BargainNext.com - coupons / deals / bargains / offers & more...


Tags for this Thread

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
  •