Merging associative array values where keys match within the same array












-1















I am generating an associative array that I want to use to generate a JSON object but have a problem of duplicate keys with different values. I want the where the keys are duplicate, the values of are merged. I have checked online but all the solutions reference 2 different arrays not the same array.



The JSON Object is:



{
"trailervideos": [
{
"category": "Video Games",
"videos": {
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
},
{
"category": "Entertainment",
"videos": {
"description": "",
"title": "intro"
}
}
]
}


What I want to achieve is all the values of repeated key "Video Games" combined so that I can generate a JSON object like:



{"trailervideos":[{"category":"Video Games","videos":[{"description":"Trailer of the game Dark Souls II","title":"Dark Souls 2 Announcement Trailer"},{"description":"Trailer of the DLC Scholar of the First Sin for the game Dark Souls II","title":"Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"},{"description":"Trailer of the DLC Ashes of Ariendel for the game Dark Souls III","title":"Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"}],{"category":"Entertainment","videos":{"description":"","title":"intro"}}]}









share|improve this question




















  • 1





    Your target JSON is not valid JSON. Please have it validated first and then post the correct version.

    – trincot
    Jan 19 at 12:15
















-1















I am generating an associative array that I want to use to generate a JSON object but have a problem of duplicate keys with different values. I want the where the keys are duplicate, the values of are merged. I have checked online but all the solutions reference 2 different arrays not the same array.



The JSON Object is:



{
"trailervideos": [
{
"category": "Video Games",
"videos": {
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
},
{
"category": "Entertainment",
"videos": {
"description": "",
"title": "intro"
}
}
]
}


What I want to achieve is all the values of repeated key "Video Games" combined so that I can generate a JSON object like:



{"trailervideos":[{"category":"Video Games","videos":[{"description":"Trailer of the game Dark Souls II","title":"Dark Souls 2 Announcement Trailer"},{"description":"Trailer of the DLC Scholar of the First Sin for the game Dark Souls II","title":"Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"},{"description":"Trailer of the DLC Ashes of Ariendel for the game Dark Souls III","title":"Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"}],{"category":"Entertainment","videos":{"description":"","title":"intro"}}]}









share|improve this question




















  • 1





    Your target JSON is not valid JSON. Please have it validated first and then post the correct version.

    – trincot
    Jan 19 at 12:15














-1












-1








-1








I am generating an associative array that I want to use to generate a JSON object but have a problem of duplicate keys with different values. I want the where the keys are duplicate, the values of are merged. I have checked online but all the solutions reference 2 different arrays not the same array.



The JSON Object is:



{
"trailervideos": [
{
"category": "Video Games",
"videos": {
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
},
{
"category": "Entertainment",
"videos": {
"description": "",
"title": "intro"
}
}
]
}


What I want to achieve is all the values of repeated key "Video Games" combined so that I can generate a JSON object like:



{"trailervideos":[{"category":"Video Games","videos":[{"description":"Trailer of the game Dark Souls II","title":"Dark Souls 2 Announcement Trailer"},{"description":"Trailer of the DLC Scholar of the First Sin for the game Dark Souls II","title":"Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"},{"description":"Trailer of the DLC Ashes of Ariendel for the game Dark Souls III","title":"Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"}],{"category":"Entertainment","videos":{"description":"","title":"intro"}}]}









share|improve this question
















I am generating an associative array that I want to use to generate a JSON object but have a problem of duplicate keys with different values. I want the where the keys are duplicate, the values of are merged. I have checked online but all the solutions reference 2 different arrays not the same array.



The JSON Object is:



{
"trailervideos": [
{
"category": "Video Games",
"videos": {
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
}
},
{
"category": "Video Games",
"videos": {
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
},
{
"category": "Entertainment",
"videos": {
"description": "",
"title": "intro"
}
}
]
}


What I want to achieve is all the values of repeated key "Video Games" combined so that I can generate a JSON object like:



{"trailervideos":[{"category":"Video Games","videos":[{"description":"Trailer of the game Dark Souls II","title":"Dark Souls 2 Announcement Trailer"},{"description":"Trailer of the DLC Scholar of the First Sin for the game Dark Souls II","title":"Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"},{"description":"Trailer of the DLC Ashes of Ariendel for the game Dark Souls III","title":"Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"}],{"category":"Entertainment","videos":{"description":"","title":"intro"}}]}






php arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 12:16









trincot

122k1586119




122k1586119










asked Jan 19 at 9:34









user3682205user3682205

8611




8611








  • 1





    Your target JSON is not valid JSON. Please have it validated first and then post the correct version.

    – trincot
    Jan 19 at 12:15














  • 1





    Your target JSON is not valid JSON. Please have it validated first and then post the correct version.

    – trincot
    Jan 19 at 12:15








1




1





Your target JSON is not valid JSON. Please have it validated first and then post the correct version.

– trincot
Jan 19 at 12:15





Your target JSON is not valid JSON. Please have it validated first and then post the correct version.

– trincot
Jan 19 at 12:15












1 Answer
1






active

oldest

votes


















1














There is no 'JSON-object'. JSON is a string reprentation a JavasScript Object Notation you can build a PHP object-array-structure from, using json_decode. To get a JSON string from PHP variables, the function json_encode is used.



The easiest way is to iterate over the trailervideos building a new associative array since there can only be unique keys. We can eleminate the key names by the function array_values later to prevent json_encode from building an object instead of an array because associative arrays do not exist in JavaScript.



This version does process all categories, "Video Games" as well as "Entertainment" and, if available, even more.



$a = ;

foreach (($o = json_decode($json))->trailervideos as $v)
{
isset($a[$v->category]) || $a[$v->category] = new stdClass();
$a[$v->category]->category = $v->category;
$a[$v->category]->videos = $v->videos;
}

$o->trailervideos = array_values($a);

var_dump(json_encode($o));


The (formatted) JSON result looks like that:



{
"trailervideos": [
{
"category": "Video Games",
"videos": [
{
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
},
{
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
},
{
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
]
},
{
"category": "Entertainment",
"videos": [
{
"description": "",
"title": "intro"
}
]
}
]
}





share|improve this answer


























  • Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

    – user3682205
    Jan 19 at 18:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265748%2fmerging-associative-array-values-where-keys-match-within-the-same-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There is no 'JSON-object'. JSON is a string reprentation a JavasScript Object Notation you can build a PHP object-array-structure from, using json_decode. To get a JSON string from PHP variables, the function json_encode is used.



The easiest way is to iterate over the trailervideos building a new associative array since there can only be unique keys. We can eleminate the key names by the function array_values later to prevent json_encode from building an object instead of an array because associative arrays do not exist in JavaScript.



This version does process all categories, "Video Games" as well as "Entertainment" and, if available, even more.



$a = ;

foreach (($o = json_decode($json))->trailervideos as $v)
{
isset($a[$v->category]) || $a[$v->category] = new stdClass();
$a[$v->category]->category = $v->category;
$a[$v->category]->videos = $v->videos;
}

$o->trailervideos = array_values($a);

var_dump(json_encode($o));


The (formatted) JSON result looks like that:



{
"trailervideos": [
{
"category": "Video Games",
"videos": [
{
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
},
{
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
},
{
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
]
},
{
"category": "Entertainment",
"videos": [
{
"description": "",
"title": "intro"
}
]
}
]
}





share|improve this answer


























  • Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

    – user3682205
    Jan 19 at 18:00
















1














There is no 'JSON-object'. JSON is a string reprentation a JavasScript Object Notation you can build a PHP object-array-structure from, using json_decode. To get a JSON string from PHP variables, the function json_encode is used.



The easiest way is to iterate over the trailervideos building a new associative array since there can only be unique keys. We can eleminate the key names by the function array_values later to prevent json_encode from building an object instead of an array because associative arrays do not exist in JavaScript.



This version does process all categories, "Video Games" as well as "Entertainment" and, if available, even more.



$a = ;

foreach (($o = json_decode($json))->trailervideos as $v)
{
isset($a[$v->category]) || $a[$v->category] = new stdClass();
$a[$v->category]->category = $v->category;
$a[$v->category]->videos = $v->videos;
}

$o->trailervideos = array_values($a);

var_dump(json_encode($o));


The (formatted) JSON result looks like that:



{
"trailervideos": [
{
"category": "Video Games",
"videos": [
{
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
},
{
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
},
{
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
]
},
{
"category": "Entertainment",
"videos": [
{
"description": "",
"title": "intro"
}
]
}
]
}





share|improve this answer


























  • Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

    – user3682205
    Jan 19 at 18:00














1












1








1







There is no 'JSON-object'. JSON is a string reprentation a JavasScript Object Notation you can build a PHP object-array-structure from, using json_decode. To get a JSON string from PHP variables, the function json_encode is used.



The easiest way is to iterate over the trailervideos building a new associative array since there can only be unique keys. We can eleminate the key names by the function array_values later to prevent json_encode from building an object instead of an array because associative arrays do not exist in JavaScript.



This version does process all categories, "Video Games" as well as "Entertainment" and, if available, even more.



$a = ;

foreach (($o = json_decode($json))->trailervideos as $v)
{
isset($a[$v->category]) || $a[$v->category] = new stdClass();
$a[$v->category]->category = $v->category;
$a[$v->category]->videos = $v->videos;
}

$o->trailervideos = array_values($a);

var_dump(json_encode($o));


The (formatted) JSON result looks like that:



{
"trailervideos": [
{
"category": "Video Games",
"videos": [
{
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
},
{
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
},
{
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
]
},
{
"category": "Entertainment",
"videos": [
{
"description": "",
"title": "intro"
}
]
}
]
}





share|improve this answer















There is no 'JSON-object'. JSON is a string reprentation a JavasScript Object Notation you can build a PHP object-array-structure from, using json_decode. To get a JSON string from PHP variables, the function json_encode is used.



The easiest way is to iterate over the trailervideos building a new associative array since there can only be unique keys. We can eleminate the key names by the function array_values later to prevent json_encode from building an object instead of an array because associative arrays do not exist in JavaScript.



This version does process all categories, "Video Games" as well as "Entertainment" and, if available, even more.



$a = ;

foreach (($o = json_decode($json))->trailervideos as $v)
{
isset($a[$v->category]) || $a[$v->category] = new stdClass();
$a[$v->category]->category = $v->category;
$a[$v->category]->videos = $v->videos;
}

$o->trailervideos = array_values($a);

var_dump(json_encode($o));


The (formatted) JSON result looks like that:



{
"trailervideos": [
{
"category": "Video Games",
"videos": [
{
"description": "Trailer of the game Dark Souls II",
"title": "Dark Souls 2 Announcement Trailer"
},
{
"description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II",
"title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer"
},
{
"description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III",
"title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4"
}
]
},
{
"category": "Entertainment",
"videos": [
{
"description": "",
"title": "intro"
}
]
}
]
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 19 at 12:33

























answered Jan 19 at 12:01









Quasimodo's cloneQuasimodo's clone

3,74611028




3,74611028













  • Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

    – user3682205
    Jan 19 at 18:00



















  • Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

    – user3682205
    Jan 19 at 18:00

















Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

– user3682205
Jan 19 at 18:00





Thanks @Quasimodo's clone, worked. My problem started with how I was building the initial associative array before encoding it. Messed up with a foreach loop. Thanks once again

– user3682205
Jan 19 at 18:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265748%2fmerging-associative-array-values-where-keys-match-within-the-same-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre