我有两个arrays
- 一个single
和其他multi-dimensional
。
$abc='["first","second","third","fourth"]';
$def='[{"post_id":"1","postid":"42","tags":["eminem","baby","jordan"]},{"post_id":"3","postid":"38","tags"
:["abc","def","jordan"]},{"post_id":"4","postid":"40","tags":["eminem","baby","first","second","fourth"
]}]';
$ghi=json_decode($def,true);
$jkl=json_decode($abc,true);
echo '<pre>';
print_r($jkl);
echo '</pre>';
echo '<pre>';
print_r($ghi);
echo '</pre>';
这就是它的样子:
Array
(
[0] => first
[1] => second
[2] => third
[3] => fourth
)
Array
(
[0] => Array
(
[post_id] => 1
[postid] => 42
[tags] => Array
(
[0] => eminem
[1] => baby
[2] => jordan
)
)
[1] => Array
(
[post_id] => 3
[postid] => 38
[tags] => Array
(
[0] => abc
[1] => def
[2] => jordan
)
)
[2] => Array
(
[post_id] => 4
[postid] => 40
[tags] => Array
(
[0] => eminem
[1] => baby
[2] => first
[3] => second
[4] => fourth
)
)
)
我要做的是在多维数组中存在的子数组tags
内搜索单维数组的元素。
这是我的代码:
$users = array_unique($jkl);
$array = [];
$i=0;
foreach($users as $user)
{
if (in_array($user, $ghi[$i]['tags']))
{
$newdata = array (
'postId' => $ghi[$i]['postid'],
'tag' => $user
);
array_push($array, $newdata);
}
$i++;
}
但是我得到了错误提到的问题标题。 可能的原因是什么?谢谢!
答案 0 :(得分:1)
您的两个数组都有不同数量的元素。第一个数组有四个元素,第二个数组有三个元素。这就是你遇到问题的原因。
您应该检查变量是否已设置且必须是数组。使用!empty()和is_array()
foreach($users as $k=>$user){
if(!empty($ghi[$k]['tags']) && is_array($ghi[$k]['tags'])) { // check this condition here
if (in_array($user, $ghi[$k]['tags'])){
$newdata = array (
'postId' => $ghi[$k]['postid'],
'tag' => $user
);
array_push($array, $newdata);
}
}
}
注意: - 我使用数组密钥$k
代替$i
答案 1 :(得分:1)
您必须遍历multi-dimensional
数组并执行您想要的操作: -
<?php
$abc='["first","second","third","fourth"]';
$def='[{"post_id":"1","postid":"42","tags":["eminem","baby","jordan"]},{"post_id":"3","postid":"38","tags"
:["abc","def","jordan"]},{"post_id":"4","postid":"40","tags":["eminem","baby","first","second","fourth"
]}]';
$ghi=json_decode($def,true);
$jkl=json_decode($abc,true);
$users = array_unique($jkl);
$array = []; // empty array
foreach($ghi as $gh) // iterate through multi-dimensional array not single one
{
for($i=0;$i<count($users);$i++){ // a for loop to iterate through single-dimensional completly
if (in_array($users[$i],$gh['tags'])) // if single dimensional array value exist in multi-dimensional tags array
{
$newdata = array (
'postId' => $gh['postid'],
'tag' => $users[$i]
);
array_push($array, $newdata); // push the data
}
}
}
echo "<pre/>";print_r($array); // print newly created array
?>
输出: - https://eval.in/602523
注意: - 尝试将变量名称设置为不会产生歧义。谢谢
答案 2 :(得分:0)
这是因为2个数组的长度不同,请使用isset()
删除错误:
因为$ghi
数组的第三个索引不存在,并且它尝试获取$ghi[3]['tags']
- 这就是错误发生的原因
尝试:
foreach($users as $user)
{
if(isset($ghi[$i]['tags'])) { // just add this condition here
if (in_array($user, $ghi[$i]['tags']))
{
$newdata = array (
'postId' => $ghi[$i]['postid'],
'tag' => $user
);
array_push($array, $newdata);
}
}
$i++;
}
答案 3 :(得分:0)
这取决于您要搜索的内容。 如果你想要检查$ ghi数组中的所有$ users,那么你需要每次都循环遍历。
foreach($users as $user)
{
for ($i = 0; $i < count($ghi); $i++) {
if (in_array($user, $ghi[$i]['tags']))
{
$newdata = array (
'postId' => $ghi[$i]['postid'],
'tag' => $user
);
array_push($array, $newdata);
}
}
}
这将导致$ newdata由以下
组成array(2) { ["postId"]=> string(2) "40" ["tag"]=> string(6) "fourth" }
但是如果你只想要检查每个数组一次。然后你需要检查以确保你没有走出界限。
$ ghi中只有3个元素。 $ users中有4个元素。
所以你可以循环遍历foreach中较小的数组 - 切换$ users - &gt; $ GHI 或者在foreach循环中检查$ ghi在$ i是什么时候有一个有效元素。
d