1

我有一个问题,比如我有一个多维数组,如果内部数组包含“编辑”,我想删除父数组,数组是这样的

[fields] => Array
    (
        [0] => Array
            (
                [header] => Array
                    (
                        [fieldName] => edit
                        [displayName] => Edit
                        [width] => 40
                        [group] => 0,1,2,3
                        [cssClass] => dgAssetsCentered
                    )

                [visibility] => Array
                    (
                        [showOnStart] => 1
                        [editable] => 
                    )

                [cell] => Array
                    (
                        [type] => link
                        [params] => Array
                            (
                                [label] => Edit
                            )

                    )

                [sort] => Array
                    (
                        [sortable] => 1
                    )

                [validator] => Array
                    (
                        [name] => 
                        [params] => 
                    )

            )

        [1] => Array
            (
                [header] => Array
                    (
                        [fieldName] => OFFERID
                        [displayName] => Offer Id
                        [group] => 0

我想删除数组 0,因为内部数组包含“编辑”我能够删除 [标题] 数组无法删除 [0] 数组。请建议。

谢谢亚历克斯。

4

2 回答 2

1
foreach($fields as $key => $field)
{
  if(isset($field['header']['fieldName']) && $field['header']['fieldName'] == 'edit')
    unset($field[$key]);
}

UPD:另外,如果你想删除数组,如果任何元素等于“编辑”,试试这个算法:

foreach($fields AS $key => $array)
{
  foreach($array as $innered)
  {
    if(array_search('edit', $innered)) 
    {
      unset($fields[$key]);
      break;
    }
  }
}
于 2012-06-14T20:31:06.960 回答
1

unset(fields);将清除整个数组。

unset(fields[0]);将清除字段数组中的元素 0

于 2012-06-14T20:31:18.373 回答