-1

I am working with a multidimensional array that will contain a variable number of empty objects inside of it. I've written a script to simply unset() the empty ones, and it works fine. However, even without an echo() function, the non-empty values are being printed when my loop is run, and I cannot seem to figure out why.

An example of an array that would use run through my loop:

Array
(
[0] => Group Object
    (
        [_title] => <strong>Abdomen</strong><ul>
        [_issues] => <li><strong>Ascites</strong>: 1</li><li><strong>Blood Clot</strong>: 2</li>
    )

[1] => Group Object
    (
        [_title] => <strong>Air Sac</strong><ul>
        [_issues] => <li><strong>Cloudy</strong>: 1</li>
    )

[2] => Group Object
    (
        [_title] => <strong>Beak Treatment</strong><ul>
        [_issues] => <li><strong>Severe</strong>: 3</li>
    )

[3] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[4] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[5] => Group Object
    (
        [_title] => <strong>Crop</strong><ul>
        [_issues] => <li><strong>Impacted</strong>: 2</li>
    )

[6] => Group Object
    (
        [_title] => <strong>Feathering</strong><ul>
        [_issues] => <li><strong>Soiled</strong>: 1</li>
    )

[7] => Group Object
    (
        [_title] => <strong>Feet</strong><ul>
        [_issues] => <li><strong>Swollen</strong>: 1</li>
    )

[8] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[9] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[10] => Group Object
    (
        [_title] => <strong>Heart</strong><ul>
        [_issues] => <li><strong>Compromised</strong>: 1</li>
    )

[11] => Group Object
    (
        [_title] => <strong>Hock</strong><ul>
        [_issues] => <li><strong>Clear Exudate</strong>: 2</li>
    )

[12] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[13] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[14] => Group Object
    (
        [_title] => 
        [_issues] => 
    )

[15] => Group Object
    (
        [_title] => <strong>Kidney</strong><ul>
        [_issues] => <li><strong>Injured</strong>: 2</li>
    )

)

The foreach() loop that clears out the empty Objects inside of the array:

foreach ($issues as $key => $link) {
            if ($issues[$key] != '') {
        unset($issues[$key]);   
    }
  }

And finally, the output that is printed when the loop is run:

<strong>Abdomen</strong><ul>
    <li><strong>Ascites</strong>: 1</li>
    <li><strong>Blood Clot</strong>: 2</li>
<strong>Air Sac</strong><ul>
    <li><strong>Cloudy</strong>: 1</li>
<strong>Beak Treatment</strong><ul>
    <li><strong>Severe</strong>: 3</li>
<strong>Crop</strong><ul>
    <li><strong>Impacted</strong>: 2</li>
<strong>Feathering</strong><ul>
    <li><strong>Soiled</strong>: 1</li>
<strong>Feet</strong><ul>
    <li><strong>Swollen</strong>: 1</li>
<strong>Heart</strong><ul>
    <li><strong>Compromised</strong>: 1</li>
<strong>Hock</strong><ul>
    <li><strong>Clear Exudate</strong>: 2</li>
<strong>Kidney</strong><ul>
    <li><strong>Injured</strong>: 2</li>

Obviously I just want the empty groups removed and nothing to be printed. Any help is appreciated.

kero
  • 10,647
  • 5
  • 41
  • 51
aaronhuisinga
  • 299
  • 1
  • 5
  • 16
  • 2
    That means that you need to show us more code, with the code you provided, it's impossible that there would be an output ... – HamZa Mar 30 '13 at 23:11

3 Answers3

1

As was suggested here, you can use array_filter

class Foo {

    public $bar;

    public function __construct($arg)
    {
        $this->bar = $arg;
    }

    public function isEmpty()
    {
        if (is_string($this->bar) && !empty($this->bar))
        {
            return false;
        }

        return true;
    }

}

$array = array(
    new Foo(29),
    new Foo(''),
    new Foo('hello'),
    new Foo(null),
    new Foo('world'),
);

function obj_is_empty($obj)
{
    return ( ! $obj->isEmpty() );
}

print "<pre>";
var_dump(array_filter($array, "obj_is_empty"));

Works fine

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
0

I might be wrong, but your array should have items surrounded by quotes:

    [_title] => "<strong>Abdomen</strong><ul>"

otherwise whatever coming is unpredictable.

zipser
  • 250
  • 1
  • 8
0

You are checking against array keys (_title, _issues), not values (Abdomem, etc).

enapupe
  • 15,691
  • 3
  • 29
  • 45