Answer the question
In order to leave comments, you need to log in
Is it correct to select the only element of an array that is also an array?
The task is to display additional images for the product. The data of each image is an array and is stored in an array common to all images - FILE_VALUE:
$arResult["DISPLAY_PROPERTIES"]["MORE_PHOTO"]["FILE_VALUE"
] properties):
Array ( [0] => Array ( [ID] => 64 [HEIGHT] => 500 [WIDTH] => 950 [FILE_SIZE] => 144931 [CONTENT_TYPE] => image/jpeg [SRC] => / 1.jpg ) [1] => Array ( [ID] => 65 [HEIGHT] => 500 [WIDTH] => 950 [FILE_SIZE] => 111784 [CONTENT_TYPE] => image/jpeg [SRC] => /2 .jpg ) )
It does a great job of looping:
<? foreach ($arResult["DISPLAY_PROPERTIES"]["MORE_PHOTO"]["FILE_VALUE"] as $PHOTO): ?>
<img src="<?=$PHOTO["SRC"]?>" />
<? endforeach; ?>
<img src="6" />
<img src="5" />
<img src="9" />
<img src="1" />
<img src="/" />
Answer the question
In order to leave comments, you need to log in
But, when there is only one picture, then an array is stored in FILE_VALUE at once
Summarizing all of the above, we get the following code:
$arMoreFoto = $arResult["DISPLAY_PROPERTIES"]["MORE_PHOTO"]["FILE_VALUE"];
if ( !empty($arMoreFoto["SRC"]) ){
$arMoreFoto = array( $arMoreFoto );
}
<? foreach ($arMoreFoto as $PHOTO) {?>
<img src="<?=$PHOTO["SRC"]?>" />
<? } ?>
if(isset($arResult["DISPLAY_PROPERTIES"]["MORE_PHOTO"]["FILE_VALUE"][0])){
...
}
Before foreach, do an if , which will use the count function ( https://secure.php.net/manual/en/function.count.php) to check the number of elements in the array. If more than 1, then your option, which is given in the question, otherwise stupidly refer to $arResult["DISPLAY_PROPERTIES"]["MORE_PHOTO"]["FILE_VALUE"][0].
PS Perhaps there is a better option, wait for other answers)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question