Update: This has still not been solved, so all help is appreciated!
Please note: I stripped out all the unnecessary items and left just the bare minimum needed.
So here I have the following foreach loop inside my method:
$data = [];
foreach($this->get_authenticated_users() as $user) {
.....
... Please note: I removed the non-needed code ...
.....
$data[] = json_decode($json_data);
}
var_dump($data);
So let's say that I have var_dump($data)
which gives me the following output:
array(2) {
[0]=>
object(stdClass)#2503 (2) {
["data"]=>
array(1) {
[0]=>
object(stdClass)#2477 (6) {
["timestamp"]=>
string(24) "2022-02-23T01:26:50 0000"
["id"]=>
string(17) "17899959194451041"
}
}
}
[1]=>
object(stdClass)#2499 (2) {
["data"]=>
array(1) {
[0]=>
object(stdClass)#2476 (6) {
["timestamp"]=>
string(24) "2022-02-20T19:41:17 0000"
["id"]=>
string(17) "1789995915463041"
}
}
}
}
I want to convert the array into a single object, so I use the following foreach
outside of the original foreach
such as this:
$data = [];
foreach($this->get_authenticated_users() as $user) {
.....
... Please note: I removed the non-needed code ...
.....
$data[] = json_decode($json_data);
}
foreach ($data as $key => $single_data) {
$merged_data[$key] = $single_data->data;
}
var_dump($merged_data);
Where var_dump($merged_data)
is the ["data"]
of $data
which outputs the following:
array(2) {
[0]=>
array(1) {
[0]=>
object(stdClass)#2473 (6) {
["timestamp"]=>
string(24) "2022-02-23T01:26:50 0000"
["id"]=>
string(17) "17899959194451041"
}
}
[1]=>
array(1) {
[0]=>
object(stdClass)#2472 (6) {
["timestamp"]=>
string(24) "2022-02-20T19:41:17 0000"
["id"]=>
string(17) "17956063417578984"
}
}
}
So this isn't the outcome that I want, I want the outcome to be more along these lines:
object(stdClass)#2498 (2) {
["data"]=> array(2) {
[0]=> object(stdClass)#2472 (6) {
["id"]=> string(17) "17956063417578984"
["timestamp"]=> string(24) "2022-02-20T19:41:17 0000"
}
[1]=> object(stdClass)#2472 (6) {
["id"]=> string(17) "17956063417578984"
["timestamp"]=> string(24) "2022-02-23T01:26:50 0000"
}
}
}
Can someone spot what I might be doing wrong in my execution?
CodePudding user response:
Now lets go over what happens. Your input data for the for loop is the following:
array(2) {
[0]=>
array(1) {
[0]=>
object(stdClass)#2473 (6) {
["timestamp"]=>
string(24) "2022-02-23T01:26:50 0000"
["id"]=>
string(17) "17899959194451041"
}
}
[1]=>
array(1) {
[0]=>
object(stdClass)#2472 (6) {
["timestamp"]=>
string(24) "2022-02-20T19:41:17 0000"
["id"]=>
string(17) "17956063417578984"
}
}
}
Your for loop iterates over every entry and gets key and value. An entry of this array is:
[0]=>
object(stdClass)#2503 (2) {
["data"]=>
array(1) {
[0]=>
object(stdClass)#2477 (6) {
["timestamp"]=>
string(24) "2022-02-23T01:26:50 0000"
["id"]=>
string(17) "17899959194451041"
}
}
}
The key of this entry is 0, that explains why your output array an entry with key 0. You also take the contents of "data" of your entry and put it inside this new entry of your output array. The the content of "data" is the following:
array(1) {
[0]=>
object(stdClass)#2477 (6) {
["timestamp"]=>
string(24) "2022-02-23T01:26:50 0000"
["id"]=>
string(17) "17899959194451041"
}
}
If you now put these things together, you place the "data" value into a new array at key 0, which looks like this:
[0]=>
array(1) {
[0]=>
object(stdClass)#2473 (6) {
["timestamp"]=>
string(24) "2022-02-23T01:26:50 0000"
["id"]=>
string(17) "17899959194451041"
}
}
This is exactly what you observe.
Your for loop would have to look like this:
foreach ($data as $key => $single_data) {
$merged_data[$key] = $single_data->data[0];
}
The problem was, that "data" contains an array, but you want to access the first entry of that array. After that, you have to place the new output array inside another stdClass object to achieve your wanted data structure.
CodePudding user response:
I think you have Lots of unnecessary code, I would be more sure if I could see some of the irrelevant code, but this is basically what I think you can do to achieve what you want in a lot less code.
$data = new stdClass();
foreach($this->get_authenticated_users() as $user) {
.....
... Please note: I removed the non-needed code ...
.....
$data->data[] = json_decode($json_data);
}
var_dump($data);