簡體   English   中英

需要一點幫助弄清楚如何在循環內添加數組

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

我具有使用RETS從服務器下載照片的功能。 我想獲取照片的路徑,並將它們存儲在JSON文件中以在輪播上顯示。 數組函數位於foreach循環內,它將初始化,但一次只能存儲一個對象。 每次循環遍歷對象時,它都會破壞前一個對象並僅存儲新對象。

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));
                }
            }
        }
    }
}

正如我上面所說,實際結果是,每次循環在一個對象上進行迭代時,它都會破壞前一個並存儲新的。 理想的結果是,它將僅在每次新迭代后追加數組。

謝謝。


編輯 :更新的代碼與似乎正在工作的解決方案:

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));
                }
            }
        }
    }
}

編輯2

我在第一次更新的代碼上遇到了問題。 JSON文件一直為鍵分配數字值,而不是我需要的鍵“ id”和“ images”。 使用array_push()進行了更多修改,最終得到了所需的結果:

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));
                }
            }
        }
    }
}

輸出:

{
    "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"
}

最后編輯

我最后一次運行是在JSON文件中復制密鑰。 顯然不理想。 我發現這是因為我在array_push函數中兩次調用了$ dataImage。 當我取出多余的參數時,我不斷收到來自PHP的警告,稱它需要兩個參數。 我的解決方法是制作一個虛擬數組並將其粘貼在array_push函數中。

我懷疑這不是很好的代碼。 但是鑒於我現在對此有足夠的了解,這是我得到的解決方法,並且我現在正在按預期獲得結果。

碼:

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));
                } 

            }
        }
    }
}

該線程顯然已被鎖定。 即使給出解釋,我也不清楚其被鎖定的原因,但是我要這樣說:如果有人出現在該線程上,我不建議您使用此解決方案。 請按照評論中的一些建議進行調查。 鑒於我正在使用的程序來檢索和處理這些數據,這就是我的感覺。

如果您正在使用PHRETS並嘗試在此處執行類似的操作,請嘗試一下,如果您有更優雅的方法,我會全力以赴。

謝謝。

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

這會將這些分配的結果放入$ dataImage數組中。 $ dataImage ['id']和$ dataImage ['image']仍將包含最后一個圖像ID和源。

您可能想嘗試類似的方法:

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

然后,遍歷$ dataImage:

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

編輯:這是一個使用關聯數組數組的示例:

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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM