简体   繁体   中英

How to make a multi dimensional array in a foreach loop

I'm trying to create a multi dimensional array in a foreach loop.

First let me place my code:

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

Now let me explain. Before the loop I create an empty array called jsonstring. Inside the loop i create another array called body. The body array contains data from the array im looping through. At the end of the loop I merge body with json string.

The problem is, whenever I print jsonstring outside the loop it only gives me 1 instance of the loop. The results look like this:

(
    [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
)

Just once, but i want every instance of the loop stored in the array.

My expectation is:

 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
    )

you should doing this instead of using $jsonstring = array_merge($jsonstring , $body); its will generate array every loop

$jsonstring[] = $body;

You should rather be doing :

$jsonstring[] = $body;

This way you'll add each array created to the original array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM