[英]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?对我来说,从
data
和users
中获取所有密钥并将它们组合在一起的最佳方式是什么? 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.