dongpu1315 2018-05-22 07:27
浏览 34
已采纳

从php中的多维数组中删除相关元素

I have an array of image data like this:

[other-image] => Array
        (
            [img] => Array
                (
                    [0] => 1526973657.jpg
                    [1] => 1526973661.jpg
                    [2] => 1526973665.jpg
                )

            [path] => Array
                (
                    [0] => ../post-upload/1/
                    [1] => ../post-upload/1/
                    [2] => ../post-upload/1/
                )

            [type] => Array
                (
                    [0] => 1
                    [1] => 1
                    [2] => 1
                )

            [thumb] => Array
                (
                    [0] => thumb_1526973661.jpg
                    [1] => thumb_1526973665.jpg
                    [2] => thumb_1526973668.jpg
                )

        )

Now I want to delete an image and it's all related data from sub arrays. (path, type, thumb data)

This is how I tried it in php:

  $delkey = '1526973657.jpg';
  if(in_array($delkey, $_SESSION['other-image']['img'])){
      $imgkey = array_search($delkey, $_SESSION['other-image']['img']);
      if($imgkey) unset($_SESSION['other-image']['img'][$imgkey]);
  }

But problem is I can't delete related data from other arrays. Can anybody tell me how to do this? Thank you.

  • 写回答

3条回答 默认 最新

  • douyu0725 2018-05-22 07:46
    关注

    You should use !==false after array_search() because it may return first index i.e. 0 in some cases, so your condition will not executed. And regarding delete related data from other arrays, you have to unset other data related to that key.

    if($imgkey!==false){
            unset($_SESSION['other-image']['img'][$imgkey]);
            unset($_SESSION['other-image']['path'][$imgkey]);
            unset($_SESSION['other-image']['type'][$imgkey]);
            unset($_SESSION['other-image']['thumb'][$imgkey]);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dsa5233 2018-05-22 07:35
    关注

    Is the related data has same key with img? If they are same, I think you only need to add some codes to delete other data like the way was used to delete img.

    if($imgkey) unset($_SESSION['other-image']['path'][$imgkey]);
    if($imgkey) unset($_SESSION['other-image']['type'][$imgkey]);
    if($imgkey) unset($_SESSION['other-image']['thumb'][$imgkey]);
    
    评论
  • douzhicai7148 2018-05-22 07:36
    关注

    If the keys in img sub-array are related with the same key(index) in sub-arrays(path, type and thumb, you can also unset those keys. e.g.

    $delkey = '1526973657.jpg';
    if(in_array($delkey, $_SESSION['other-image']['img'])){
        $imgkey = array_search($delkey, $_SESSION['other-image']['img']);
        if($imgkey){
            unset($_SESSION['other-image']['img'][$imgkey]);
            unset($_SESSION['other-image']['path'][$imgkey]);
            unset($_SESSION['other-image']['type'][$imgkey]);
            unset($_SESSION['other-image']['thumb'][$imgkey]);
        }
    }
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 字符串的比较老是报错
  • ¥15 很简单的c#代码补全
  • ¥15 复杂表达式求值程序里的函数优先级问题
  • ¥15 求密码学的客成社记ji着用
  • ¥35 POI导入树状结构excle
  • ¥15 初学者c语言题目解答
  • ¥15 div editable中的光标问题
  • ¥15 mysql报错1415Not allowed to return a result set from a trigger 不知如何修改
  • ¥60 Python输出Excel数据整理,算法较为复杂
  • ¥15 回答几个问题 关于数据库