Home > Software design >  Remove the whole array elements, where array element key count < 2 in PHP
Remove the whole array elements, where array element key count < 2 in PHP

Time:10-09

Below is my code that output this array:

 // update users
$where_in = array('1102','');
$admin_data = $this->db->select('id,email,domain')->where_in('id',$where_in)->get('users')->result();

echo "<pre>";print_r($admin_data);

current output array

1102,
Array
(
    [0] => stdClass Object
        (
            [id] => 1
        )

)
1111, 
Array
(
    [0] => stdClass Object
        (
            [id] => 1132
        )

    [1] => stdClass Object
        (
            [id] => 1133
        )

)

I am trying to accomplish by doing this, but not getting expected result.

foreach ($admin_data as $key) {

    if (count($admin_data) < 2) {
        unset($admin_data);
    }
}

Expected result: I want to remove whole array element, where array key less than 2. I wish to get only array with more than 1 key count like below:

1111, 
    Array
    (
        [0] => stdClass Object
            (
                [id] => 1132
            )
    
        [1] => stdClass Object
            (
                [id] => 1133
            )
    
    )

CodePudding user response:

I'm not sure if I understood, but if you have an array of arrays and want to remove the inner arrays with less than 2 elements, you could do:

<?php

$original = [
    ["a"],
    ["b", "c"],
];

$filtered = array_filter(
    $original,
    function ($innerArray) {
        return count($innerArray) >= 2;
    }
);

print_r($filtered);

The result is

Array
(
    [1] => Array
        (
            [0] => b
            [1] => c
        )

)

PHP Fiddle

CodePudding user response:

Check again your code, it contains some logical problems:

foreach ($admin_data as $key) {
    if (count($admin_data) < 2) {
        unset($admin_data);
    }
}

If $admin_data has one item, the if (count($admin_data) < 2) test is true and
unset($admin_data); is executed. That means (simplifying) that the all the contents of $admin_data are deleted.
If $admin_data has two or more items, the if (count($admin_data) < 2) test is never true. And it is so for each iteration.
Both cases are not logical, because you do not need the foreach loop for this test to work. Besides, you might not want to delete $admin_data contents.

What you need to do, is to iterate through $admin_data items, check the item to see if it is an array and if that array has one item, remove the item from the $admin_data array. Or you could create another array only with the valid items.

Here is a possible example of both:

/* unset the item if it is an array with one item */
foreach ($admin_data as $key => $value) {
    if (is_array($value) && count($value) === 1) {
        unset($admin_data[$key]);
    }
}

/* create a new array with the valid items */
$valid_admin_data = [];
foreach ($admin_data as $key => $value) {
    if (is_array($value) && count($value) > 1) {
        $valid_admin_data[$key] = $value;
    }
}

I would also suggest you to read again the foreach documentation. Also, be consistents with names of variables, since it helps avoid misunderstandings: for instance, foreach ($admin_data as $key) could be better named to foreach ($admin_data as $value), since with one variable, you extract the value of current item.

CodePudding user response:

I believe you are trying to unset elements within your "$admin_data" array, however you call unset on the array itself. If you pass the index of the array element into your loop, with the element itself, then you can check if the element has less than two inner elements, and unset that element accordingly.

$elementsToRemove = [];
foreach($admin_data as $index => $key) {
    if (count($key) < 2) {
        $elementsToRemove[] = $index;
    }
}

$newArray = array_diff_key($admin_data, array_flip($elementsToRemove));
  • Related