9

I am trying to come up with a means of working with what could potentially be very large array sets. What I am doing is working with the facebook graph api.

So when a user signs up for a service that I am building, I store their facebook id in a table in my service. The point of this is to allow a user who signs up for my service to find friends of their's who are on facebook and have also signed up through my service to find one another easier.

What I am trying to do currently is take the object that the facebook api returns for the /me/friends data and pass that to a function that I have building a query to my DB for the ID's found in the FB data which works fine. Also while this whole bit is going on I have an array of just facebook id's building up so I can use them in an in_array scenario. As my query only returns facebook id's found matching

While this data is looping through itself to create the query I also update the object to contain one more key/value pair per item on the list which is "are_friends"=> false So far to this point it all works smooth and relatively fast, and I have my query results. Which I am looping over.

So I am at a part where I want to avoid having a loop within a loop. This is where the in_array() bit comes in. Since I created the array of stored fb id's I can now loop over my results to see if there's a match, and in that event I want to take the original object that I appended 'are_friends'=>false to and change the ones in that set that match to "true" instead of false. I just can't think of a good way without looping over the original array inside the loop that is the results array.

So I am hoping someone can help me come up with a solution here without that secondary loop

The array up to this point that starts off as the original looks like

Array(
    [data](
            [0] => array(
                         are_fb_friends => false
                         name => user name
                         id => 1000
                        )
            [1] => array(
                         are_fb_friends => false
                         name => user name
                         id => 2000
                        )
            [2] => array(
                         are_fb_friends => false
                         name => user name
                         id => 3000
                        )
            )
    )

As per request

This is my current code logic, that I am attempting to describe above..

public function fromFB($arr = array())
{
    $new_arr = array();
    if((is_array($arr))&&(count($arr) > 0))
    {
        $this->db->select()->from(MEMB_BASIC);
        $first_pass = 0;
        for($i=0;$i < count($arr);$i++)
        {
            $arr[$i]['are_fb_friends'] = "false";
            $new_arr[] = $arr[$i]['id'];
            if($first_pass == 0)
            {
                $this->db->where('facebookID', $arr[$i]['id']);
            }
            else
            {
                $this->db->or_where('facebookID', $arr[$i]['id']);
            }
            $first_pass++;
        }
        $this->db->limit(count($arr));
        $query = $this->db->get();
        if($query->num_rows() > 0)
        {
            $result = $query->result();
            foreach($result as $row)
            {
                if(in_array($row->facebookID, $new_arr))
                {
                    array_keys($arr, "blue");
                }
            }
        }
    }
    return $arr;
}
chris
  • 36,115
  • 52
  • 143
  • 252

1 Answers1

7

To search a value and get its key in an array, you can use the array_search function which returns the key of the element.

$found_key = array_search($needle, $array);

For multidimensional array search in PHP look at https://stackoverflow.com/a/8102246/648044.

If you're worried about optimization I think you have to try using a query on a database (with proper indexing).

By the way, are you using the Facebook Query Language? If not give it a try, it's useful.

Community
  • 1
  • 1
Guglie
  • 2,121
  • 1
  • 24
  • 43
  • Not currently using the FBQL, what we are doing needs accessibility to a point across devices which to my current knowledge won't work for us (I could be wrong). But in general the graph API's serve us for most of our general needs as far as FB related stuff goes. – chris Mar 11 '13 at 16:30
  • On a side note, the `array_search()` will that return the index of the array within the array its searching? or the index within the array it finds the search. In other words will it from the example above if I search for 3000 will it return a "2" or "id" – chris Mar 11 '13 at 16:34
  • array search I think may work for some of my later needs, however the link to another post in sorts nailed it. I modified it a little but overall it was spot on to the need, even though not really wanting to foreach over the data set an extra time, I think though thats an unavoiable evil. – chris Mar 13 '13 at 16:40