@
@
@distorter2015-09-03 10:49:31
PHP
@distorter, 2015-09-03 10:49:31

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; ?>

But, when there is only one picture, then FILE_VALUE immediately stores an array with a single element:
Array ( [ID] => 64 [HEIGHT] => 500 [WIDTH] => 950 [FILE_SIZE] => 144931 [CONTENT_TYPE] => image/ jpeg [SRC] => /1.jpg )
And the above loop gives an unexpected result - it displays the tag as many times as there are elements in the array, and returns the first character of the property value in the src attribute:
<img src="6" />
<img src="5" />
<img src="9" />
<img src="1" />
<img src="/" />

QUESTION:
How to correctly describe the exception for the case when there is only one picture?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Melkij, 2015-09-03
_

But, when there is only one picture, then an array is stored in FILE_VALUE at once

The normal solution is to correct this filling logic.
If there must be a collection, then there must be a collection. And do not care, out of ten, one element or completely empty.
I want to remind you that even PHP 5.4 lives for the last 10 days. Update PHP.
I still hope that you are not even trying to develop something with error output turned off. And starting from 5.4.0, accessing a string as an array by string key provokes E_WARNING. But the first byte (by offset zero, i.e.) is still output.

E
Express777, 2015-09-07
@Express777

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"]?>"   />
<? } ?>

The upside of this is that you don't even have to change your foreach. A collection will always come to it.
Of course your foreach is small, and in principle one could duplicate it in an if-else. But I do not welcome any duplication. Moreover, there are often foreach with complex layout and structure.
If you are using an IDE, I advise you to abandon endforeach and any alternative end* syntax and use curly braces.
if(isset($arResult["DISPLAY_PROPERTIES"]["MORE_PHOTO"]["FILE_VALUE"][0])){
...
}

hmm.. this code will always return true if the array is not empty. That is, they cannot check the nesting of an array.

N
Nikita Kamenev, 2015-09-03
@NickStone

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 question

Ask a Question

731 491 924 answers to any question