PHP找到多维数组的数组索引键更新阵列 [英] PHP find the array index key of multi dimensional array to update array
问题描述
我想拿出来与什么可能是非常大的阵列组的工作的一种手段。我在做什么正在与Facebook的图形API。
因此,当一个用户注册,我建立一个服务,我存储自己的Facebook ID在一个表在我的服务。这点是让谁给我服务的签约用户找到自己的谁是在Facebook和也签署了通过我的服务,以找到彼此更容易的朋友。
我想做目前要做的是把对象的 / ME /朋友
数据Facebook的API返回并传递到我已建立一个功能查询我的数据库的ID在工作正常的FB数据中找到。也同时这整个位是怎么回事我刚才的Facebook ID的建筑起来的数组,所以我可以在 in_array
情景中使用它们。正如我的查询只返回Facebook的帐号的发现匹配的
虽然这个数据是通过自身循环创建查询我也更新对象包含每个项目多了一个键/值对是are_friends=&GT就行了;假
到目前为止,这一点这一切工作顺利,比较快的,我有我的查询结果。对此我遍历。
所以我在这里我想避免一个循环内循环的一部分。这就是 in_array()
位的用武之地。我公司自创建存储FB ID的数组现在我可以遍历我的结果,看看是否有一个匹配,并且在事件我想利用我追加'are_friends'=&GT原来的对象;假
并更改该组匹配真,而不是虚假的人。我只是不能没有循环的循环是结果数组中原始数组在想一个好办法。
所以,我希望有人能帮助我想出这里没有解决的二次回路
阵列到这一点上,作为原始长相开始了像
阵列(
[数据](
[0] =>阵列(
are_fb_friends =>假
名称= GT;用户名
ID => 1000
)
[1] =>阵列(
are_fb_friends =>假
名称= GT;用户名
ID => 2000
)
[2] =>阵列(
are_fb_friends =>假
名称= GT;用户名
ID => 3000
)
)
)
根据要求的
这是我目前的code的逻辑,那我试图上面描述..
公共职能fromFB($ ARR =阵列())
{
$ new_arr =阵列();
如果((is_array($ ARR))及及(计数($ ARR)0))
{
$这个 - > DB-GT&;选择() - GT;从(MEMB_BASIC);
$ first_pass = 0;
为($ I = 0; $ I<计数($ ARR); $ I ++)
{
$常用3 [$ i] ['are_fb_friends'] =假;
$ new_arr [] = $改编[$ i] ['身份证'];
如果($ first_pass == 0)
{
$这个 - > DB-化合物其中('facebookID',$改编[$ i] ['身份证']);
}
其他
{
$这个 - > DB-GT&; or_where('facebookID',$改编[$ i] ['身份证']);
}
$ first_pass ++;
}
$这个 - > DB-GT&;限制(计数($ ARR));
$查询= $这个 - > DB-GT&;得到();
如果($查询 - > NUM_ROWS()大于0)
{
$结果= $查询 - >的结果();
的foreach($结果作为$行)
{
如果(in_array($行向> facebookID,$ new_arr))
{
array_keys($改编,蓝);
}
}
}
}
返回$ ARR;
}
要搜索的价值并获得其关键的一个数组,你可以使用的 array_search功能返回元素的关键。
$ found_key = array_search($针,$阵列);
有关在 http://stackoverflow.com/a/8102246/648044 在PHP看看多维数组的搜索
如果你担心优化我认为你必须使用一个数据库的查询(用正确的索引)来试试。
顺便说一句,您使用的是 Facebook查询语言的?如果不给它一个尝试,它是非常有用的。
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 http://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找到多维数组的数组索引键更新阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!