From sql I create an array $sails that look like:
Array
(
[0] => Array
(
[type] => RES
[date] => 2022-05-14
[doy] => 133
[skipperid] => 217
[boat] => Laura
[start] => 09:00:00
[end] => 22:00:00
[spots] => 5
[fname] => David
[lname] => Cross
)
[1] => Array
(
[type] => SAIL
[date] => 2022-05-14
[doy] => 133
[skipperid] => 1
[boat] => Avrora
[start] => 10:00:00
[end] => 13:00:00
[spots] => 3
[fname] => Bob
[lname] => Smith
)
[2] => Array
(
[type] => RES
[date] => 2022-05-24
[doy] => 143
[skipperid] => 1
[boat] => Irlbach
[start] => 09:00:00
[end] => 13:30:00
[spots] => 3
[fname] => Bob
[lname] => Smith
)
)
I want to convert this to an object of arrays of objects. Essentially something that looks like
{[{},{}],[{}]}
Here's what I am trying:
$allSails = new stdClass();
$sailCnt = count($sails);
for($i = 0; $i < $sailCnt; $i ){
$dayKey = $sails[$i]['doy'];
$allSails->$dayKey = array();
$itmCnt = count($sails[$i]);
$keys = array_keys($sails[$i]);
$items = new stdClass();
$j = 0;
foreach($keys as $key){
$items->$key = $sails[$i][$keys[$j]];
$j ;
}
array_push($allSails->$dayKey, clone $items);
}
but this is the output:
stdClass Object
(
[133] => Array
(
[0] => stdClass Object
(
[type] => SAIL
[date] => 2022-05-14
[doy] => 133
[skipperid] => 1
[boat] => Avrora
[start] => 10:00:00
[end] => 13:00:00
[spots] => 3
[fname] => Bob
[lname] => Smith
)
)
[143] => Array
(
[0] => stdClass Object
(
[type] => RES
[date] => 2022-05-24
[doy] => 143
[skipperid] => 1
[boat] => Irlbach
[start] => 09:00:00
[end] => 13:30:00
[spots] => 3
[fname] => Bob
[lname] => Smith
)
)
)
CodePudding user response:
This is a clean-up of the OP's code:
$sails = [
['type' => 'RES', 'date' => '2022-05-14', 'doy' => '133', 'skipperid' => '217', 'boat' => 'Laura', 'start' => '09:00:00', 'end' => '22:00:00', 'spots' => '5', 'fname' => 'David', 'lname' => 'Cross'],
['type' => 'SAIL', 'date' => '2022-05-14', 'doy' => '133', 'skipperid' => '1', 'boat' => 'Avrora', 'start' => '10:00:00', 'end' => '13:00:00', 'spots' => '3', 'fname' => 'Bob', 'lname' => 'Smith',],
['type' => 'RES', 'date' => '2022-05-24', 'doy' => '143', 'skipperid' => '1', 'boat' => 'Irlbach', 'start' => '09:00:00', 'end' => '13:30:00', 'spots' => '3', 'fname' => 'Bob', 'lname' => 'Smith',],
];
$allSails = new stdClass();
foreach($sails as $sail) {
$dayKey = $sail['doy'];
if (!array_key_exists($dayKey, (array)$allSails)) {
$allSails->{$dayKey} = [];
}
$items = new stdClass();
foreach ($sail as $key => $value) {
$items->{$key} = $value;
}
$allSails->$dayKey[] = $items;
}
print_r($allSails);
CodePudding user response:
$data = [
['type' => 'RES', 'date' => '2022-05-14', 'doy' => '133', 'skipperid' => '217', 'boat' => 'Laura', 'start' => '09:00:00', 'end' => '22:00:00', 'spots' => '5', 'fname' => 'David', 'lname' => 'Cross'],
['type' => 'SAIL', 'date' => '2022-05-14', 'doy' => '133', 'skipperid' => '1', 'boat' => 'Avrora', 'start' => '10:00:00', 'end' => '13:00:00', 'spots' => '3', 'fname' => 'Bob', 'lname' => 'Smith',],
['type' => 'RES', 'date' => '2022-05-24', 'doy' => '143', 'skipperid' => '1', 'boat' => 'Irlbach', 'start' => '09:00:00', 'end' => '13:30:00', 'spots' => '3', 'fname' => 'Bob', 'lname' => 'Smith',],
];
$result = [];
array_walk($data, function ($item) use (&$result) {
$obj = new stdClass();
foreach ($item as $key => $value) {
$obj->{$key} = $value;
}
$result[$obj->doy][] = $obj;
});
print_r($result);
If you really want to put these into a wrapper object instead of an array amend it to look like this:
$result = new stdClass(); // Changed this line
array_walk($data, function ($item) use (&$result) {
$obj = new stdClass();
foreach ($item as $key => $value) {
$obj->{$key} = $value;
}
$result->{$obj->doy}[] = $obj; // Changed this line
});
CodePudding user response:
Have you tried doing simple casting?
$object=(object) $former_array
If that does not work, you can use a JSON shortcut:
$json=json_encode($array, JSON_FORCE_OBJECT)
$object=json_decode($json, false)
