Home > other >  Merge two object arrays into one object or array with keys and values
Merge two object arrays into one object or array with keys and values

Time:02-02

So I have a method that I'm calling such as $data = Twitter::get_profile_tweets();

This outputs the following items from the Twitter API return:

object(stdClass)#1591 (3) {
  ["data"]=>
  array(1) {
    [0]=>
    object(stdClass)#1604 (5) {
      ["author_id"]=>
      ...
  ["includes"]=>
  object(stdClass)#1606 (1) {
    ["users"]=>
    array(1) {
      [0]=>
      object(stdClass)#1590 (12) {
        ["profile_image_url"]=>
        ...
}

What would be the best possible way for me to take all the keys from data and users and marge them together? I've tried array_merge and various other things and I'm having lots of trouble.

I'd basically just want an object or array structured like this:

object(stdClass)#1591 (3) {
   array(1) {
     [0]=> {
        ["author_id"] =>
        ["profile_image_url"] =>
        ...
     }
   }
}

Basically take the data array index and match it to the users array index and just merge data[0] with users[0], data[1] with users[1], etc..

What would be my best approach be? All help will be appreciated!

CodePudding user response:

You can use traditional array mapping to merge arrays element by element like that with an arbitrary number of input arrays:

$result = array_map(
    fn(...$args) => array_merge($args),
    $data->data,
    $data->includes->users
);

CodePudding user response:

Here's the simple approach.

<?php

$obj = (object) [
    'data' => [
        'author_id' => "abc123",
    ],
    's' => (object) [
        'users' => [
'profile_image_url' => "http://example.com"
        ],
    ],
];

var_dump($obj);
// object(stdClass)#2 (2) { ["data"]=> array(1) { ["author_id"]=> string(6) "abc123" } ["s"]=> object(stdClass)#1 (1) { ["users"]=> array(1) { ["profile_image_url"]=> string(18) "http://example.com" } } }

$result = simple_merge($obj);

var_dump($result);
// array(1) { [0]=> array(2) { ["author_id"]=> string(6) "abc123" ["profile_image_url"]=> string(18) "http://example.com" } }

// assuming the data array is always the same length as the user array
function simple_merge($object) {
    $out = [];
    
    for ($i = 0; $i < count($object->data); $i  ) {
        foreach ($object->data as $key => $value) {
            $out[$i][$key] = $value;
        }
        
        foreach($object->s->users as $key => $value) {
            $out[$i][$key] = $value;

        }
    }

    return $out;
}

This assumes the arrays are the same length. Let me know if you want me to write you a stronger version that handles arrays of different lengths.

If you can do an array merge to get what you need with less code (and everyone on your team understands how it works and can work with it) then that is probably a good approach.

If you can't get that to work and just need to manipulate the data into the right shape and keep moving it can be sometimes useful to just use very manual and straightforward means.

If you are having trouble with the array merges is it because there's a deeper level of nesting on the second array? (Inside 2 objects?)

  •  Tags:  
  • Related