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.

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

我有一个像这样的图像数据数组:</ p>

   [other-image] =&gt; 数组
(
 [img] =&gt;数组
(
 [0] =&gt; 1526973657.jpg 
 [1] =&gt; 1526973661.jpg 
 [2] =&gt; 1526973665.jpg \  n)
 
 [path] =&gt;数组
(
 [0] =&gt; ../post-upload/1/
 [1] =&gt; ../post-upload/1/  
 [2] =&gt; ../post-upload/1/
)
 
 [type] =&gt;数组
(
 [0] =&gt; 1 
 [1] =&gt;  ; 1 
 [2] =&gt; 1 
)
 
 [thumb] =&gt;数组
(
 [0] =&gt; thumb_1526973661.jpg 
 [1] =&gt; thumb_1526973665.jpg  
 [2] =&gt; thumb_1526973668.jpg 
)
 
)
 </ code> </ pre> 
 
 

现在我要删除一个图像,它是来自sub的所有相关数据 阵列。 (路径,类型,拇指数据)</ p>

这是我在 php </ code>中尝试的方法:</ p>

  $  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]); 
} 
 </ code> </  pre> 
 
 

但问题是我无法从其他数组中删除相关数据。 任何人都可以告诉我该怎么做? 谢谢。 </ p> </ div>

3条回答 默认 最新

相关推荐 更多相似问题