drx3157 2016-03-14 00:43
浏览 31
已采纳

将数组插入PHP数组

Working with an array file with following structure. I know there are additional arrays that need to be inserted under each array 'color'.

 $items=array (
   0 => 
  array (
    'color' => 'category_a',
  ),
  1 => 
  array (
    'book' => 'Gone With The Wind',
    'movie' => 'GWTW',
    'id'=> 'A100'
  ),

  2 => 
  array (
    'book' => 'Goldfinger',
    'movie' => 'GF',
    'id'=> 'A103'
  ),
   3 => 
  array (
    'color' => 'category_b',
  ),

  4 => 
  array (
    'book' => 'Across The Great Dvide',
    'movie' => 'ATGD',
    'id'=> 'B102'
  ),
  5 => 
  array (
    'book' => 'Goldfinger',
    'movie' => 'GF',
    'id'=> 'B103'
  ),
  );

Once this array is created, I am using a list to loop thru to verify that each value in the list is placed in each 'color' array as follows

  foreach ($controllist as $key=>$value){
    foreach($items as $item){
      if(in_array($value['book'],$item){
            echo "PRESENT IN ARRAY"."<BR>";
      }else{
             echo "INSERT INTO ARRAY HERE"."<BR>";
      }
     }
     }

For simplicity my controllist looks like Gone With The wind Across The Great Divide Goldfinger Once complete I should end up with the info for Across The Great Divide inserted into 'color'=> 'category a' as the [2] with Goldfinger moving down one. In 'color'=>category_b' the first array should be Gone With The Wind. Any of the 'color' arrays could be missing an array at any position. To sum it up, need to check for the existence of a value from the list, if not present insert into the array. Other than using the foreach loops shown is there an easier way of doing this? If not how can I get the information inserted into the proper position? Thanks

EDIT: I believe the question may not be clear. What I need to do is check for the existence of one array in another. If the value in conrollist is not present in the array, insert an array into the array according the position in the conrollist. The inserted array will have the same structure as the others (I can take care of this part). I am having trouble determining if it exist and if not inserting it. Hope this helps

  • 写回答

1条回答 默认 最新

  • douduxia1551 2016-03-14 02:00
    关注

    You might want to be using a for loop instead so you have a pointer on each iteration in order to determine where you are in the array.

    foreach($items as $item){
        for($i = 0; $i < count($controllist); $i++) {
            if(in_array($controllist[$i]['book'],$item){
                echo "PRESENT IN ARRAY AT POS ".$i."<BR>";
            }else{
                $controllist[$i]['book'] = $yourvar;
                echo "INSERT INTO ARRAY HERE"."<BR>";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮