Currently I am trying to restructure a multidimensional array that is the result of a database query. The goal is to finally encode the array as JSON as follows, but the php function i have written isn’t working (see code at end):
desiredJSONoutput = {"TypeA":[
{"1":[
{"TypeB":[4,5]},
{"TypeC":[7,8,4]}]},
{"2":[
{"TypeB":[2,3]},
{"TypeC":[6]}]},
{"4":[
{"TypeB":[33,12]},
{"TypeC":[908]}]}]}
Database structure is:
fromType fromID toType toID
-----------------------------------------------------
TypeA 1 TypeB 4
TypeA 1 TypeB 5
TypeA 1 TypeC 7
TypeA 1 TypeC 8
TypeA 1 TypeC 4
TypeA 2 TypeB 2
TypeA 2 TypeB 3
TypeA 2 TypeC 6
TypeA 2 TypeB 33
TypeA 2 TypeB 12
TypeA 2 TypeC 908
the result of my current sql query is this php array:
Array
(
Array
(
['fromType'] => 'TypeA'
['fromID'] => '1'
['toType'] => 'TypeB'
['toID'] => '4'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '1'
['toType'] => 'TypeB'
['toID'] => '5'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '1'
['toType'] => 'TypeC'
['toID'] => '7'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '1'
['toType'] => 'TypeC'
['toID'] => '8'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '1'
['toType'] => 'TypeC'
['toID'] => '4'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '2'
['toType'] => 'TypeB'
['toID'] => '2'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '2'
['toType'] => 'TypeB'
['toID'] => '3'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '2'
['toType'] => 'TypeC'
['toID'] => '6'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '3'
['toType'] => 'TypeB'
['toID'] => '33'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '3'
['toType'] => 'TypeB'
['toID'] => '12'
)
Array
(
['fromType'] => 'TypeA'
['fromID'] => '3'
['toType'] => 'TypeC'
['toID'] => '908'
)
)
and the restructured array i need before encoding to JSON I think is:
Array
(
['TypeA'] => Array
(
['1'] => Array
(
['TypeB'] => Array
(
[0] => 4
[1] => 5
)
['TypeC'] => Array
(
[0] => 7
[1] => 8
[2] => 4
)
)
['2'] => Array
(
['TypeB'] => Array
(
[0] => 2
[1] => 3
)
['TypeC'] => Array
(
[0] => 6
)
)
['3'] => Array
(
['TypeB'] => Array
(
[0] => 33
[1] => 12
)
['TypeC'] => Array
(
[0] => 908
)
)
)
)
I am not sure why the PHP code as follows is not doing the trick to restructure the returned php array to the structure I want:
class Utilities {
public function linksReEncode($rowsArray) {
$result = array();
foreach ($rowsArray as $row) {
$fromtype = $row['fromtype'];
$fromID = $row['fromID'];
$toType = $row['toType'];
$toID = $row['toID'];
$arr = $result[$fromType][$fromID][$toType];
array_push($arr, $toID);
}
}
}
EDIT
based on further research and the first answer from @Waygood here’s the function that i am now using and which is working. I wonder if all the checking for existing keys is necessary and if this is the most economical way to achieve this
public function linksReEncode($rows) {
$result = array();
foreach ($rows as $row) {
$fromType = $row['fromType'];
$fromID = $row['fromID'];
$toType = $row['toType'];
$toID = $row['toID'];
if(!array_key_exists($fromType, $result))
$result[$fromType] = array();
if(!array_key_exists($fromID, $result[$fromType]))
$result[$fromType][$fromID] = array();
if(!array_key_exists($toType, $result[$fromType][$fromID]))
$result[$fromType][$fromID][$toType] = array();
array_push($result[$fromType][$fromID][$toType], $toID);
}
return $result;
}
you are just redefining $arr within each loop, not altering $result
change:
to: