SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array_search with needle array

    I am trying to get the key of an associative array of database results, based on the value of the 'card_id' field. My best guess was to do an array_search, using "array ('card_id' => '1')" as the needle, but this doesn't work. It works if I create an array, but not when I make the array using a db query.

    Here's an example of the results I'm trying to search through:

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    card_id] => 1
            
    )

        [
    1] => Array
            (
                [
    card_id] => 2
            
    )

        [
    2] => Array
            (
                [
    card_id] => 3
            
    )


    Any ideas?

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your array is more complex (an extra dimension) that it needs to be.
    Is it possible to re-format it wherever it's coming from to be a flat 1D array?
    Is there other data being stored?

  3. #3
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    I think your array is more complex (an extra dimension) that it needs to be.
    Is it possible to re-format it wherever it's coming from to be a flat 1D array?
    Is there other data being stored?
    Of course, I just took out the other data to make the example simpler.

    Cory

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /**
    * Extract cart data for specific cart
    * @param array $array
    * @param int $cart_id
    * @return mixed
    */
    function get_cart_data($array$cart_id) {
        foreach(
    $array as $cartData) {
            if(
    $cartData['cart_id'] == $cart_id) return $cartData;
        }
        return 
    false;
    }

    //Data for cart ID 2
    if($data get_cart_data($cartData2)) print_r($data);
    else echo 
    'Cart data not found'

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    I am trying to get the key of an associative array of database results, based on the value of the 'card_id' field. My best guess was to do an array_search, using "array ('card_id' => '1')" as the needle, but this doesn't work.
    Sure it does.

    PHP Code:
    $a = array(
        array(
    't' => 1),
        array(
    't' => 5),
        array(
    't' => 3),
    );

    echo 
    array_search(array('t' => 5), $a); // 1 

  6. #6
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See, that's the thing. I was able to get an example like that working too, but when I tried it with database results, it would never work. So I'm wondering if there is some other format for an associative array from a db that I have to match in the needle.

    Cory

  7. #7
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By associative array of results, do you mean you're using mysql_fetch_assoc? If so it will only return results for one row at a time, you need to loop over it.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    So I'm wondering if there is some other format for an associative array from a db that I have to match in the needle.
    It depends on what function you're using: fetch_row, fetch_assoc or fetch_array.

  9. #9
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it's been looped over using fetch_assoc and stored as a single array.

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you're "needle" array doesn't contain all keys or they are in wrong order.

    Try this function

    PHP Code:
    function array_search_intersect($ary$needle) {
        
    $out = array();
        foreach(
    $ary as $k => $v)
            if(
    count(array_intersect_assoc($v$needle)) == count($needle))
                
    $out[] = $k;
        return 
    $out;



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
  •