I want to merge two same keys in an array and get the sum of the values.
I want the same structure as it is now.Because this data needs to be converted to JSON.
This is what i get now.
{
"data": [{
"count_of_invites": 5,
"user": "Rajesh",
"id": "53"
},
{
"count_of_invites": 9,
"user": "Student",
"id": "45"
},
{
"count_of_invites": 4,
"user": "Student",
"id": "45"
}
]
}
As you can see the id 45 are repeated.As i want the result as,
Expected output
{
"data": [{
"count_of_invites": 5,
"user": "Rajesh",
"id": "53"
},
{
"count_of_invites": 13,
"user": "Student",
"id": "45"
}
]
}
As you can see the duplicate entry should be removed as well as the count_of_invites of duplicate entry should be added.
<?php
$data = [
[
'id' => 2,
'name' => 'Paul',
'count' => 4
],
[
'id' => 3,
'name' => 'Peter',
'count' => 5
],
[
'id' => 3,
'name' => 'Peter',
'count' => 7
]
];
foreach($data as $array)
$counts[$array['id']][] = $array['count'];
$counts = array_map('array_sum', $counts);
foreach($data as $k => $array)
$data[$k]['count'] = $counts[$array['id']];
$data = array_unique($data, SORT_REGULAR);
print json_encode($data, JSON_PRETTY_PRINT);
Output:
[
{
"id": 2,
"name": "Paul",
"count": 4
},
{
"id": 3,
"name": "Peter",
"count": 12
}
]
You can achieve it this way:
$ids = array();
$output = array();
foreach ($input as $value) {
if (!isset($ids[$value["id"]])) {
$ids[$value["id"]]=$count($output);
$output[]=$value;
} else {
$output[$ids[$value["id"]]]["count_of_invites"] = $value["count_of_invites"];
$output[$ids[$value["id"]]]["user"] = $value["user"];
}
}
The count method was declared as variable and i've added with addition assignment operator.
Thank You for helping.
$ids = array();
$output = array();
foreach ($response as $value) {
if (!isset($ids[$value["id"]])) {
$ids[$value["id"]] = count($output);
$output[] = $value;
}
else {
$output[$ids[$value["id"]]]["count_of_invites"] += $value["count_of_invites"];
$output[$ids[$value["id"]]]["user"] = $value["user"];
}
}
Related
I'm building a tree feature for my app. My tree has infinite level. I'm using this functions to build my tree (you can try my code here https://onlinephp.io/c/a50bc):
function build(array $array): array
{
if (count($array) <= 0) {
return [
'leaf_ids' => [],
'parent_ids' => [],
'tree' => [],
];
}
$groupByParent = [];
foreach ($array as $arr) {
$parent_id = empty($arr['parent_id']) ? 0 : $arr['parent_id'];
$groupByParent[$parent_id][] = $arr;
}
return makeTree($groupByParent, $groupByParent[0]);
}
function makeTree(array &$parentList, array $items): array
{
$tree = [];
$leafIds = [];
$parentIds = [];
foreach ($items as $item) {
if (isset($parentList[$item['id']])) {
$item['children'] = makeTree($parentList, $parentList[$item['id']])['tree'];
}
if (!isset($item['children'])) {
$leafIds[] = $item['id'];
} else {
$parentIds[] = $item['id'];
}
$tree[] = $item;
}
return ['tree' => $tree, 'leaf_ids' => $leafIds, 'parent_ids' => $parentIds];
}
$array = [
[
'id' => 1,
'parent_id' => NULL,
'value' => 'Hi',
],
[
'id' => 2,
'parent_id' => NULL,
'value' => 'Bye',
],
[
'id' => 3,
'parent_id' => 1,
'value' => 'Hey',
],
[
'id' => 4,
'parent_id' => 1,
'value' => 'Hello',
],
[
'id' => 5,
'parent_id' => 2,
'value' => 'Cya',
],
[
'id' => 6,
'parent_id' => 5,
'value' => 'Byebye',
],
];
print_r(json_encode(build($array), JSON_PRETTY_PRINT));
I'd like to get the list of leafs and parents but my code doesn't work at all. At the end I don't have all parents ids and my leaf_ids array is empty.
I got this output:
{
"tree": [
{
"id": 1,
"parent_id": null,
"value": "Hi",
"children": [
{
"id": 3,
"parent_id": 1,
"value": "Hey"
},
{
"id": 4,
"parent_id": 1,
"value": "Hello"
}
]
},
{
"id": 2,
"parent_id": null,
"value": "Bye",
"children": [
{
"id": 5,
"parent_id": 2,
"value": "Cya",
"children": [
{
"id": 6,
"parent_id": 5,
"value": "Byebye"
}
]
}
]
}
],
"leaf_ids": [],
"parent_ids": [
1,
2
]
}
The output that I want for leaf_ids is: "leaf_ids": [3,4,6] and for parent_ids: "parent_ids": [1,2,5].
How can I do to return leafs and parents list ?
When you make the recursive call, you will need to merge the results for leaf_ids and parent_ids obtained from sub tree results as well, like below:
<?php
if (isset($parentList[$item['id']])) {
$sub_tree = makeTree($parentList, $parentList[$item['id']]);
$item['children'] = $sub_tree['tree'];
$leafIds = array_merge($leafIds, $sub_tree['leaf_ids']);
$parentIds = array_merge($parentIds, $sub_tree['parent_ids']);
}
Online Demo
I have the following JSON object:
{
"id": 1,
"name": null,
"block": {
"type": "none",
"descends": [
{
"operation":"sum",
"descends":[
{
"label":2,
"value":false
}
]
},
{
"label": 1,
"value": 3,
},
{
"label": 2,
"value": 2
}
],
"label": 1,
"value": true
}
}
I want to collect all the label and value attributes and store them in an array, so I created the following function:
public function collectValues($arr){
$finalValues = [];
foreach($arr as $key => $element){
if($key=='block'){
foreach($element as $key2 => $block){
if($key2=='descends'){
foreach($block as $key3 => $node_block){
if($key3=='descends'){
foreach($node_block as $key4 => $anotherNode){
if($key4 == 'descends'){
foreach($anotherNode as $finalNode){
$finalValues [] = array('lable'=>$finalNode->label, 'value' =>$finalNode->value);
}
}
}
}
else{
$finalValues [] = array('lable' => $node_block->label, 'value' => $node_block->value);
}
}
}
}
$finalValues [] = array('lable'=> $element->label, 'value' => $element->value);
}
}
return $finalValues;
}
The function works and I get the following:
[
{
"lable": 2,
"value": false
},
{
"lable": 1,
"value": 3
},
{
"lable": 2,
"value": 2
},
{
"lable": 1,
"value": true
}
]
The problem is that the JSON object can contain more descends like:
{
"id": 1,
"name": null,
"block": {
"type": "none",
"descends": [
{
"operation":"sum",
"descends":[
{
"operation":"sum",
"descends":[
{
"label":2,
"value":false
}
],
"label":2,
"value":false
}
]
},
{
"operation":"sum",
"descends":[
{
"label":2,
"value":false
}
],
"label": 1,
"value": 3,
},
{
"label": 2,
"value": 2
}
],
"label": 1,
"value": true
}
}
This means I will have to add more foreach loops. A good way to handle such a situation is by using recursive. How can I transform the function above into a recursive one?
Your looped function does not make sense to change. Easier to rewrite from scratch
Here is my use case for recursion. Maybe there is an easier option, but I didn’t chase optimization
$array = json_decode($json, true); //in $json your JSON)
$result = []; // array for result
checkLabel($array, $result);
print_r($result);
function checkLabel(array $array, &$result){
//first check every element on nested array
foreach ($array as $key => $value){
if (is_array($value)) {
//if found call recursive function
checkLabel($value, $result);
}
}
//then check 'label' key.. its mean we have attribute
if (array_key_exists('label', $array)) {
//save it
$result[] = [
'label' => $array['label'],
'value' => $array['value']??'' //if have label but without value)
];
}
}
I need help on how to group the collection below by location and then by sizes:
[
{
"location_id": 1,
"location_address": "Some address",
"location_image": "https://some/image.url",
"size_id": 1,
"size_code": "S",
"available_boxes": 3
},
{
"location_id": 1,
"location_address": "Some address",
"location_image": "https://some/image.url",
"size_id": 2,
"size_code": "M",
"available_boxes": 4
},
{
"location_id": 1,
"location_address": "Some address",
"location_image": "https://some/image.url",
"size_id": 3,
"size_code": "L",
"available_boxes": 5
},
{
"location_id": 2,
"location_address": "Some other address",
"location_image": "https://some/other/image.url",
"size_id": 4,
"size_code": "XL",
"available_boxes": 10
}
]
Here is the expected result:
Really appreciate it, thanks in advance!
Might not be elegant, but this does the job:
$locations = $this->records->unique('location_id');
$transformed = [];
foreach ($locations as $location) {
$sizes = $this->records->whereStrict('location_id', $location->location_id);
$transformedSizes = [];
foreach ($sizes as $size) {
$transformedSizes[] = [
'id' => $size->size_id,
'code' => $size->size_code,
'available_boxes' => $size->available_boxes
];
};
$transformed[] = array_merge(
[
'id' => $location->location_id,
'address' => $location->location_address,
'image' => $location->location_image
],
[
'sizes' => $transformedSizes
]
);
}
return $transformed;
I have this array of object:
[
{
"id": 1,
"name": "Carbo",
"menus": [
{
"id": 33,
"name": "FloralWhite",
"image": {
"web": "https://lorempixel.com/640/360/food/?89722",
"mobile": "https://lorempixel.com/640/360/food/?89722",
"square": "https://lorempixel.com/640/360/food/?89722"
},
"logs": {
"price": 2
}
},
{
"id": 40,
"name": "LightGray",
"image": {
"web": "https://lorempixel.com/640/360/food/?63930",
"mobile": "https://lorempixel.com/640/360/food/?63930",
"square": "https://lorempixel.com/640/360/food/?63930"
},
"logs": {
"price": 2
}
},
]
}
]
What I want to achieve:
[
{
"id": 1,
"name": "Carbo",
"menus": [
{
"id": 33,
"name": "FloralWhite",
"image": {
"web": "https://lorempixel.com/640/360/food/?89722",
"mobile": "https://lorempixel.com/640/360/food/?89722",
"square": "https://lorempixel.com/640/360/food/?89722"
},
"price": 2
},
{
"id": 40,
"name": "LightGray",
"image": {
"web": "https://lorempixel.com/640/360/food/?63930",
"mobile": "https://lorempixel.com/640/360/food/?63930",
"square": "https://lorempixel.com/640/360/food/?63930"
},
"price": 2
},
]
}
]
I've tried using laravel collection flatten with depth but it fail
$data = collect($data->menus);
$data = $data->flatten(1);
$data->values()->all();
How can I flatten the menus['logs'] object so it can one level with menu?
Something like this should do the trick. Simply iterate over your array, set the new property and remove the one you don't want.
$data = json_decode("[{
\"id\": 1,
\"name\": \"Carbo\",
\"menus\": [
{
\"id\": 33,
\"name\": \"FloralWhite\",
\"image\": {
\"web\": \"https://lorempixel.com/640/360/food/?89722\",
\"mobile\": \"https://lorempixel.com/640/360/food/?89722\",
\"square\": \"https://lorempixel.com/640/360/food/?89722\"
},
\"logs\": {
\"price\": 2
}
},
{
\"id\": 40,
\"name\": \"LightGray\",
\"image\": {
\"web\": \"https://lorempixel.com/640/360/food/?63930\",
\"mobile\": \"https://lorempixel.com/640/360/food/?63930\",
\"square\": \"https://lorempixel.com/640/360/food/?63930\"
},
\"logs\": {
\"price\": 2
}
}
]
}]");
foreach($data as $val){
foreach($val->menus as $menuVal){
$menuVal->price = $menuVal->logs->price;
unset($menuVal->logs);
}
}
#Stefen Suhat i really don't know what is the syntax for laravel but i did it with a simple php foreach() hope this will help you, as your array's depth is long so i had gone to all the levels of your array, check code and output snippet here https://eval.in/806879
try below one:
<?php
$array = array(
array(
"id"=> 1,
"name"=> "Carbo",
"menus"=> array(
array(
"id"=> 33,
"name"=> "FloralWhite",
"image"=> array(
"web"=> "https=>//lorempixel.com/640/360/food/?89722",
"mobile"=> "https=>//lorempixel.com/640/360/food/?89722",
"square"=> "https=>//lorempixel.com/640/360/food/?89722"
),
"logs"=> array(
"price"=> 2
)
),
array(
"id"=> 40,
"name"=> "LightGray",
"image"=> array(
"web"=> "https=>//lorempixel.com/640/360/food/?63930",
"mobile"=> "https=>//lorempixel.com/640/360/food/?63930",
"square"=> "https=>//lorempixel.com/640/360/food/?63930"
),
"logs"=> array(
"price"=> 2
)
),
)
)
);
foreach($array as $key => $value){
foreach ($value as $key1 => $value1) {
if(is_array($value1) && $key1 == "menus"){
foreach($value1 as $key2 => $value2) {
foreach ($value2 as $key3 => $value3) {
if(is_array($value3) && $key3 == "logs"){
unset($array[$key][$key1][$key2][$key3]);
$array[$key][$key1][$key2] = array_merge($array[$key][$key1][$key2], $value3);
}
}
}
}
}
}
echo "array after<br>";
echo "<pre>";
print_r($array); //your array after
?>
I am trying to convert following table data into nested array using PHP. I am almost done but stuck at one point.
id name parent
1 Apparel
2 Appliances
46 Apparel 1
47 Child Apparel 46
49 Child Apparel 2 47
Using this code
$cats = array(); // from database
$refs = array();
$rcats = array();
foreach ($cats as $cat) {
$thisref = &$refs[$cat['id']];
$thisref['id'] = $cat['id'];
$thisref['name'] = $cat['name'];
$thisref['leaf'] = "true";
if (!$cat['parent']) {
$rcats[$cat['id']] = &$thisref;
} else {
unset($refs[$cat['parent']]['leaf']);
$refs[$cat['parent']]['items'][$cat['id']] = &$thisref;
}
}
print_r(json_encode($rcats));
This results into following JSON.
{
"1": {
"id": "1",
"name": "Apparel",
"items": {
"46": {
"id": "46",
"name": "Apparel",
"items": {
"47": {
"id": "47",
"name": "Child Apparel",
"items": {
"49": {
"id": "49",
"name": "Child Apparel 2",
"leaf": "true"
}
}
}
}
}
}
},
"2": {
"id": "2",
"name": "Appliances",
"leaf": "true"
}
}
Where as I want the JSON like
[
{
"id": "1",
"name": "Apparel",
"items": [
{
"id": "46",
"name": "Apparel",
"items": [
{
"id": "47",
"name": "Child Apparel",
"items": [
{
"id": "49",
"name": "Child Apparel 2",
"leaf": "true"
}
]
}
]
}
]
},
{
"id": "2",
"name": "Appliances",
"leaf": "true"
}
]
Am i wrong? :p
$cats = array(); // From Database, but i use this
$cats = array(
array(
'id' => 1,
'name' => 'Jakarta',
'parent' => NULL
),
array(
'id' => 2,
'name' => 'Bandung',
'parent' => 1
),
array(
'id' => 3,
'name' => 'Surabaya',
'parent' => 1
),
array(
'id' => 4,
'name' => 'Bali',
'parent' => NULL
),
array(
'id' => 5,
'name' => 'Batam',
'parent' => NULL
),
);
$refs = array();
$rcats = array();
foreach ($cats as $cat) {
$thisref = &$refs[$cat['id']];
$thisref['id'] = $cat['id'];
$thisref['name'] = $cat['name'];
$thisref['leaf'] = "true";
if (!$cat['parent']) {
$rcats[] = &$thisref;
}
else {
unset($refs[$cat['parent']]['leaf']);
$refs[$cat['parent']]['items'][] = &$thisref;
}
}
/*
// IF YOU NEED CHANGE TO OBJECT
$rcats = (object)$rcats;
if(isset($rcats->items)){
$rcats->items = (object)$rcats->items;
}
*/
header('Content-type: application/json');
print_r(json_encode($rcats));
Here I'm assuming you are storing your final results in $refs
print_r(json_encode(array_values($rcats)));
If you want to take out all the indexes, in your for loop, do [] instead of the id:
if (!$cat['parent']) {
$rcats[] = &$thisref;
} else {
unset($refs[$cat['parent']]['leaf']);
$refs[$cat['parent']]['items'][] = &$thisref;
}
A quick example I throw:
<?php
$a = array('1' => array('a' => 3), '2' => array('b' => 4));
echo json_encode($a) . "\n";
echo json_encode(array_values($a)) . "\n";
Outputs:
{"1":{"a":3},"2":{"b":4}}
[{"a":3},{"b":4}]