我正在使用以下代码循环遍历多维数组并查找前往某个大学的用户,在本例中为UCF。
$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
$isBA = false;
if (is_array($friend['education'])) {
foreach ($friend['education'] as $school) {
if (isset($school)) {
foreach ($school['school'] as $name) {
if (strpos(strtolower($name['name']), 'University of Central Florida') !== false) {
$friends_BA[] = $friend['name'];
continue 3; // skip to the next friend
}
}
}
}
}
}
d($friends_BA);
多维数组看起来像这样。这是来自Facebook Graph API的朋友列表:
Array
(
[0] => Array
(
[name] => PERSON
[education] =>
)
[1] => Array
(
[name] => PERSON
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[year] => Array
(
[id] => 138383069535219
[name] => 2005
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 20697868961
[name] => Boston University
)
[concentration] => Array
(
[0] => Array
(
[id] => 108654845832522
[name] => Business Administration
)
)
[type] => College
)
[2] => Array
(
[school] => Array
(
[id] => 108289315859633
[name] => University of Miami
)
[year] => Array
(
[id] => 138879996141011
[name] => 2013
)
[type] => Graduate School
)
)
)
[2] => Array
(
[name] => PERSON
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 115444241803885
[name] => Saint Andrews High School
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 112033702149888
[name] => Boca Raton High
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[2] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[type] => High School
)
[3] => Array
(
[school] => Array
(
[id] => 107573562605861
[name] => Duke University
)
[concentration] => Array
(
[0] => Array
(
[id] => 104045469631213
[name] => Political science
)
)
[type] => College
)
)
)
[3] => Array
(
[name] => PERSON
[education] =>
)
[4] => Array
(
[name] => PERSON
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 106039752760627
[name] => Berwick Academy
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[type] => High School
)
[2] => Array
(
[school] => Array
(
[id] => 105690226130720
[name] => Northeastern University
)
[concentration] => Array
(
[0] => Array
(
[id] => 108654845832522
[name] => Business Administration
)
)
[type] => College
[classes] => Array
(
[0] => Array
(
[id] => 189873264368867
[name] => 2011
)
)
)
)
)
我已经运行了一个类似的程序来查找用户在某些确实有效的主题中的主修,但由于某种原因,上面的代码不起作用。这是正在运作的那个:
$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
$isBA = false;
if (is_array($friend['education'])) {
foreach ($friend['education'] as $school) {
if (isset($school['concentration'])) {
foreach ($school['concentration'] as $concentration) {
if (strpos(strtolower($concentration['name']), 'business') !== false) {
$friends_BA[] = $friend['name'];
continue 3; // skip to the next friend
}
}
}
}
}
}
d($friends_BA);
与往常一样,任何帮助都将得到真诚的感谢。节日快乐!
更新
有人想对这个人进行刺杀吗?
$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
$isBA = false;
if (is_array($friend['current_location'])) {
foreach ($friend['current_location'] as $location) {
if (isset($location)) {
foreach ($location['city'] as $city) {
$lowerName = strtolower($city);
if (strpos($lowerName, 'orlando') !== false || strpos($lowerName, 'gainesville') !== false) {
$friends_BA[] = $friend['name'];
continue 3; // skip to the next friend
}
}
}
}
}
}
d($friends_BA);
数组如下所示:
Array
(
[0] => Array
(
[name] => PERSONS NAME
[current_location] => Array
(
[city] => New York
[state] => New York
[country] => United States
[zip] =>
[id] => 108424279189115
[name] => New York, New York
)
)
[1] => Array
(
[name] => PERSONS NAME
[current_location] =>
)
[2] => Array
(
[name] => PERSONS NAME
[current_location] =>
)
[3] => Array
(
[name] => PERSONS NAME
[current_location] =>
)
[4] => Array
(
[name] => PERSONS NAME
[current_location] => Array
(
[city] => San Jose
[state] => California
[country] => United States
[zip] =>
[id] => 111948542155151
[name] => San Jose, California
)
)
[5] => Array
(
[name] => PERSONS NAME
[current_location] => Array
(
[city] => Boston
[state] => Massachusetts
[country] => United States
[zip] =>
[id] => 106003956105810
[name] => Boston, Massachusetts
)
)
一直在玩它一个小时,但似乎无法使它工作。我在第二个foreach声明中得到了无效的论据。
答案 0 :(得分:1)
你的问题是灵敏度。您在字符串上使用strtolower
来检查大学,但是,您发布的大学字符串是大小写混合的。将它更改为此行,它应该工作:
if (strpos(strtolower($name['name']), 'university of central florida') !== false) {
如果您希望案例匹配,请删除strtolower
,重新添加大写字母,它也应与案例匹配。
<强>更新强>
问题的第二部分$name['name']
应该只是$name
。由于您正在直接访问学校阵列,因此它正在遍历该部分。
if (strpos(strtolower($name), 'university of central florida') !== false) {
应该这样做。所以基本上foreach循环遍历学校数组,因此名称作为字符串值而不是数组的一部分。
更新2
$lowerName = strtolower($name);
if (strpos($lowerName, 'university of central florida') !== false
|| strpos($lowerName, 'ucf') !== false) {
答案 1 :(得分:0)
尝试将这两个函数一起遍历整个数组。
它们完全是动态的,将会超越任何复杂性的多维数组。
我正在使用它将所有维度回显为1,您可以使用它来检查key =&gt;值对并检查它是否是您要查找的值。
function workwitharrays($v) {
if (is_array($v)) {
foreach ($v as $key => $value) {
if (is_array($value)) {
workwitharrays($value);
} elseif (!is_array($value)) {
echo "<tr><td>{$key}</td><td>{$value}</td></tr>";
}
}
}
}
function array_get_key_val($firstarray) {
if (is_array($firstarray)) {
foreach ($firstarray as $k => $v) {
if (is_array($v)) {
workwitharrays($v);
} elseif (!is_array($v)) {
echo "<tr><td>{$k}</td><td>{$v}</td></tr>";
}
}
}
}
echo "<table><tbody>";
array_get_key_val($ArrayMultidim);
echo "</tbody></table";
答案 2 :(得分:-1)
你能详细说明你的意思吗?&#34;没有工作&#34;?如果你的测试数据是正确的,那么你的$ friends_BA []数组就不应该添加任何内容,因为这些人都没有进入过佛罗里达中部大学#39;
您收到错误了吗?您可能希望从值中删除撇号。如果if语句尝试执行此操作,圣安德鲁可能会导致问题
If('St. Andrew's' == 'University of Central Florida')
这会导致错误。