U
U
UnderDog322020-04-29 14:19:36
PHP
UnderDog32, 2020-04-29 14:19:36

Creating an object in an array with a condition. What am I doing wrong?

There is a code:

<?
  $arrProgressBuilds = [
  [
  'name' => $arResult[PROPERTIES][per1][NAME],
  ],
  [
  'name' => $arResult[PROPERTIES][per2][NAME],
  ],
  [
  'name' => $arResult[PROPERTIES][per3][NAME],
  ],
  ];
  
  $countArrBuilds = count($arrProgressBuilds);
  $countClassBuilds = [
  '2' => '6',
  '4' => '4',
  '6' => '4',
  '8' => '3'
  ];
?>


There is a task - not to declare an element in an array if a certain variable is empty.

I write code:

<?
  $arrProgressBuilds = [
  if (!empty($arResult[PROPERTIES][per1][VALUE])) {
    [
    'name' => $arResult[PROPERTIES][per1][NAME],
    ],
  }
  if (!empty($arResult[PROPERTIES][per2][VALUE])) {
    [
    'name' => $arResult[PROPERTIES][per2][NAME],
    ],
  }
  if (!empty($arResult[PROPERTIES][per3][VALUE])) {
    [
    'name' => $arResult[PROPERTIES][per3][NAME],
    ],
  }
  
  $countArrBuilds = count($arrProgressBuilds);
  $countClassBuilds = [
  '2' => '6',
  '4' => '4',
  '6' => '4',
  '8' => '3'
  ];
?>

Nothing works, PHP throws an error.

Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-04-29
@UnderDog32

The condition inside the declaration. Change to:

$arrProgressBuilds = [];
if (!empty($arResult['PROPERTIES']['per1']['VALUE'])) {
  $arrProgressBuilds[] = [
  'name' => $arResult['PROPERTIES']['per1']['NAME'],
  ];
}
if (!empty($arResult['PROPERTIES']['per2']['VALUE'])) {
  $arrProgressBuilds[] = [
  'name' => $arResult['PROPERTIES']['per2']['NAME'],
  ];
}
if (!empty($arResult['PROPERTIES']['per3']['VALUE'])) {
  $arrProgressBuilds[] = [
  'name' => $arResult['PROPERTIES']['per3']['NAME'],
  ];
}

Learn to read errors, they say what is wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question