问题描述
我试图想出一个可能是非常大的数组集合的方法。我正在做的是使用Facebook图形API。所以当用户注册我正在建立的服务时,我将他们的Facebook id存储在表格中我的服务要点是允许注册我的服务的用户找到他们在Facebook上的朋友,并通过我的服务注册,以便更容易找到另一个。
目前我正在尝试的是将对象API API返回给 / me / friends
数据,并传递给我建立的一个函数查询到我的数据库,找到在FB数据中找到的ID,工作正常。此外,虽然这一切正在发生,我有一个只是Facebook id的建立的阵列,所以我可以使用它们在一个 in_array
场景中。因为我的查询只返回facebook id的匹配
虽然这个数据循环自己来创建查询,我还更新对象,每个项目包含一个以上的键/值对在的列表上,are_friends=> false
到目前为止,这一切都顺利而相对较快,我有查询结果。我正在循环。
所以我在一个部分,我想避免在循环中有一个循环。这是 in_array()
位的地方。由于我创建了存储的fb id的数组,我现在可以循环查看我的结果,看看是否有匹配,在那里事件我想采取我附加的原始对象'are_friends'=> false
并将该集合中匹配的那些更改为true而不是false。我只是想不到一个很好的方法,没有循环在结果数组的循环内的原始数组。
所以我希望有人可以帮助我来在这里没有辅助循环的解决方案
直到这一点的数组从原来的样子开始就像
Array(
[data](
[0] =>数组(
are_fb_friends => false
name =>用户名
id => 1000
)
[1] =>数组(
are_fb_friends => false
name =>用户名
id => 2000
)
[2] =>数组(
are_fb_friends => false
name =>用户名
id => 3000
)
)
)
根据请求
这是我目前的代码逻辑,我试图在上面描述..
公共函数fromFB($ arr = array())
{
$ new_arr = array();
if((is_array($ arr))&&(count($ arr)> 0))
{
$ this-> db-> select()肽从(MEMB_BASIC);
$ first_pass = 0;
($ 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, 蓝色);
}
}
}
}
return $ arr;
}
要搜索一个值并获取其关键在阵列中,您可以使用它返回元素的键。
$ found_key = array_search($ needle,$ array);
对于PHP中的多维数组搜索,请查看。
如果您担心优化问题,我认为您必须尝试在数据库上使用查询(具有适当的索引)。
顺便说一下,您是否使用?如果没有尝试,这很有用。
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;
}
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.
这篇关于PHP查找多维数组的数组索引键更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!