4

I have an array

$array = array (
  'pubMessages' => 
  array (
    0 => 
    array (
      'msg' => 'Not bad',
      'type' => 'warning',
    ),
    1 => 
    array (
      'msg' => 'Bad',
      'type' => 'error',
    ),
  ),
);

To remove the sub array having 'type' => 'error', I use bellow code

$key = array_search('error', $array);
unset($array["pubMessages"][$key]);

The key name of the array pubMessages is changed every time, please tell me how to get this key name dynamically? The count of arrays in pubMessages is also variable.

3 Answers 3

5

Get dynamic key name using array_keys() and then loop through inner array and check if key type is equal to error remove it.

$dynamicKey = array_keys($array)[0];
foreach($array[$dynamicKey] as $item){
    if ($item['type'] == 'error')
        unset($array[$dynamicKey][$key]);
}

Check result in demo

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

1 Comment

Thank you Mohammad, the name of the key pubMessages is changed dynamically.
2

I would use an array_filter() on this kind of search, like so:

$array = array(
            'pubMessages' => array (
                0 => array (
                    'msg' => 'Not bad',
                    'type' => 'warning'
                ),
                1 => array (
                    'msg' => 'Bad',
                    'type' => 'error'
                )
            )
        );

// array_search() will return false. It is not how 
// array_search() works on a multi-dimensional array
// $key = array_search('error',$array);

function findError($a) {
    return ($a['type'] != 'error');
}

// deal with "unknown" first index / key name issue
$idx = array_keys($array)[0];
$array[$idx] = array_filter($array[$idx],"findError");

var_dump($array);
exit;

And the output will be:

array(1) {
    ["pubMessages"]=> array(1) {
        [0]=> array(2) {
             ["msg"]=> string(7) "Not bad"
             ["type"]=> string(7) "warning"
        }
    }
}

Edit: Added a fix for the unknown key / index issue

3 Comments

IMHO this is probably the way to go.
This solution doesn't work because The key name of the array pubMessages is changed every time
@Mohammad : fixed the issue, which I would have thought the OP could have solved them self.
1

You can just use foreach() over the whole array to pick up any elements.

The main problem is that your just saying

$key = array_search('error', $array);

This won't look through all sub-arrays etc. to find the value, just the top level. As the error is in the type column of the array, you would have to extract that column and search in this array instead. You can use array_column() to pick out the data.

foreach ( $array as &$subarray ) {
    $key = array_search('error', array_column($subarray, "type"));
    unset($subarray[$key]);
}

Comments

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.