我想循环记录,检索每个项目的多个属性和值,然后继续将每个项目作为关联数组使用名称/值对推入一个大型多维数组。
例如,在我的循环中,我有2条记录,但是我的关联数组一直在被覆盖。我怎样才能推送关联数组。
我的代码只返回一个键,但它应该返回两个键。
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;
答案 0 :(得分:1)
在开始循环之前将$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
);