2018-04-05 07:57
浏览 97

PHP Array_intersect输入未定义的偏移量

I got an array working like this:

$listaMaterias[x]['id_materia'] = (value with number and letters random)
$listaMaterias[x]['name_materia'] = (string)
$listaEditoriales[x]['id_editorial'] = (value with n. and l. random)
$listaEditoriales[x]['name_editorial'] = (string)

A 'materia' is a book's category. I made a foreach where I get all values from an xml right. Many editorials and materias, where some of them comes repeated.

And then, I make a method with an array_intersect to make remove repeated values, but I get an error :

    $listaEdits_result = array();  // final results
    $listaMats_result = array();

    $listaEds_first_res = $listaEditoriales[0];
    for ($j = 1 ; $j < count($listaEditoriales) ; $j++ ){
        $listaEdits_result = array_intersect($listaEds_first_res, $listaEditoriales[$j]);
        $listaEds_first_res = $listaEdits_result;

    $listaMts_first_res = $listaMaterias[0];
    for ($k = 1 ; $k < count($listaMaterias) ; $k++ ){

             // Line 285, is this one above

        $listaMats_result = array_intersect($listaMts_first_res, $listaMaterias[$j]);
        $listaMts_first_res = $listaMats_result;

And finally, I get this error :

Notice: Undefined offset: 20 in [URL]/menu-librosnormales.php on line 285

Warning: array_intersect(): Argument #2 is not an array in [URL]/menu-librosnormales.php on line 285

Why access offset 20 if before I count this quantity in every array :

count($listaEditoriales) : 20

count($listaMaterias) : 14

图片转代码服务由CSDN问答提供 功能建议


  $ listaMaterias  [x] ['id_materia'] =(带数字和字母的值随机)
 $ listaMaterias [x] ['name_materia'] =(字符串)
 $ listaEditoriales [x] ['id_editorial'] =(带n的值 。和l。随机)
 $ listaEditoriales [x] ['name_editorial'] =(字符串)

'materia'是一本书的类别。 我做了一个foreach,我从xml中获取所有值。 许多社论和材料,其中一些重复出现。

然后,我使用 array_intersect 创建一个方法来删除重复的值,但我得到了一个 错误:

  $ listaEdits_result = array();  //最终结果
 $ listaMats_result = array(); 
 $ listaEds_first_res = $ listaEditoriales [0]; 
 for($ j = 1; $ j&lt; count($ listaEditoriales); $ j ++){\  n $ listaEdits_result = array_intersect($ listaEds_first_res,$ listaEditoriales [$ j]); 
 $ listaEds_first_res = $ listaEdits_result; 
 $ listaMts_first_res = $ listaMaterias [0]; 
 for($ k = 1;  $ k&lt; count($ listaMaterias); $ k ++){
 $ listaMats_result = array_intersect($ listaMts_first_res,$ listaMaterias [$ j]); 
 $  listaMts_first_res = $ listaMats_result; 


通知 :未定义的偏移量:第285行的[URL] /menu-librosnormales.php中的20

警告:array_intersect():参数#2不是[URL] / menu-librosnormales中的数组 第285行.php


计数( $ listaEditoriales): 20

count($ listaMaterias): 14

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutuo6048 2018-04-05 08:03

    Instead of $listaMaterias[$j] do $listaMaterias[$k] in second loop below line:-

    $listaMats_result = array_intersect($listaMts_first_res, $listaMaterias[$j]);

    Note:- If your aim is to remove duplicates from an array then you can use array_unique() easily.

    打赏 评论
  • dousi1906 2018-04-05 08:43

    In the second loop you use $listaMaterias[$j] but the loop is indexed by $k, not by $j.

    The value of $j is count($listaEditoriales) because this was the last value of $j when the first loop ended. Since $listMaterias contains only 14 items, trying to access its 21st item triggers the notice you described.

    If the purpose of each loop is to compute the intersection of the arrays stored in $listaEditoriales (and $listaMaterias) then you can do with a single call to array_intersect() using arguments unpacking (the so-called "splat operator"):

    $listaEds_first_res = array_intersect(...$listaEditoriales);
    $listaMts_first_res = array_intersect(...$listaMaterias);

    The arguments unpacking operator is available since PHP 5.6. If you need to run the code on older PHP versions then you can use call_user_func_array() instead:

    $listaEds_first_res = call_user_func_array('array_intersect', $listaEditoriales);
    $listaMts_first_res = call_user_func_array('array_intersect', $listaMaterias);

    The two lines of code above do the same thing as the entire block of code you posted in the question (faster and without errors).

    打赏 评论

相关推荐 更多相似问题