donglian8407 2018-04-05 07:57
浏览 102
已采纳

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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类