简体   繁体   English

将两个 object arrays 合并为一个 object 或带有键和值的数组

[英]Merge two object arrays into one object or array with keys and values

So I have a method that I'm calling such as $data = Twitter::get_profile_tweets();所以我有一个我正在调用的方法,例如$data = Twitter::get_profile_tweets();

This outputs the following items from the Twitter API return:这会从 Twitter API 返回中输出以下项目:

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?对我来说,从datausers中获取所有密钥并将它们组合在一起的最佳方式是什么? I've tried array_merge and various other things and I'm having lots of trouble.我已经尝试过 array_merge 和其他各种东西,但我遇到了很多麻烦。

I'd basically just want an object or array structured like this:我基本上只想要一个 object 或结构如下的数组:

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..基本上取数据数组索引并将其与用户数组索引匹配,然后将data[0] with users[0]data[1] with users[1]等合并。

What would be my best approach be?我最好的方法是什么? All help will be appreciated!所有帮助将不胜感激!

You can use traditional array mapping to merge arrays element by element like that with an arbitrary number of input arrays:您可以使用传统的数组映射将 arrays 元素与任意数量的输入 arrays 类似:

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

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.这假设 arrays 的长度相同。 Let me know if you want me to write you a stronger version that handles arrays of different lengths.如果你想让我给你写一个更强大的版本来处理不同长度的 arrays,请告诉我。

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?) (在 2 个物体内?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将两个数组合并为一个数组(相同的键) - Merge two arrays into one array (identic keys) 如何将两个对象数组合并和asort()合并为一个新数组? - How to merge and asort() two object arrays into one new array? 如何通过仅接管与第一个具有相同键的第二个数组中的值来合并两个 arrays? - How to merge two arrays by taking over only values from the second array that has the same keys as the first one? 我正在尝试合并两个数组,一个包含值,另一个仅包含我想用于该数组的键 - I am trying to merge two arrays, one contains values and the other just contains the keys i would like to use for the array 合并两个数组,将第一个数组值保留为键,将第二个数组值保留为值 - Merge two arrays keeping the first array values as keys and second array values as values 如何根据键值对将两个数组合并为一个数组 - how to merge two arrays in one array based on key and values pair 如何在一个数组php中合并两个数组值 - how to merge two arrays values in one array php 如何将两个数组的值合并到一个数组中并保存在数据库中 - How can merge two arrays values in one array and save in database 使用相同的键将2个阵列合并为一个阵列 - Merge 2 arrays into one array with same keys 通过键合并两个数组 - Merge two arrays by keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM