rvor
rvor

Reputation: 79

Need a little help figuring out how to append an array inside of a loop

I have a function that downloads photos from a server, using RETS. I want to take the path of the photos and store them in a JSON file for display on a carousel. The array function is inside a foreach loop, and it'll initialize but it'll only store one object at a time. Every time the loop goes through the objects, it destroys the previous one and just stores the new one.

function downloadPhotos($results, $rets) {
    if (is_null($results) == false) {
        $all_IDs = $results->lists('Matrix_Unique_ID');
        foreach ($all_IDs as $Id) {
            if(!file_exists("/var/www/html/mls-search/search-ui/public/img/")){
                mkdir("/var/www/html/mls-search/search-ui/public/img/");
        }
            $photos = $rets->GetObject('Property', 'XLargePhoto', $Id, '*', 0);
            foreach ($photos as $photo) {
                if ($photo->isError() == false ) {
                    file_put_contents("/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg", $photo->getContent());
                    $dataImage = array();
                    $dataImage['id'] = $photo->getContentId();
                    $dataImage['image'] = "/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg";
                    file_put_contents("/var/www/html/mls-search/search-ui/public/propimg.json", json_encode($dataImage));
                }
            }
        }
    }
}

The actual results, as I said above, is that it destroys the previous one and just stores the new one every time the loop iterates over an object. The desired result is that it would just append the array with each new iteration.

Thank you.


EDIT: Updated code with a solution that seems to be working right now:

function downloadPhotos($results, $rets) {
    $dataImage = array();
    if (is_null($results) == false) {
        $all_IDs = $results->lists('Matrix_Unique_ID');
        $dataImage = array();
        foreach ($all_IDs as $Id) {
            if(!file_exists("/var/www/html/mls-search/search-ui/public/img/")){
                mkdir("/var/www/html/mls-search/search-ui/public/img/");
        }
            $photos = $rets->GetObject('Property', 'XLargePhoto', $Id, '*', 0);
            foreach ($photos as $photo) {
                if ($photo->isError() == false ) {
                    file_put_contents("/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg", $photo->getContent());
                    array_push($dataImage, $dataImage['id'] = $photo->getContentId(), $dataImage['image'] = "/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg");
                    file_put_contents("/var/www/html/mls-search/search-ui/public/propimg.json", json_encode($dataImage));
                }
            }
        }
    }
}

EDIT 2

I was having issues with that first updated code. The JSON file kept assigning numeric values to the keys, instead of the "id" and "images" keys I needed it to. Did a little bit more finagling with array_push() and I finally got the result I needed:

function downloadPhotos($results, $rets) {
    $dataImage = array();
    if (is_null($results) == false) {
        $all_IDs = $results->lists('Matrix_Unique_ID');
        foreach ($all_IDs as $Id) {
            if(!file_exists("/var/www/html/mls-search/search-ui/public/img/")){
                mkdir("/var/www/html/mls-search/search-ui/public/img/");
            }
            $photos = $rets->GetObject('Property', 'XLargePhoto', $Id, '*', 0);
            foreach ($photos as $photo) {
                if ($photo->isError() == false ) {
                    file_put_contents("/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg", $photo->getContent());
                    array_push($dataImage, $dataImage[] = array("id" => $photo->getContentId(), "images"=>"/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg"));
                    file_put_contents("/var/www/html/mls-search/search-ui/public/propimg.json", json_encode($dataImage));
                }
            }
        }
    }
}

Output:

{
    "id": "22128021",
    "images": "\/var\/www\/html\/mls-search\/search ui\/public\/img\/22128021-0.jpg"
},

{
    "id": "22128021",
    "images": "\/var\/www\/html\/mls-search\/search ui\/public\/img\/22128021-0.jpg"
},

{
    "id": "22128021",
    "images": "\/var\/www\/html\/mls-search\/search ui\/public\/img\/22128021-1.jpg"
}

FINAL EDIT

The last run that I did was duplicating keys in the JSON file. Obviously not ideal. I found that it was because I called $dataImage twice in the array_push function. When I took that extra parameter out, I kept getting thrown warnings from PHP saying that it expected two parameters. My work around was to make a dummy array and stick it in the array_push function.

Admittedly, this is not good code, I suspect. But given the amount of knowledge I have about this right now, it was the workaround I got and I'm getting the results as expected now.

Code:

function downloadPhotos($results, $rets) {
    $dummyImage = array();
    if (is_null($results) == false) {
        $all_IDs = $results->lists('Matrix_Unique_ID');
        foreach ($all_IDs as $Id) {
            if(!file_exists("/var/www/html/mls-search/search-ui/public/img/")){
                mkdir("/var/www/html/mls-search/search-ui/public/img/");
            }
             $photos = $rets->GetObject('Property', 'XLargePhoto', $Id, '*', 0);
            foreach ($photos as $photo) {
                if ($photo->isError() == false ) {
                    file_put_contents("/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg", $photo->getContent());
                    array_push($dummyImage, $dataImage[] = array("id" => $photo->getContentId(), "images"=>"/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg"));
                    file_put_contents("/var/www/html/mls-search/search-ui/public/propimg.json", json_encode($dataImage));
                } 

            }
        }
    }
}

This thread has apparently been locked. The reasons for it being locked are unclear to me, even given the explanation, but I would say this: if anyone comes up on this thread, I don't recommend this solution. Follow some of the suggestions in the comments instead and investigate those. This was what I felt like I had to do given the program I was working with to retrieve and process this data.

If you're using PHRETS and are trying to do something similar here, go ahead and give it a try and if you have a more elegant approach, I'm all down for it.

Thank you.

Upvotes: 1

Views: 85

Answers (1)

slim
slim

Reputation: 2583

array_push($dataImage, $dataImage['id'] = $photo->getContentId(), $dataImage['image'] = "/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg");

That places the results of those assignments into the $dataImage array. $dataImage['id'] and $dataImage['image'] will still contain the last image id and source.

You may want to try something like:

$dataImage[$photo->getContentId()] = "/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg";

Then, iterate over $dataImage:

foreach ($dataImage as $key => $value) {
    echo $key + ": " + $value;
}

EDIT: Here's an example using an array of associative arrays:

function downloadPhotos($results, $rets) {
    $allDataImages = array();
    if (is_null($results) == false) {
        $all_IDs = $results->lists('Matrix_Unique_ID');
        foreach ($all_IDs as $Id) {
            if(!file_exists("/var/www/html/mls-search/search-ui/public/img/")){
                mkdir("/var/www/html/mls-search/search-ui/public/img/");
        }
            $photos = $rets->GetObject('Property', 'XLargePhoto', $Id, '*', 0);
            foreach ($photos as $photo) {
                if ($photo->isError() == false ) {
                    file_put_contents("/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg", $photo->getContent());
                    $dataImage = array();
                    $dataImage['id'] = $photo->getContentId();
                    $dataImage['image'] = "/var/www/html/mls-search/search-ui/public/img/{$photo->getContentId()}-{$photo->getObjectId()}.jpg";
                    $allDataImages[] = $dataImage;
                }
            }
        }
    }
    file_put_contents("/var/www/html/mls-search/search-ui/public/propimg.json", json_encode($allDataImages));
}

Upvotes: 0

Related Questions