Transform a array php

I would like to know if I can transform my array from state 1 to state 2 like the example below.
state 1 (example):
array(5) {
[0]=> array(10) {
["id"]=> "6"
["name"]=> "peter"
["date"]=> "2020-09-25"
["sleep"]=> "20"
["pain"]=> "30"...
[1]=> array(10) {
["id"]=> "6"
["name"]=> "peter"
["date"]=> "2020-09-26"
["sleep"]=> "80"
["pain"]=> "90"...
[2]=> array(10) {
["id"]=> "10"
["name"]=> "john"
["date"]=> "2022-09-25"
["sleep"]=> "25"
["pain"]=> "15"...
[3]=> array(10) {
["id"]=> "10"
["name"]=> "john"
["date"]=> "2022-09-25"
["sleep"]=> "55"
["pain"]=> "65"...
[4]=> array(10) {
["id"]=> "6"
["name"]=> "peter"
["date"]=> "2020-09-27"
["sleep"]=> "40"
["pain"]=> "60"...
ID = 6
andName = peter
appear 3 times andID = 10
andName = john
appear 2 times.
State 2:
array(2){
[0]=>array() {
['id']=> "6"
['name']=>"peter"
["dates" => [2020-09-25, 2020-09-26, 2020-09-27]]
["sleeps" => [20, 80, 40]]
["pains" => [30, 90, 60]]
[1]=>array() {
['id']=> "10"
['name']=>"john"
["dates" => [2022-09-25, 2022-09-25]]
["sleeps" => [25, 55]]
["pains" => [15, 65]]
}
The idea is to have an array with only once the same ID and the same name then arrays with the values of the same field such as
(id => 1, name => kevin, [dates => [date1, date2, date3]], [sleep => s1, s2, s3]]
from this same user.
ID = 6
andName = peter
appear 1 time andID = 10
andName = john
appear 1 time but each with all their own data.
I hope I was understandable.
Answer
Solution:
Yes you can. As yourid
seems to be unique, iterate it and create new array using yourid
index as the main index, you can use array_key_exists() to do it. It should look like this after all:
array(2){
[6]=>array() {
['id']=> "6"
['name']=>"peter"
["dates" => [2020-09-25, 2020-09-26, 2020-09-27]]
["sleeps" => [20, 80, 40]]
["pains" => [30, 90, 60]]
[10]=>array() {
['id']=> "10"
['name']=>"john"
["dates" => [2022-09-25, 2022-09-25]]
["sleeps" => [25, 55]]
["pains" => [15, 65]]
}
That's 5-minutes craft and I'm pretty sure you'll do it yourself.
<?php
$data = [
[
'id' => '6',
'name' => 'peter',
'date' => '2020-09-25',
'sleep' => '20',
'pain' => '30'
],
[
'id' => '6',
'name' => 'peter',
'date' => '2020-09-26',
'sleep' => '80',
'pain' => '90',
],
[
'id' => '10',
'name' => 'john',
'date' => '2022-09-25',
'sleep' => '25',
'pain' => '15',
],
[
'id' => '10',
'name' => 'john',
'date' => '2022-09-25',
'sleep' => '55',
'pain' => '65',
],
[
'id' => '6',
'name' => 'peter',
'date' => '2020-09-27',
'sleep' => '40',
'pain' => '60'
]
];
$newArr = [];
foreach ($data as $oldItem) {
$id = $oldItem['id'];
if (!array_key_exists($id, $newArr)) {
$newArr[$id] = [
'id' => $id,
'name' => $oldItem['name'],
'dates' => [$oldItem['date']],
'sleeps' => [$oldItem['sleep']],
'pains' => [$oldItem['pain']],
];
} else {
$newArr[$id]['dates'][] = $oldItem['date'];
$newArr[$id]['sleeps'][] = $oldItem['sleep'];
$newArr[$id]['pains'][] = $oldItem['pain'];
};
}
echo "<pre>JSON:\n";
echo json_encode($newArr, JSON_PRETTY_PRINT);
echo "\n\nprint_r():\n";
print_r($newArr);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught error: call to undefined function mysqli_connect()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.