0

I have a multidimensional array that is used to pull images and data into the page. The page will also have a sort by system which will display only the array items that have a certain tag (currently tagging yes or no with 0 and 1) I am using an if statement to decide whether the page displays the default (all of the photos) or the sorted array. The default works fine on its own, but im trying to get the sorted to display. When i include the html/php for displaying the images, instead of "echo $mImage['img_caption'];" (Which is a stand in, im just testing if i can even get the statement to display the items. Instead of the full caption string, it displays the first character of every value of every key.) i get errors akin to "unexpected "{" or "<" and the page wont load. Ive basically had to teach myself php over the course of three days, so im sure im missing something easy. Ill include an example of the array, the php for the sort, and the section that works to display the entire array.

Here is my arrays

    $arrImages[] = 
    [
        'img_sm'=>'exampleple.jpg', 
        'img_lg'=>'example2.thumb.jpg',
        'img_caption'=>'multifamily',
        'img_description'=>'example',
        'img_path' => 'img/getest',
        'img_type'=>'getest',
        'MultiFamily'=>1,
    ];
$arrImages[] = 
    [
        'img_sm'=>'example.jpg', 
        'img_lg'=>'example.thumb.jpg',
        'img_caption'=>'notmultifamily',
        'img_description'=>'example786',
        'img_path' => 'img/getest',
        'img_type'=>'getest',
        'MultiFamily'=> 0,
    ];

And here is the if statement

    <?php 
$sorted = 1;
//If the viewer is sorting, check what they are sorting by and then sort by that before pushing the page to change display. 
if ($sorted = 1){//start if sorted statement
$MultiFamilySorted = 1; //replace this with a function that checks all checkboxes to see if they are sorted by that or not. This is simulating that the viewer is sorting to see only multifamily homes.
foreach($arrImages as $sortedImages)://Start sort loop
if ($sortedImages['MultiFamily'] = 1){//start display sorted
foreach($sortedImages as $mImage): //start display loop

echo $mImage['img_caption'];//i am just using this to test if it will display 
    endforeach;
}//end display sorted
endforeach;
}//end if sorted statement
?>

This is the section for my default display

<div class="ImageDisplayTestBox">
    <?php 
    foreach($arrImages as $mImage): //loop through the image array
    ?>
    <div class="masonry-item no-default-style col-sm-3">
                    <a href="<?php echo $mImage['img_path'] . '/' . $mImage['img_lg']; ?>">
                        <span class="thumb-info thumb-info-centered-info thumb-info-no-borders">
    <span class="thumb-info-wrapper">
        <img src="<?php echo $mImage['img_path'] . '/' . $mImage['img_sm']; ?>" class="img-fluid" alt="">
            <span class="thumb-info-title">
                <span class="thumb-info-inner"><?php echo $mImage['img_caption']; ?></span>
                <span class="thumb-info-type"><?php echo $mImage['img_type']; ?></span>
            </span>
            <span class="thumb-info-action">
                <span class="thumb-info-action-icon"><i class="fas fa-plus"></i></span>
            </span>
    </span>
    </span>
    </a>
    </div>

    <?php endforeach; //end loop ?>
</div>

Thanks!

Tristian
  • 31
  • 3

1 Answers1

1

I did not check you code for the syntax error you mentioned, but there is a simpler way of doing this:

<?php 
  if ($sorted == 1){
    $arrImages = array_filter($arrImages, function($image){
        return $image['MultiFamily'] == 0;
    });
  }
?>

You can use this instead of your sorting code.

This executes the sorting function for each array member. If the function returns true it will pass it to the the sorted array. If it is false it will skip it.

The documentation of array_filter is here: https://www.php.net/manual/de/function.array-filter.php

psb
  • 21
  • 2
  • 1
    When a question is misusing the equals operator, please close as Off-topic: typo or flag to close as a duplicate. – mickmackusa Aug 14 '22 at 09:50