A
A
Anton2017-08-26 15:28:10
PHP
Anton, 2017-08-26 15:28:10

How not to display empty values ​​in an array (Bitrix)?

Bitrix code

<?$arSelect = Array("ID","PROPERTY_TIME");$arFilter = Array("IBLOCK_ID"=>34);$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>550), $arSelect);while($ob = $res->GetNextElement()){  $arFields = $ob->GetFields(); 

echo ($arFields[PROPERTY_TIME_VALUE]).", "; 
}?>

Outputs: 1, 1, 6, 7, 7, 7, 7, 8676, , , , , , , , , , , , , , , 5, , 111, 111
How to remove empty values?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly Kirsanov, 2017-08-26
@bitrixworkshop

You need to check for empty value in $arFields['PROPERTY_TIME_VALUE']. If empty, then continue .
A better option is to add a check for a non-empty value to $arFilter. Alternatively '!TIME' => false

M
Maxim Timofeev, 2017-08-26
@webinar

echo (!empty($arFields[PROPERTY_TIME_VALUE]))?$arFields[PROPERTY_TIME_VALUE].", ":"";

N
Nikolay S., 2017-08-31
@Nikolays93

For starters: writing everything in one line is not the best manner.
2: PROPERTY_TIME_VALUE it is desirable to wrap "PROPERTY_TIME_VALUE" in quotes, of course, if it is not a constant
3: Add a check for the value

$arSelect = Array("ID","PROPERTY_TIME");
$arFilter = Array("IBLOCK_ID"=>34);
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>550), $arSelect);

while($ob = $res->GetNextElement()){
    $arFields = $ob->GetFields();

    if( !empty($arFields["PROPERTY_TIME_VALUE"]) )
        echo ($arFields["PROPERTY_TIME_VALUE"]).", ";

    // или:
    // if( empty($arFields["PROPERTY_TIME_VALUE"]) ) continue;
    // echo ($arFields["PROPERTY_TIME_VALUE"]).", ";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question