1

I have arrays included same keys but, values can be different. I have arrays included same keys but, values can be different.

my arrays:

$arr1 = json_decode('{

"Chain": "Monaco Chain",

"Style": "Monaco Swarovski",

"Gauge": "9.50 mm",

"Length": "30.00",

"Color": "YELLOW",

"Karat": "21",

"Lock": "Long",

"2 Tone": "Yes",

"Alternate": "Yes"

}', true);

$arr2 = json_decode('{

"Chain": "Monaco Chain",

"Style": "Monaco Swarovski",

"Gauge": "9.50 mm",

"Length": "30.00",

"Color": "GREEN",

"Karat": "21",

"Lock": "Long",

"2 Tone": "Yes",

"Alternate": "Yes"

}', true);

$arr3 = json_decode('{

"Chain": "Monaco Chain",

"Style": "Monaco Swarovski",

"Gauge": "9.50 mm",

"Length": "30.00",

"Color": "YELLOW",

"Karat": "21",

"Lock": "Long",

"2 Tone": "No",

"Alternate": "Yes"

}', true);

$arr4 = json_decode('{

"Chain": "Monaco Chain",

"Style": "Monaco Swarovski",

"Gauge": "9.50 mm",

"Length": "300.00",

"Color": "YELLOW",

"Karat": "21",

"Lock": "Long",

"2 Tone": "Yes",

"Alternate": "Yes"

}', true)

I have to merge different values as an array and keep them in their own key, so I should get this output:

'{

"Chain": "Monaco Chain",

"Style": "Monaco Swarovski",

"Gauge": "9.50 mm",

"Length": ["30.00", "300.00"],

"Color": ["YELLOW", "GREEN"],

"Karat": "21",

"Lock": "Long",

"2 Tone": ["Yes", "No"],

"Alternate": "Yes"

}'

Note: Please don't care the jsons.

Thanks for your help and time.

7
  • 1
    What you have tried so far ? Commented Aug 31, 2019 at 13:52
  • Thanks for your response @RakeshJakhar, I'm trying build a new array like I needed. Commented Aug 31, 2019 at 14:27
  • How many arrays you have? are you export them from a database or what? Because I know the solution. But it is only for two arrays. If you tell me how do you create those array, maybe I can provide you some better answare :) Commented Aug 31, 2019 at 14:47
  • Thank you @NemanjaJeremic, I have approximately 10k arrays, I want to just merge same keys. Commented Aug 31, 2019 at 14:51
  • 1
    Thanks for your suggestions I think the issue solved. Commented Aug 31, 2019 at 14:57

1 Answer 1

1
function key_concat(...$arrays) {
    if (count($arrays) == 1) return $arrays[0];
    $temp = array_shift($arrays);
    for ($i = 0; $i < count($arrays); $i++) {
        foreach($arrays[$i] as $key => $value) {
            if ((is_string($temp[$key]) && trim($temp[$key]) != trim($arrays[$i][$key])) ||
                (is_array($temp[$key]) && array_search($arrays[$i][$key], $temp[$key]) === false)
            ) {
                if (is_string($temp[$key])) {
                    $temp[$key] = Array($temp[$key], $arrays[$i][$key]);
                } else if (is_array($temp[$key])) {
                    $temp[$key][] = $arrays[$i][$key];
                }
            }
        }
    }
    return $temp;
}
print_r(key_concat($arr1, $arr2, $arr3, $arr4, $arr5));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.