如何在foreach循环中制作多维数组

时间:2019-01-29 14:48:07

标签: php arrays

我正在尝试在foreach循环中创建多维数组。

首先让我放置我的代码:

$jsonstring = [];


foreach($results as $result){

    if(isset($simplicate_fields['data'][0]['custom_fields'][17]['value'])){
            $dexcode_value          =   $simplicate_fields['data'][0]['custom_fields'][17]['value'];
    }else{
          $dexcode_value          = '';
    }


    if(isset($simplicate_fields['data'][0]['visiting_address']['country'])){
            $simplicate_land        =   $simplicate_fields['data'][0]['visiting_address']['country'];
    }else{
         $simplicate_land        = '';
    }


    $body = array(

        'DEXCODE' => $dexcode_value,

        'LAND' => $simplicate_land,
    );


    $jsonstring = array_merge($jsonstring , $body);

}

print_r($jsonstring);

现在让我解释一下。在循环之前,我创建了一个名为jsonstring的空数组。 在循环内部,我创建了另一个名为body的数组。主体数组包含im循环通过的数组中的数据。在循环结束时,我将主体与json字符串合并。

问题是,每当我在循环外打印jsonstring时,它只会给我1个循环实例。结果如下:

(
    [DEXCODE] => 
    [LAND] => Country_The Netherlands
    [CP GENDER] => heer
    [OROrganisation] => private
    [STRAATNAAM] => private
    [ORCITY] => private
    [OREMAIL] => private
    [ORWEBSITE] => private
    [CP_PHONE] => private
    [first_name] => private
    [last name] => private
    [FUNCTIE] => private
    [CPMobile] => private
    [CPEmail1] => private
    [ORPostcode] => private
    [ORTelephone] => private
    [ACTIEFINDUITSLAND] => Ja
    [GESPREKSVELDSIMPLICATE] => private
    [organisatie grootte] => Onbekend
    [B2B of B2C] => Onbekend
    [doel groep] => Onbekend
    [situatie in duitsland] => Onbekend
    [URL] => private
    [contactpersonen] => private
    [simplicate_id] => private
)

只是一次,但是我希望将循环的每个实例存储在数组中。

我的期望是:

 1(
        [DEXCODE] => 
        [LAND] => Country_The Netherlands
        [CP GENDER] => heer
        [OROrganisation] => private
        [STRAATNAAM] => private
        [ORCITY] => private
        [OREMAIL] => private
        [ORWEBSITE] => private
        [CP_PHONE] => private
        [first_name] => private
        [last name] => private
        [FUNCTIE] => private
        [CPMobile] => private
        [CPEmail1] => private
        [ORPostcode] => private
        [ORTelephone] => private
        [ACTIEFINDUITSLAND] => Ja
        [GESPREKSVELDSIMPLICATE] => private
        [organisatie grootte] => Onbekend
        [B2B of B2C] => Onbekend
        [doel groep] => Onbekend
        [situatie in duitsland] => Onbekend
        [URL] => private
        [contactpersonen] => private
        [simplicate_id] => private
    )
    2(
        [DEXCODE] => 
        [LAND] => Country_The Netherlands
        [CP GENDER] => heer
        [OROrganisation] => private
        [STRAATNAAM] => private
        [ORCITY] => private
        [OREMAIL] => private
        [ORWEBSITE] => private
        [CP_PHONE] => private
        [first_name] => private
        [last name] => private
        [FUNCTIE] => private
        [CPMobile] => private
        [CPEmail1] => private
        [ORPostcode] => private
        [ORTelephone] => private
        [ACTIEFINDUITSLAND] => Ja
        [GESPREKSVELDSIMPLICATE] => private
        [organisatie grootte] => Onbekend
        [B2B of B2C] => Onbekend
        [doel groep] => Onbekend
        [situatie in duitsland] => Onbekend
        [URL] => private
        [contactpersonen] => private
        [simplicate_id] => private
    )
    3(
        [DEXCODE] => 
        [LAND] => Country_The Netherlands
        [CP GENDER] => heer
        [OROrganisation] => private
        [STRAATNAAM] => private
        [ORCITY] => private
        [OREMAIL] => private
        [ORWEBSITE] => private
        [CP_PHONE] => private
        [first_name] => private
        [last name] => private
        [FUNCTIE] => private
        [CPMobile] => private
        [CPEmail1] => private
        [ORPostcode] => private
        [ORTelephone] => private
        [ACTIEFINDUITSLAND] => Ja
        [GESPREKSVELDSIMPLICATE] => private
        [organisatie grootte] => Onbekend
        [B2B of B2C] => Onbekend
        [doel groep] => Onbekend
        [situatie in duitsland] => Onbekend
        [URL] => private
        [contactpersonen] => private
        [simplicate_id] => private
    )

2 个答案:

答案 0 :(得分:2)

您应该这样做,而不是使用$jsonstring = array_merge($jsonstring , $body);,它会在每个循环中生成数组

$jsonstring[] = $body;

答案 1 :(得分:1)

您应该这样做:

$jsonstring[] = $body;

这样,您会将创建的每个数组添加到原始数组中