1

I have a problem like i have a multidimensional array and i want to remove a parent array if an inner array contains 'edit' the array is like this

[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

i want to remove array 0 because inner array contains 'edit' i am able to delete [header] array not able to delete [0] array.please suggest.

thanks alex.

5
  • Can you post the code that deletes the header array? Commented Jun 14, 2012 at 20:26
  • 1
    unset($array["fields"][0]); should work? Commented Jun 14, 2012 at 20:28
  • no it's not working, and it's hardcoded one too sometimes edit may be in another position Commented Jun 14, 2012 at 20:30
  • also, you can try array_search() Commented Jun 14, 2012 at 20:37
  • the "Edit" can exist in any-key? or only in that "displayName" ? why you dont just unset the field right away instead of filling it with "Edit" and there search for it.. Commented Jun 14, 2012 at 20:52

2 Answers 2

2

unset(fields); will clear the entire array.

unset(fields[0]); will clear the element 0 inside fields array

Sign up to request clarification or add additional context in comments.

1 Comment

This works very well :) It will unset any variable passed in. Look at usage in my answer.
1
foreach($fields as $key => $field)
{
  if(isset($field['header']['fieldName']) && $field['header']['fieldName'] == 'edit')
    unset($field[$key]);
}

UPD: Also, if you wanna remove array, if any element equal 'edit', try this algorithm:

foreach($fields AS $key => $array)
{
  foreach($array as $innered)
  {
    if(array_search('edit', $innered)) 
    {
      unset($fields[$key]);
      break;
    }
  }
}

1 Comment

The above is for 2 dimensional we should call recursively

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.