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

时间:2021-02-20 21:29:44

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.

我正在尝试提出一种处理可能非常大的数组的方法。我正在做的是使用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.

因此,当用户注册我正在构建的服务时,我将他们的facebook id存储在我的服务中的表中。这样做的目的是允许注册我的服务的用户找到他们在Facebook上的朋友,并通过我的服务注册,以便更容易找到彼此。

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

我目前正在尝试做的是获取facebook api为/ me / friends数据返回的对象,并将其传递给我已经为我的数据库构建查询的函数,以获取在FB数据中找到的ID,该工作正常。虽然这一切都在进行,但我有一个只是facebook id的数组,所以我可以在in_array场景中使用它们。因为我的查询只返回找到的facebook id匹配

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.

虽然这个数据循环通过自己来创建查询,但我还更新了对象,以便在列表中每个项目包含一个键/值对,这是“are_friends”=> false到目前为止,它一切顺利且相对较快,我有我的查询结果。我正在循环。

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.

所以我处在一个我希望避免在循环中循环的部分。这是in_array()位的来源。由于我创建了存储的fb id的数组,我现在可以遍历我的结果以查看是否存在匹配,并且在那个事件中我想采用我附加的原始对象'are_friends '=> false to并更改该集合中与“true”匹配的那些而不是false。如果没有循环遍历结果数组循环中的原始数组,我就无法想到一个好方法。

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;
}

1 个解决方案

#1


5  

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.

要搜索值并在数组中获取其键,可以使用array_search函数返回元素的键。

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

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

有关PHP中的多维数组搜索,请访问https://*.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.

顺便问一下,您使用的是Facebook查询语言吗?如果不试一试,它很有用。

#1


5  

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.

要搜索值并在数组中获取其键,可以使用array_search函数返回元素的键。

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

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

有关PHP中的多维数组搜索,请访问https://*.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.

顺便问一下,您使用的是Facebook查询语言吗?如果不试一试,它很有用。