需要一点帮助来弄清楚如何在循环中附加数组



我有一个使用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));
}

最新更新