How to manipulate php arrays [duplicate] - php

This question already has answers here:
Group rows in an associative array of associative arrays by column value and preserve the original first level keys
(7 answers)
Closed 7 years ago.
How to manipulate php arrays. I have a $data variable below.
$data = Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
[2] => Array
(
[id] => 3
[capacity] => 900
[category] => students
)
[3] => Array
(
[id] => 4
[capacity] => 300
[category] => students
)
)
Now I want to group the data with same category like below. . I am not really familiar in php. Can somebody out there help me how to achieve this. What function should I used. Thanks
Array
(
[users] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
),
[students] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => students
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => students
)
)
)

Just iterate over the array and add it depending on the content of the category field to a new array:
$new = array();
foreach ($data as $val) {
$new[$val['category']][] = $val;
}

This does what you requested
<?php
$data = array(
0 => array (
"id" => 1,
"capacity" => 900,
"category" => "users"
),
1 => array (
"id" => 2,
"capacity" => 300,
"category" => "users"
),
2 => array (
"id" => 3,
"capacity" => 900,
"category" => "students"
),
3 => array (
"id" => 4,
"capacity" => 300,
"category" => "students"
)
);
$groups = array();
foreach ($data as $i) {
if ($i['category'] === "students") {
$groups['students'][] = $i;
}
else if ($i['category'] === "users") {
$groups['users'][] = $i;
}
}
echo "<pre>", print_r($groups), "</pre>";
Here is a working demo - http://codepad.viper-7.com/G4Br28

Related

get the sum value of specific key in given array and add row of count after specific completed index

Actual I get
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
[1] => Array
(
[name] => person2
[value] => 5
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person4
[value] => 10
)
)
Actually i need
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
//here i want new row index 1 array
[1] => Array
(
[name] => total
[value] => 11 //this value is the sum of index 1
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person2
[value] => 5
)
// here i want add new line index 4 array
[4] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 2 and 3
)
[5] => Array
(
[name] => person4
[value] => 10
)
// here i want add new line index 6 array
[6] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 5
)
)
may this help you i have tried some step making grouping but sum is not correct but code may help you
$a = Array
(
0 => Array
(
'name' => 'person1',
'value' => 11,
),
1 => Array
(
'name' => 'person2',
'value' => 5,
),
2 => Array
(
'name' => 'person2',
'value' => 5,
),
3 => Array
(
'name' => 'person4',
'value' => 10,
)
);
foreach($a as $c){
$d[]=$c['name'];
$e[]=$c['value'];
}
//print_r($d);
$group = array();
foreach($d as $key=>$val){
$group[$val][] = $e[$key];
}
// this loop for check the max number and count total price
$data = array();
$total = 0;
foreach($group as $key=>$val){
for($i=0;$i<count($val);$i++){
$data[]['name'] = $key;
$data[]['value'] = $val[$i];
$suma[] = $val[$i];
}
$sunmb =array_sum($suma);
$data[]['name'] = 'total';
$data[]['value'] = $sunmb;
}
print_r($data);
You can check code run here https://wtools.io/php-sandbox/b1LD

How To Merge/push array into one in php

code
$result = [];
foreach ($getAllAgent as $rkey => $rvalue) {
$found = -1;
for ($i = 0; $i < count($result); $i++) {
if ($result[$i]["brokerid"] == $rvalue["brokerid"]) {
$found = $i;
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details']; //here to combine
break;
}
}
// if not found, create new
if ($found == -1) {
$result[] = $rvalue;
}
results
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
[0] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
)
)
I have one set of an array, and I loop it and restructure match the data based on ID. After that I will try to merge the same data into one array, I able add into one array. But it will not combine as one. The correct result should be as below.
[get_agent_details] => Array
(
[0] => Array(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
),
[1] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
Your problem is in this line:
$result[] = $rvalue;
Consider the case where you only have one item; this will result in:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
But to be consistent, you need get_agent_details to be a list of items, that happens to have one entry:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[0] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
)
So you need to re-arrange your data, for instance by writing:
$rvalue['get_agent_details'] = [0 => $rvalue['get_agent_details']];
$result[] = $rvalue;
Then, since get_agent_details will already be a list when you encounter a second matching item, your existing code in the inner loop will do the right thing:
// Add an item to the list
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details'];

merge two array values

How can I make this array:
Array
(
[0] => Array
(
[id] => 3412341233214
[number] => 21000
)
[1] => Array
(
[id] => 12121212121212
[number] => 18000
)
[2] => Array
(
[id] => 12121212121212
[number] => 17000
)
)
Look like the one below, where [1] and [2] have been merged into a single array based on having the same id and the number has been added together.
Array
(
[0] => Array
(
[id] => 3412341233214
[number] => 21000
)
[1] => Array
(
[id] => 12121212121212
[number] => 35000
)
)
Create a $tmp array the store the number property in it.
Only store it if the id doesn't exist yet using:
if(!isset($tmp[$obj['id']]))
To make each value unique
After that, count the $tmp number with the $array number.
$tmp[$obj['id']]['number'] += $obj['number'];
https://3v4l.org/UMJ4p
$array = array(
0 => array(
"id" => 3412341233214,
"number" => 21000
),
1 => array(
"id" => 12121212121212,
"number" => 18000
),
2 => array(
"id" => 12121212121212,
"number" => 17000
)
);
$tmp = Array();
foreach($array as $obj) {
if(!isset($tmp[$obj['id']])) {
$tmp[$obj['id']] = array_merge(Array('number'=>1),$obj);
continue;
}
$tmp[$obj['id']]['number'] += $obj['number'];
}
print_r(array_values($tmp));
The following code results into
(
[0] => Array
(
[number] => 21000
[id] => 3412341233214
)
[1] => Array
(
[number] => 35000
[id] => 12121212121212
)
)

How to check is php array have different value of key

I have this array
Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
Now here how to check if "job_id" is different. eg all have same job_id or not and how many different job_id array have.
Thanks
Try this using array function:
$data = Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
$totalDifferentJobId = count(array_unique(array_column($data, 'job_id')));
It will give you total unique job
Well, if it is not a too large array, you can do the following:
<?php
$arr = [
['id' => 1, 'job_id' => 100],
['id' => 2, 'job_id' => 100],
['id' => 3, 'job_id' => 101]
];
$jobids = array_unique(array_column($arr, 'job_id'));
var_dump($jobids);
This looks like a db-result, in wich case it would be better to use a group-by statement?

PHP Grouping Array by Index [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have a multidimensional array:
Array
(
[type] => Array
(
[0] => text
[1] => portfolio
[2] => slide
[3] => text
)
[grid] => Array
(
[0] => 3
[1] => 5
[2] => 3
[3] => 4
)
[title] => Array
(
[0] => title1
[3] => title2
)
[content] => Array
(
[0] => content1
[3] => content2
)
[item] => Array
(
[1] => 6
[2] => 7
)
[pagination] => Array
(
[1] => 8
)
[order] => Array
(
[1] => desc
[2] => asc
)
)
And want to group it by [type] key given in the array:
Array (
[0] => Array (
[type] => text
[grid] => 3
[title] => title1
[content] => content1
)
[1] => Array (
[type] => portfolio
[grid] => 5
[item] => 6
[pagination] => 1
[order] => desc
)
[2] => Array (
[type] => slide
[grid] => 3
[item] => 7
[order] => asc
)
[3] => Array (
[type] => text
[grid] => 4
[title] => title2
[content] => content2
)
Is there a way or PHP function to do array grouping like that?
This snippet achieves that:
$result = array();
foreach ($array as $key => $data) {
foreach ($data as $offset => $value) {
if (isset($result[$offset])) {
$result[$offset][$key] = $value;
} else {
$result[$offset] = array($key => $value);
}
}
}
Working DEMO
array_map() with null for the callback will do exactly what you want. However it will have number for the index instead of names.
If you write your own callback then you can return an array with the names you need.
Since apparently people want the actual code:
array_map(null, $type_array, $grid_array, $title_array, $content_array, $item_array);
It really is as simple as that. Most of the other answers are so large and unnecessary.
Note: This assumes a fixed number of arrays - if it's not fixed then this won't work, and then go with Florent's answer.
You can do it with this function:
$type_array = array('text', 'portfolio', 'slide', 'text');
$grid_array = array(3, 5, 3, 4);
$title_array = array(0 => 'title1', 3 => 'title2');
$content_array = array(0 => 'content1', 3 => 'content2');
$item_array = array(1 => 6, 2 => 7);
function group_arrays($type_array, $grid_array, $title_array, $content_array, $item_array) {
$temp_array = array();
for($i = 0; $i < count($type_array); $i++) {
$temp_array[$i] = array( 'type' => #$type_array[$i],
'grid' => #$grid_array[$i],
'title' => #$title_array[$i],
'content' => #$content_array[$i],
'item' => #$item_array[$i]);
}
return $temp_array;
}
print_r(group_arrays($type_array, $grid_array, $title_array, $content_array, $item_array));
Hope this helps!

Categories