Reputation: 2181
I want to create this response
'Points' => array(
'Point' => array(
array(
'Type' => 'value',
'Zone' => 'value
),
array(
'Type' => 'value',
'Zone' => 'value'
)
)
)
My code gives me this:
array:1 [▼
"Points" => array:1 [▼
"Point" => array:2 [▼
"Type" => 4
"Zone" => "Front"
]
]
]
Which is very close, unfortunately de Points key is being overwritten anyone knows what I am doing wrong?
$pointsObject = array();
foreach ($points as $point) {
$pointsObject['Points']['Point'] = array(
'Type' => $point->type,
'Zone' => $point->zone
);
}
dd($pointsObject);
Upvotes: 0
Views: 51
Reputation: 179
You are overwriting the value of $pointsObject['Points']['Point'] in every loop. For to avoid overwrite its value you should add [] at the end. Example:
$pointsObject['Points']['Point'][] = array(...);
That push new values into the array in every loop.
Regards.
Upvotes: 3
Reputation: 2080
Try like this
Just add [] after ['Points']['Point']
$pointsObject = array();
foreach ($points as $point) {
$pointsObject['Points']['Point'][] = array(
'Type' => $point->type,
'Zone' => $point->zone
);
}
dd($pointsObject);
Upvotes: 1