简体   繁体   中英

How to merge between two arrays by index

I have two arrays .. first array like this :

$questions = [
    "type" => "form",
    "controls" => []
];

and second array is filled by foreach loop like this :

$count = 0;
foreach($x as $y){
    $controls [
        "id" => $y.$count,
        "id2" => $y.$count+1,
    ]

    $count++;
}

I want to merge all values from the second array to controls index from first array

any help please

No need to merge, just put the result directly into the location you want them in the loop

$questions = [
        "type" => "form",
        "controls" => []
    ];   

$count = 0;
foreach($x as $y){
    $questions['controls'][] =  ["id" => $y.$count,
                                 "id2" => $y.$count+1];
    $count++;
}

Just do:

$questions['controls'] = $controls;

You also need to fix your syntax for filling in $controls , it should be:

$controls[] = [
    "id" => $y.$count,
    "id2" => $y.$count+1,
];

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