I have a array dataImages which holds the arrays of image informations and looks like this
Array (
[0] => Array (
[id] => 104
[name] => sample-large-Test-1-a52d268be9ad9c.png
[user] => 31
[main_image] => 1
)
[1] => Array (
[id] => 105
[name] => sample-large-Test-1-a52d268bee6ba5.jpg
[user] => 31
[main_image] => 0
)
[2] => Array (
[id] => 106
[name] => sample-large-Test-1-a52d268bf4c457.jpg
[user] => 31
[main_image] => 0
)
)
How can i check if in array dataImages
is an image with main_image === 1
and how can i show data for that image?
You can do this in loop also:
foreach ($dataImages as $image){
if ($image['main_image']) print_r($image)
}
There might be a better depending on what you want to do exactly (e.g. just loop), but one option is array_filter
in which case you can do
function hasMain($var){
return $var["main_image"];
}
print_r(array_filter($theArray, "hasMain"));
function getMainImage($images) {
if(!is_array($images))
return null;
foreach($images as $image) {
if(isset($image['main_image']) && $image['main_image'] == 1)
return $image;
}
return null;
}
$mainImage = getMainImage($myImages);
Something like this should do the trick.
You can check if a variable contains an array by using is_array. http://nl1.php.net/manual/en/function.is-array.php
$main_image = FALSE;
$img = '';
foreach($images as $image) {
if ( $image['main_image'] == 1 ){
$main_image = TRUE;
$img = $image['name'];
break;
}
}