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 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示