php PHP查找多维数组的数组索引键来更新数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15333085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP find the array index key of multi dimensional array to update array
提问by chris
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/friendsdata 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_arrayscenario. As my query only returns facebook id's found matching
我目前正在尝试做的是获取 facebook api 为/me/friends数据返回的对象,并将其传递给一个函数,我已经为我的数据库构建了一个查询,以获取在 FB 数据中找到的 ID,这可以正常工作。此外,虽然这整个过程正在进行中,但我有一个仅包含 facebook id 的数组,因此我可以在某个in_array场景中使用它们。因为我的查询只返回 facebook id's found 匹配
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"=> falseSo 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'=>falseto 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到的原始对象并更改这些对象在那个匹配到“真”而不是假的集合中。我只是想不出一个好方法,而不是在作为结果数组的循环内循环遍历原始数组。
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;
}
采纳答案by Guglie
To search a value and get its key in an array, you can use the array_search functionwhich returns the key of the element.
要在数组中搜索值并获取其键,您可以使用array_search 函数返回元素的键。
$found_key = array_search($needle, $array);
For multidimensional array search in PHP look at https://stackoverflow.com/a/8102246/648044.
对于 PHP 中的多维数组搜索,请查看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.
顺便问一下,你在使用Facebook 查询语言吗?如果不试一试,它是有用的。

