0

The Array contains some non-empty arrays . i need to fetch respective non-empty array and print the data . eg: array 2 has variable as importTroubles->troubleMessage how can i print that?

Array
(
 [0] => stdClass Object
    (
    )

[1] => stdClass Object
    (
    )

[2] => stdClass Object
    (
        [return] => stdClass Object
            (
                [failureMessage] => 
                [importTroubles] => stdClass Object
                    (
                        [kind] => ParseError
                        [rowNumber] => 1
                        [troubleMessage] => Field "number1" has invalid value: "+16046799329". Invalid phone number //need to print this..
                    )

                [keyFields] => number1
                [uploadDuplicatesCount] => 0
                [uploadErrorsCount] => 1
                [warningsCount] => stdClass Object
                    (
                    )

                [callNowQueued] => 0
                [crmRecordsInserted] => 0
                [crmRecordsUpdated] => 2
                [listName] => new camp from CRM1-TargetList-CRM
                [listRecordsDeleted] => 0
                [listRecordsInserted] => 2
            )

    )

[3] => stdClass Object
    (
    )

[4] => stdClass Object
    (
    )

)

im trying with this method :

foreach($result as $object) {
foreach ($object as $items) {

    if($items !== '')
    {
        foreach ($items as $item) {
            echo "ERROR".$item->troubleMessage;
        }
    }

}
}

Thanks for your efforts

4
  • What is $result? Commented Sep 27, 2017 at 14:10
  • change your if condition to if( !empty($items) ) Commented Sep 27, 2017 at 14:16
  • actually fetching this result from ajax so i get no error Commented Sep 27, 2017 at 14:23
  • enabled the error but i get this error "Trying to get property of non-object " Commented Sep 27, 2017 at 14:33

4 Answers 4

1

Make use of php function empty()

Change your if condition as in below code :

foreach($result as $object) {
 foreach ($object as $items) {
    if( !empty($items) )
    {
        foreach ($items as $item) {
          if( isset($item->troubleMessage) )
          {
            echo "ERROR".$item->troubleMessage;
          }
        }
    }
 }
}

Now it will echo only if $items has values.

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

4 Comments

Trying to get property of non-object in(echo "ERROR".$item->troubleMessage;)
You have to use one more if condition for $items. Edited answer accordingly. check now
This is a notice not error, anyway resolved it. use code now
Thanks for your efforts bro!!
0

You don't have to iterate each object if you're only looking for a single specific item nested within it. You can just refer to that item directly.

foreach ($your_array as $object) {
    if (isset($object->return->importTroubles->troubleMessage)) {
        echo $object->return->importTroubles->troubleMessage;
    }
}

If you check if that specific nested object variable is set, it will ignore any empty objects.

1 Comment

Thanks Bro Its working but small warnings which i can manage Thanks :)
0

change your if($items !== '') to if(!empty($items)) or if($items) or if($items[0]) hope it helps

1 Comment

Trying to get property of non-object (echo "ERROR".$item->troubleMessage;)
0

You could use Collection

use Illuminate\Support\Collection;

$collection = new Collection($result);

$items = $collection->filter(function($object) {

    return isset($object->return->importTroubles->troubleMessage);

})->map(function($object) {

    return $object->return->importTroubles->troubleMessage;

});

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.