'Use associative array to merge two objects based on ID

So I have the following method $data = Twitter::get_profile_tweets() which outputs the following response:

object(stdClass)#1597 (3) {
  ["data"]=>
  array(5) {
    [0]=>
    object(stdClass)#1612 (4) {
      ["text"]=>
      string(231) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
      ["id"]=>
      string(19) "1487953501825163264"
      ["author_id"]=>
      string(19) "1487104761291849728"
    }
    [1]=>
    object(stdClass)#1586 (5) {
      ["attachments"]=>
      object(stdClass)#1606 (1) {
        ["media_keys"]=>
        array(1) {
          [0]=>
          string(22) "16_1487594952561467393"
        }
      }
      ["text"]=>
      string(56) "Telling myself how I really feel"
      ["id"]=>
      string(19) "1487594959125549059"
      ["author_id"]=>
      string(19) "1487104761291849728"
    }
  }
  ["includes"]=>
  object(stdClass)#1604 (2) {
    ["users"]=>
    array(1) {
      [0]=>
      object(stdClass)#1591 (9) {
        ["profile_image_url"]=>
        string(78) "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"
        ["id"]=>
        string(19) "1487104761291849728"
        ["name"]=>
        string(14) "FHTechServices"
      }
    }
  }
}

The API response makes no sense, but I guess you have to match the data tweets return to the corresponding user.


Here is what I'm attempting to do:

Let's say this would be the ideal return:

object(stdClass)#1597 (3) {
  array(2) {
    [0] => {
        [0] => {
            object(stdClass)#1612 (4) {
              ["text"]=>
              string(231) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
              ["id"]=>
              string(19) "1487953501825163264"
              ["author_id"]=>
              string(19) "1487104761291849728"
            }
        [1] => 
            object(stdClass)#1591 (9) {
              ["profile_image_url"]=>
              string(78) "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"
              ["id"]=>
              string(19) "1487104761291849728"
              ["name"]=>
              string(14) "FHTechServices"
            }
        }
    }
    [1] => {
        [0] => {
            object(stdClass)#1612 (4) {
              ["text"]=>
              string(231) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
              ["id"]=>
              string(19) "1487953501825163264"
              ["author_id"]=>
              string(19) "1487104761291849728"
            }
        }
        [1] => {
            object(stdClass)#1591 (9) {
              ["profile_image_url"]=>
              string(78) "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"
              ["id"]=>
              string(19) "1487104761291849728"
              ["name"]=>
              string(14) "FHTechServices"
            }
        }
    }
  }
}

We looked at ["data"] in the first index and grabbed the Author ID and then looked at the ID inside ["includes"]["users"] and if it matches, merge its own array of object.

Unsure about the below, since there might be key collisions:

What if we can just merge it all into one single array or object, so then I don't have to do [0] for the tweet keys and [1] for the user keys, I can just loop and grab the keys such as:

[0] => {
    [0] => {
        object(stdClass)#1612 (4) {
          ["text"]=>
          string(231) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
          ["id"]=>
          string(19) "1487953501825163264"
          ["author_id"]=>
          string(19) "1487104761291849728"
          ["profile_image_url"]=>
          string(78) "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"
          ["id"]=>
          string(19) "1487104761291849728"
          ["name"]=>
          string(14) "FHTechServices"
        }
    }
}

This sounds extremely confusing, so all help will be really appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source