php - Merge Array data based on id
Get the solution ↓↓↓Raw data
Array
(
[0] => Array
(
[bet_id] => 7
[agent_id] => 2
[bet_type] => 1
[amount] => 0
[name] => ethan
)
[1] => Array
(
[bet_id] => 7
[agent_id] => 2
[bet_type] => 3
[amount] => 0
[name] => ethan
)
[2] => Array
(
[bet_id] => 7
[agent_id] => 4
[bet_type] => 1
[amount] => 2
[name] => alex
)
[3] => Array
(
[bet_id] => 7
[agent_id] => 4
[bet_type] => 4
[amount] => 100
[name] => alex
)
)
I get stuck in the logic thinking to separate the above data. The above array will loop all the users data, there have 2 users here. How can I merge it into 2 arrays based on agentId. It will become as below.
Array
(
[0] => Array
(
[agent_id] => 2
[name] => ethan
[1] => 2 //bettype & amount
[3] => 0 //bettype & amount
[bet_id] => 7
)
[1] => Array
(
[agent_id] => 4
[name] => alex
[1] => 0 //bettype & amount
[4] => 100 //bettype & amount
[bet_id] => 7
)
)
Note: the only different data which is agent_id, bet_type, and amount. Other data will be the same.
Answer
Solution:
puttingbet_type
as key andamount
as value makes your data structure looks bad, but lets make it
$result = [];
foreach($data as $user) {
$found = -1;
for($i = 0; $i<count($result); $i++) {
if($result[$i]["agent_id"] == $user["agent_id"]) {
$found = $i;
break;
}
}
// if not found, create new
if($found == -1) {
$result[] = [
"agent_id" => $user["agent_id"],
"name" => $user["name"],
"bet_id" => $user["bet_id"],
$user["bet_type"] => $user["amount"]
];
} else {
$result[$i][$user["bet_type"]] = $user["amount"];
}
}
$data
is your array and the output will be like this
Array
(
[0] => Array
(
[agent_id] => 2
[name] => ethan
[bet_id] => 7
[1] => 0
[3] => 0
)
[1] => Array
(
[agent_id] => 4
[name] => alex
[bet_id] => 7
[1] => 2
[4] => 100
)
)
I suggest yo change thebet_type
position by putting them on child array like this
$result = [];
foreach($data as $user) {
$found = -1;
for($i = 0; $i<count($result); $i++) {
if($result[$i]["agent_id"] == $user["agent_id"]) {
$found = $i;
break;
}
}
// if not found, create new
if($found == -1) {
$result[] = [
"agent_id" => $user["agent_id"],
"name" => $user["name"],
"bet_id" => $user["bet_id"],
"details" => [
$user["bet_type"] => $user["amount"]
]
];
} else {
$result[$i]["details"][$user["bet_type"]] = $user["amount"];
}
}
this will make your data easier to see
the output will like this
(
[0] => Array
(
[agent_id] => 2
[name] => ethan
[bet_id] => 7
[details] => Array
(
[1] => 0
[3] => 0
)
)
[1] => Array
(
[agent_id] => 4
[name] => alex
[bet_id] => 7
[details] => Array
(
[1] => 2
[4] => 100
)
)
)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
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.