PHP如何取消设置多维数组的成员? [英] PHP How to Unset Member of Multidimensional Array?

查看:112
本文介绍了PHP如何取消设置多维数组的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个三维阵列,看起来像这样

 阵列(
[第1组] =>排列
    (
        [0] =>排列
            (
                【类别】= GT;组别1
                [名字] =>约翰
                [姓氏] =>约翰斯
                [图片] => /mysite.etc/jj.jpg
            )
        [1] =>排列
            (
                【类别】= GT;组别1
                [名字] =>约翰
                [姓氏] =>詹姆斯
                [图片] => /mysite.etc/jj2.jpg
            )
    )
[第2组] =>排列
    (
        [0] =>排列
            (
                【类别】= GT;第2组
                [名字] =>约翰
                [姓氏] =>杰克逊
                [图片] =>空值
            )
        [1] =>排列
            (
                【类别】= GT;第2组
                [名字] =>约翰
                [姓氏] =>智新
                [图片] => /mysite.etc/jj4.jpg
            )
    )...等等)

我通过数组试图循环并删除任何(即数组的第二级)没有在<$ C $价值谁C> [图] 单元。

我试过

 的foreach($ MYARRAY为$关键=&GT;&安培; $组){
    的foreach($组为&放大器; $人员){
        如果(!file_exists($员工['形象'])){
            未设置($人员);
        }
    }
}

但是,这并不删除无图像的数组项。该回路正确识别无图像员工,如果我有一点code给他们呼应到页面上,这个工程。它只是不从 $ MYARRAY 数组unset它们。

谁能帮我实现这一目标?


解决方案

 的foreach($ MYARRAY为$关键=&GT; $组){
     的foreach($组为$ K =&GT; $人员){
         如果(!file_exists($员工['形象'])){
             未设置($ MYARRAY [$关键] [$ K]);
          }
     }
}

//你应该知道的$组和$工作人员临时变量

I have a three dimensional array that looks like this

Array(
[Group 1] => Array
    (
        [0] => Array
            (
                [category] => Group1
                [firstname] => John
                [lastname] => Johns
                [image] => /mysite.etc/jj.jpg
            )
        [1] => Array
            (
                [category] => Group1
                [firstname] => John
                [lastname] => James
                [image] => /mysite.etc/jj2.jpg
            )
    )
[Group 2] => Array
    (
        [0] => Array
            (
                [category] => Group2
                [firstname] => John
                [lastname] => Jackson
                [image] => NULL
            )
        [1] => Array
            (
                [category] => Group2
                [firstname] => John
                [lastname] => Jimson
                [image] => /mysite.etc/jj4.jpg   
            )
    )...etc)

I'm trying to loop through the array and remove any people (i.e. the second level of the array) who do not have a value in the [image] cell.

I've tried

foreach($MyArray as $Key=>&$group){ 
    foreach($group as &$staff){ 
        if(!file_exists($staff['image'])){
            unset($staff);
        }
    } 
}

but this does not remove the array items with no image. The loop is correctly identifying the staff with no image as if I include a bit of code to echo them onto the page, this works. It's just not unsetting them from the $MyArray array.

Can anyone help me achieve this?

解决方案

foreach($MyArray as $Key=>$group){
     foreach($group as $k=>$staff){ 
         if( !file_exists($staff['image'])) {
             unset($MyArray[$Key][$k]);
          }
     }
}

//you should know the the $group and $staff is temp variables

这篇关于PHP如何取消设置多维数组的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆