Z
Z
zekin3752020-05-29 11:11:46
PHP
zekin375, 2020-05-29 11:11:46

How to filter an associative array?

There is a php code, I get an associative array of $result elements. Please tell me how to filter the array by the string property PROPERTY_SHOW

That is, in the end, only elements with the PROPERTY_SHOW property that have the value Y should get into the $result array

<?
if (CModule::IncludeModule("iblock")):
 
    $iblock_id = 36;
    # show url my elements
    $my_elements = CIBlockElement::GetList (
      Array("ID" => "ASC"),
      Array("IBLOCK_ID" => $iblock_id),
      false,
      false,
      Array('ID', 'NAME',  'PROPERTY_PRICE', 'DETAIL_PAGE_URL')
    );
 
$result = [];
while ($ar_fields = $my_elements->GetNext()) {
      $result[] = $ar_fields;
};
endif;
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Rodichev, 2020-05-29
@zekin375

$result = [];
while ($ar_fields = $my_elements->GetNext()) {
  
  if ($ar_fields['PROPERTY_SHOW_VALUE'] === 'Y'){
    $result[] = $ar_fields;
  }
};

T
tgarl, 2020-05-29
@tgarl

Have you tried adding it to the filter?

$my_elements = CIBlockElement::GetList (
      Array("ID" => "ASC"),
      Array("IBLOCK_ID" => $iblock_id, 'PROPERTY_SHOW'=>'Y'),
      false,
      false,
      Array('ID', 'NAME',  'PROPERTY_PRICE', 'DETAIL_PAGE_URL')
    );

P
profesor08, 2020-05-29
@profesor08

https://www.php.net/manual/en/function.array-filter.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question