循环查询并将每个项目添加为一个多维数组中的关联数组

Loop over query and add each item as associative array in one multi-dimensional array

我想遍历记录,检索每个项目的多个属性和值,然后使用 name/value 对将每个项目作为关联数组推送到一个大型多维数组中。

例如,在我的循环中我有 2 条记录,但我的关联数组不断被覆盖。我怎样才能推送关联数组。

我的代码只有 return 一把钥匙,虽然它应该 return 两把。

PHP

    // loop over wordpress posts
    while ( have_posts() ) : the_post();
        $propertytype = get_field('PropertyType');
        $propertyname = get_field('PropertyName');
        $propertylocation = get_field('PropertyLocation');
        $propertydescr = get_field('PropertyDescription');
        $propertydphone = get_field('PropertyPhone');
        $propertywebsite = get_field('PropertyWebsite');
        $propertystatus = get_field('PropertyStatus');
        $propertythumb = get_field('PropertyThumbnail');
        $propertylargeimage = get_field('PropertyLargeImage');

        //add each record as an associative array
        $data1 = array(
            'PropertyType' => $propertytype,
            'PropertyName' => $propertyname,
            'PropertyLocation' => $propertylocation,
            'PropertyDescription' => $propertydescr,
            'PropertyPhone' => $propertydphone,
            'PropertyWebsite' => $propertywebsite,
            'PropertyStatus' => $propertystatus,
            'PropertyThumbnail' => $propertythumb,
            'PropertyLargeImage' => $propertylargeimage
          );
    endwhile;

在开始循环之前将 $data1 定义为数组

$data1[] = []; // or array() for backwards compatibility

循环内部

$data1[] = array(
                'PropertyType' => $propertytype,
                'PropertyName' => $propertyname,
                'PropertyLocation' => $propertylocation,
                'PropertyDescription' => $propertydescr,
                'PropertyPhone' => $propertydphone,
                'PropertyWebsite' => $propertywebsite,
                'PropertyStatus' => $propertystatus,
                'PropertyThumbnail' => $propertythumb,
                'PropertyLargeImage' => $propertylargeimage
              );