简体   繁体   中英

PHP how to push an associative ARRAY (not its values) to another array?

The problem is the following: I'm iterating over some data containing reservations for workstations with a foreach loop. Inside this loop, I'm creating an associative array which looks like this:

  $test = [ "id" => $id, "name" => $forename." ".$surname, "startDate" => date("Ymd", strtotime($startDate)-86400), "endDate" => date("Ymd", strtotime($endDate)), "color" => $color, ]; 

This array then shall be inserted into a numeric array (could be associative as well, but numeric would be easier). I've tried array_merge, but it seems that it tries to insert the keys and their values into the destination array. This poses a problem, since the keys are always the same during each iteration. Therefore, during each iteration, the old keys and values are overwritten. Furthermore, I definitely NEED these arrays to stay arrays anyway, it would make life a lot easier for me later on.

I tried array_push but this doesnt seem to work, I get a NULL response. Then I tried array_merge_recursively, and here the data is at least preserved, but it is split into numerical arrays indexed by the former associative key, referencing the data inside with numerical keys. This isnt the desired result either.

So how can I make it that I get this result:

$numericalArray = [[associativeArray1], [associativeArray2], [associativeArray3]]; //and so on

For further insight into my approach and the problems it causes, here's the function containing the foreach loop. The commented code contains the approaches I've already tried without success:

 function createCalendarMarkupWithoutActiveDauerreservierung($connection, $allReservierungenData){ $calendarData = []; //$calendarData = ["1"]; foreach($allReservierungenData as $reservierungData){ $color = setColorForDaterange($reservierungData['status']); $username = retrieveReservierungUser($connection, $reservierungData["benutzer"]); //$calendarData .= finalizeMarkupWithoutActiveDauerreservierung($reservierungData["id"], $username["vorname"], $username["name"], $reservierungData["anfang"], $reservierungData["ende"], $color); $calendarDataSegment = finalizeMarkupWithoutActiveDauerreservierung($reservierungData["id"], $username["vorname"], $username["name"], $reservierungData["anfang"], $reservierungData["ende"], $color); //$calendarData = array_merge($calendarData, ["2"]); //$calendarData = array_merge($calendarData, $calendarDataSegment); //$calendarData = array_push($calendarData, $calendarDataSegment); //$calendarData = $calendarData + $calendarDataSegment;//array_merge($calendarData, $calendarDataSegment); $calendarData = array_merge_recursive($calendarData, $calendarDataSegment); } return $calendarData; } 

Just append each array to your result:

<?php

$one = [
    'id' => 'one',
    'num' => 1
];
$result[] = $one;
$two = [
    'id' => 'two',
    'num' => 2
];
$result[] = $two;

var_export($result);

Output:

array (
    0 => 
    array (
    'id' => 'one',
    'num' => 1,
    ),
    1 => 
    array (
    'id' => 'two',
    'num' => 2,
    ),
)

If I understand you correctly, wouldn't you want something like this?

<?php
// example code
function createCalendarMarkupWithoutActiveDauerreservierung($connection, $allReservierungenData){

  $calendarData = [];

  foreach($allReservierungenData as $reservierungData){

        $calendarDataSegment = finalizeMarkupWithoutActiveDauerreservierung($reservierungData["id"], $username["vorname"], $username["name"], $reservierungData["anfang"], $reservierungData["ende"], $color);
        $calendarData = $calendarData[$reservierungData["id"]] = $calendarDataSegment);

  }

  return $calendarData;
}

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