[英]How to fetch non-empty Array from an Array
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?
例如:数组2的变量为importTroubles-> troubleMessage我该如何打印?
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 感谢您的努力
Make use of php function empty() 利用php函数empty()
Change your if condition as in below code : 更改您的if条件,如以下代码所示:
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. 现在,仅当
$items
具有值时,它才会回显。
将您的if($items !== '')
更改为if(!empty($items))
或if($items)
或if($items[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. 如果检查是否设置了特定的嵌套对象变量,它将忽略任何空对象。
You could use Collection
您可以使用
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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.