Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP find the array index key of multi dimensional array to update array

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;
}
like image 419
chris Avatar asked Mar 11 '13 07:03

chris


People also ask

How get key of multidimensional array in PHP?

array_keys() returns the keys, numeric and string, from the array . If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

What is array_keys () used for?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

How do you check if a key exists in an array PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

How do I find a multidimensional array?

Multidimensional array search using array_search() method: The array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path.


1 Answers

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.

like image 200
Guglie Avatar answered Oct 17 '22 05:10

Guglie