I'm trying to only get the Content from an array back without the access key.
I have the following function
protected static function getContent() {
$title = $storage->getTransUnitById($dive->getScenario()->getTitle());
$text = $storage->getTransUnitById($dive->getScenario()->getText());
$additionalText = $storage->getTransUnitById($dive->getScenario()->getAdditionalText());
$buttonText = $storage->getTransUnitById($dive->getScenario()->getButtonText());
$result = null;
$result['title'] = $title instanceof TransUnit ?
$title->getTranslation($locale)->getContent() :
null;
$result['text'] = $text instanceof TransUnit ?
$text->getTranslation($locale)->getContent() :
null;
$result['additionalText'] = $additionalText instanceof TransUnit ?
$additionalText->getTranslation($locale)->getContent() :
null;
$result['buttonText'] = $buttonText instanceof TransUnit ?
$buttonText->getTranslation($locale)->getContent() :
null;
return $result;
}
Which i use like so
'scenario' => [
self::getContent($storage, $dive, $locale),
'image' => array_key_exists('scenario', $pathing) ? $pathing['scenario'] : null
],
The Problem is I get the following output
"scenario": {
"0": {
"title": "test",
"text": "test",
"additionalText": "test",
"buttonText": "test"
},
"image": "http://test.local/images/test.png"
},
What I would want is not to have the array index. Like so
"scenario": {
"title": "test",
"text": "test",
"additionalText": "test",
"buttonText": "test"
"image": "http://test.local/images/test.png"
},
How can i achieve this result ? I'm unsure - or maybe im overthining this
EDIT:
After trying array_merge like suggested i don't quite get the same output as before.
Now i have and array in an object.
"scenario": [
{
"image": "http://test.local:/images/test.png",
"title": "test",
"text": "test",
"additionalText": "test",
"buttonText": "test"
}
],
But i would like it to look like the old output (just an object)
"example": {
"title": "test",
"text": "test",
"additionalText": test,
"buttonText": "test",
"image": "http://test.local:/images/test.png"
},
CodePudding user response:
You can use argument unpacking with the triple-dot notation, to achieve this.
'scenario' => [
'image' => array_key_exists('scenario', $pathing) ? $pathing['scenario'] : null,
...self::getContent($storage, $dive, $locale)
]
Edit: This works, if you're using PHP 7.4 (which you definitely should)
CodePudding user response:
You could use array_merge()
to create the right format:
scenario=> (object) array_merge(
['image' => array_key_exists('scenario', $pathing) ? $pathing['scenario'] : null],
self::getContent($storage, $dive, $locale)),