here's my array
Array
(
[0] => Array
(
[0] => Jan 2010
[1] => 65.75
)
[1] => Array
(
[0] => Jan 2010
[1] => 211.05
)
[2] => Array
(
[0] => Jan 2010
[1] => 582.7
)
[3] => Array
(
[0] => Feb 2010
[1] => 136.3
)
[4] => Array
(
[0] => Feb 2010
[1] => 215.32
)
[5] => Array
(
[0] => Feb 2010
[1] => 413.9
)
[6] => Array
(
[0] => Mar 2010
[1] => 156.35
)
[7] => Array
(
[0] => Mar 2010
[1] => 210.54
)
[8] => Array
(
[0] => Mar 2010
[1] => 585.15
)
[9] => Array
(
[0] => Apr 2010
[1] => 126.1
)
[10] => Array
(
[0] => Apr 2010
[1] => 255.47
)
[11] => Array
(
[0] => Apr 2010
[1] => 329.1
)
[12] => Array
(
[0] => May 2010
[1] => 109
)
[13] => Array
(
[0] => May 2010
[1] => 170
)
[14] => Array
(
[0] => May 2010
[1] => 716.7
)
)
is there any way I can make all arrays with the same value in [0] merge? i want it to be something like this:
Array[0] = (Jan 2010, 65.75, 211.05, 582.7)
Array[1] = (Feb 2010, 136.3, 215.32, 413.9)
and so on...
The closest I can think to do this "simply" (read: taking advantage of native PHP features) is switching to text keys (associative array) for your result. This makes sense from a data modeling perspective as well, since in your sample result arrays you are mixing "key" data and "value" data (e.g. the first value carries the responsibility of being the label for the set == bad). The trick is to use the implicit "push" operator [], which appends a new value to an array.
foreach($sourceArray as $currentSubArray) {
$resultArray[$currentSubArray[0]][] = $currentSubArray[1];
}
Your result will look like this:
Array (
'Jan 2010' => Array (
0 => 65.75,
1 => 211.05,
2 => 582.7,
)
...
)
This is a variant of what #ctrahey suggested, that operates on the input array directly:
foreach($array as $key => &$entry) {
list($month, $value) = $entry;
if (isset($ptr[$month])) {
$ptr[$month][] = $value;
unset($array[$key]);
} else {
$ptr[$month] = &$entry;
}
}
unset($ptr);
Output with your example data:
Array
(
[0] => Array
(
[0] => Jan 2010
[1] => 65.75
[2] => 211.05
[3] => 582.7
)
[3] => Array
(
[0] => Feb 2010
[1] => 136.3
[2] => 215.32
[3] => 413.9
)
[6] => Array
(
[0] => Mar 2010
[1] => 156.35
[2] => 210.54
[3] => 585.15
)
[9] => Array
(
[0] => Apr 2010
[1] => 126.1
[2] => 255.47
[3] => 329.1
)
[12] => Array
(
[0] => May 2010
[1] => 109
[2] => 170
[3] => 716.7
)
)
actionMerge($inputArray);
function actionMerge($inputArray){
$month = array();
$earn = array();
$parentKey = 0;
$callback = function ($value, $key) use (&$month, &$earn, &$parentKey) {
if(!is_array($value)){
if($key == 0){
if(!in_array($value, $month)){
array_push($month, $value);
$earn[$value] = array();
}
$parentKey = $value;
}elseif($key == 1){
array_push($earn[$parentKey], $value);
}
}
};
array_walk_recursive($inputArray, $callback);
echo 'You should use this array';
var_dump($earn); // group money by month, I recommend you to use it
$Array = array();
foreach($month as $m){
$arr = array($m);
$arr = array_merge($arr, $earn[$m]);
array_push($Array, $arr);
}
echo '...Intead of array result what you expect';
var_dump($Array); // your expect result
}
?>
Here is result:
//You should use this array
array (size=5)
'Jan 2010' =>
array (size=3)
0 => float 65.75
1 => float 211.05
2 => float 582.7
'Feb 2010' =>
array (size=3)
0 => float 136.3
1 => float 215.32
2 => float 413.9
'Mar 2010' =>
array (size=3)
0 => float 156.35
1 => float 210.54
2 => float 585.15
'Apr 2010' =>
array (size=3)
0 => float 126.1
1 => float 255.47
2 => float 329.1
'May 2010' =>
array (size=3)
0 => int 109
1 => int 170
2 => float 716.7
//...Intead of array result what you expect
array (size=5)
0 =>
array (size=4)
0 => string 'Jan 2010' (length=8)
1 => float 65.75
2 => float 211.05
3 => float 582.7
1 =>
array (size=4)
0 => string 'Feb 2010' (length=8)
1 => float 136.3
2 => float 215.32
3 => float 413.9
2 =>
array (size=4)
0 => string 'Mar 2010' (length=8)
1 => float 156.35
2 => float 210.54
3 => float 585.15
3 =>
array (size=4)
0 => string 'Apr 2010' (length=8)
1 => float 126.1
2 => float 255.47
3 => float 329.1
4 =>
array (size=4)
0 => string 'May 2010' (length=8)
1 => int 109
2 => int 170
3 => float 716.7
It's quite horrible,but it works...
$resarray=Array(
0 => Array(0 => 'Jan 2010',1 => 65.75),
1=> Array(0 => 'Jan 2010',1 => 211.05),
2 => Array(0 => 'Jan 2010',1 => 582.7),
3 => Array(0 => 'Feb 2010',1 => 136.3),
4 => Array(0 => 'Feb 2010',1 => 215.32),
5 => Array(0 => 'Feb 2010',1 => 413.9),
6 => Array(0 => 'Feb 2010',1 => 156.35),
7 => Array(0 => 'Feb 2010',1 => 210.54),
8 => Array(0 => 'Mar 2010',1 => 585.15),
9 => Array(0 => 'Apr 2010',1 => 126.1),
10 => Array(0 => 'Apr 2010',1 => 255.47),
11 => Array(0 => 'Apr 2010',1 => 329.1),
12 => Array(0 => 'May 2010',1 => 109),
13 => Array(0 => 'May 2010',1 => 170),
14 => Array(0 => 'May 2010',1 => 716.7)
);
$CatArray=array();
$FinArray=array();
$count=count($resarray);
for($i=0;$i<$count;$i++){
$index=array_search($resarray[$i][0],$CatArray);
if(is_numeric($index) && $index>=0)
$FinArray[$index][]=$resarray[$i][1];
else{
$CatArray[]=$resarray[$i][0];
$FinArray[]=array($resarray[$i][0],$resarray[$i][1]);
}
}
unset($CatArray);
unset($resarray);
$count=count($FinArray);
for($i=0;$i<$count;$i++){
$resarray[$i]=implode(',',$FinArray[$i]);
}
Output:
Array
(
[0] => Jan 2010,65.75,211.05,582.7
[1] => Feb 2010,136.3,215.32,413.9,156.35,210.54
[2] => Mar 2010,585.15
[3] => Apr 2010,126.1,255.47,329.1
[4] => May 2010,109,170,716.7
)
Related
I am making API request to Google Sheets and I receive this response:
Array
(
[0] => Array
(
[Title] => Hours
[January] => 1
[February] => 2
[March] => 3
[April] => 4
[May] => 5
[June] => 6
[July] => 7
[August] => 8
)
[1] => Array
(
[Title] => Days
[January] => 3
[February] => 5
[March] => 1
[April] => 6
[May] => 3
[June] => 7
[July] => 4
[August] => 2
)
[2] => Array
(
[Title] => Weeks
[January] => 3
[February] => 5
[March] => 3
[April] => 4
[May] => 0
[June] => 0
[July] => 2
[August] => 6
[September] => 0
[October] => 0
[November] => 1
[December] => 0
)
)
How could I loop trough and modify this array to something like this so I can use it with HighCharts JS library?
series: [{
title: 'Hours',
data: [1, 2, 3, 4, 5, 6 .......]
}, {
title: 'Days',
data: [4, 6, 3, 6, ........]
}, {
title: 'Weeks',
data: [1, 9, 1, 3, ........]
}, {
....
}]
I tried this way:
if ($response->status) {
$rawData = json_decode(json_encode($response->data), true);
}
$series = [];
foreach ($rawData as $index => $rawDatum) {
if (!isset($rawDatum['Title'])) {
continue;
}
foreach ($rawDatum as $columnKey => $value) {
if ($columnKey == 'CvA') {
$series[$columnKey]['Title'][] = $value;
}
}
}
What I got as result:
Array
(
[Title] => Array
(
[title] => Array
(
[0] => Hours
[1] => Days
[2] => Weeks
)
)
)
Also is there a way to get all names of the months saved in $months array for example without doubles?
The following piece of code will create an array with title and the values for each respective subarray Hours, Days, Weeks.
We will also collect the union of the months in an array named $months.
$series = [];
$months = [];
foreach ($rawData as $subarray) {
$title = $subarray['Title'];
// Remove title key
unset($subarray['Title']);
$series[] = [
'title' => $title,
'data' => array_values($subarray),
];
// Union operator to keep unique months.
$months += array_keys($subarray);
}
echo '<pre>';
print_r($months);
echo '</pre>';
echo '<pre>';
print_r($result);
echo '</pre>';
Result $months:
Array
(
[0] => January
[1] => February
[2] => March
[3] => April
[4] => May
[5] => June
[6] => July
[7] => August
[8] => September
[9] => October
[10] => November
[11] => December
)
Result $series:
Array
(
[0] => Array
(
[title] => Hours
[data] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
)
[1] => Array
(
[title] => Days
[data] => Array
(
[0] => 3
[1] => 5
[2] => 1
[3] => 6
[4] => 3
[5] => 7
[6] => 4
[7] => 2
)
)
[2] => Array
(
[title] => Weeks
[data] => Array
(
[0] => 3
[1] => 5
[2] => 3
[3] => 4
[4] => 0
[5] => 0
[6] => 2
[7] => 6
[8] => 0
[9] => 0
[10] => 1
[11] => 0
)
)
)
Given this data:
$data = [
[
'Title' => 'Hours',
'January' => 1,
'February' => 2,
'March' => 3,
'April' => 4,
'May' => 5,
'June' => 6,
'July' => 7,
'August' => 8,
],
[
'Title' => 'Days',
'January' => 3,
'February' => 5,
'March' => 1,
'April' => 6,
'May' => 3,
'June' => 7,
'July' => 4,
'August' => 2,
],
[
'Title' => 'Weeks',
'January' => 3,
'February' => 5,
'March' => 3,
'April' => 4,
'May' => 0,
'June' => 0,
'July' => 2,
'August' => 6,
'September' => 0,
'October' => 0,
'November' => 1,
'December' => 0,
]
];
You should be able to do this:
// Store everything here
$result = [];
foreach ($data as $item) {
// Grab the title
$ret['title'] = $item['Title'];
// Remove it from the source dataset
unset($item['Title']);
// Take the remaining values, join with comma
$ret['data'] = implode(',', array_values($item));
// Append to main array
$result[] = $ret;
}
You can skip the implode if want an actual array
How can i add array_sum to the string in my loop without making another foreach loop for it? I am trying to combine all of the numbers together instead of having this multi dimensional array and then just have the value and i see that array_sum wont add them up because its inside of an array. any ideas?
$hours_arr = array();
foreach($proj_time as $item){
$hours_arr [$item['project_id']]['item_value'] = $item['item_value'];
$hours_arr [$item['project_id']]['hours'][] = $item['hours'];
}
//output
array (size=3)
4 =>
array (size=2)
'item_value' => string 'Coaching' (length=8)
'hours' =>
array (size=1)
0 => string '999.99' (length=6)
1487 =>
array (size=2)
'item_value' => string 'Standby' (length=7)
'hours' =>
array (size=1)
0 => string '15.00' (length=5)
1488 =>
array (size=2)
'item_value' => string 'Standby' (length=7)
'hours' =>
array (size=4)
0 => string '10.00' (length=5)
1 => string '10.00' (length=5)
2 => string '10.00' (length=5)
3 => string '10.00' (length=5)
I would like my output to be
1488 =>
array (size=2)
'item_value' => string 'Standby' (length=7)
'hours' => string '40.00' (length=5)
edit: added contents of $proj_time
Array
(
[0] => Array
(
[project_id] => 4
[consultant_id] => 51
[engagement_id] => 8
[hours] => 999.99
[item_value] => Coaching
)
[1] => Array
(
[project_id] => 1487
[consultant_id] => 1
[engagement_id] => 1
[hours] => 15.00
[item_value] => Standby
)
[2] => Array
(
[project_id] => 1488
[consultant_id] => 31
[engagement_id] => 7
[hours] => 10.00
[item_value] => Design App RFP
)
[3] => Array
(
[project_id] => 1488
[consultant_id] => 32
[engagement_id] => 41
[hours] => 10.00
[item_value] => Training
)
[4] => Array
(
[project_id] => 1488
[consultant_id] => 55
[engagement_id] => 41
[hours] => 10.00
[item_value] => Training
)
[5] => Array
(
[project_id] => 1488
[consultant_id] => 1
[engagement_id] => 1
[hours] => 10.00
[item_value] => Standby
)
)
Instead of creating array and then applying operation, while creating itself why don't you sum up like this:
DEMO
$hours_arr = array();
foreach($proj_time as $item){
$hours_arr [$item['project_id']]['item_value'] = $item['item_value'];
if(array_key_exists('hours', $hours_arr [$item['project_id']]))
$hours_arr [$item['project_id']]['hours'] += $item['hours'];
else
$hours_arr [$item['project_id']]['hours'] = $item['hours'];
}
Result:
Array
(
[4] => Array
(
[item_value] => Coaching
[hours] => 999.99
)
[1487] => Array
(
[item_value] => Standby
[hours] => 15
)
[1488] => Array
(
[item_value] => Standby
[hours] => 40
)
)
Try this out
<?php
$hours_arr = array();
foreach($proj_time as $item){
if(!isset($hours_arr [$item['project_id']]) || $hours_arr [$item['project_id']]['item_value'] != $item['item_value']) {
$hours_arr [$item['project_id']]['item_value'] = $item['item_value'];
$hours_arr [$item['project_id']]['hours'][] = $item['hours'];
} else {
$hours_arr [$item['project_id']]['hours'][0] += $item['hours'];
}
}
Hello stackoverflow community. I need help with arrays. It's my weakness. I've got this kind of array:
Array
(
[0] => Array
(
[id] => 7
[slot] => 1
[name] => Apple
[start_date] => 12/16/2015
[end_date] => 03/10/2016
[status] => 1
[pre_exp_email] => 0
)
[1] => Array
(
[id] => 8
[slot] => 1
[name] => Cherry
[start_date] => 12/29/2015
[end_date] => 03/20/2016
[status] => 1
[pre_exp_email] => 0
)
[2] => Array
(
[id] => 5
[slot] => 3
[name] => Bananna
[start_date] => 11/30/2015
[end_date] => 00/00/0000
[status] => 1
[pre_exp_email] => 0
)
[3] => Array
(
[id] => 1
[slot] => 4
[name] => Kiwi
[start_date] => 11/21/2015
[end_date] => 12/21/2016
[status] => 1
[pre_exp_email] => 0
)
)
And my job is to randomize elements which has same [slot], but leave order ascending. For example now it is:
1 Apple 1 Cherry 3 Bannana 4 Kiwi
I need to randomize those elements who has same slot number. So Apple and Cherry would swap positions. How can I do this stuff?
Update : Using shuffle & usort :
shuffle($fruits);
function cmp($a, $b) {
if ($a['slot'] == $b['slot']) {
return 0;
}
return ($a['slot'] < $b['slot']) ? -1 : 1;
}
usort($fruits, "cmp");
Make a new array from the original having slot as keys
$elements = array(
0 => Array
(
'id' => 7,
'slot' => 1
),
1 => Array
(
'id' => 8,
'slot' => 1
),
2 => Array
(
'id' => 9,
'slot' => 1
),
3 => Array
(
'id' => 9,
'slot' => 5
)
);
foreach($elements as $element){
$newArray[$element['slot']][] = $element; //put every element having the same slot
}
$elementSlots = array_keys($newArray); // all slots are stored in elementSlots
$Result = array();
foreach($elementSlots as $slot) {
shuffle($newArray[$slot]); //randomize elements having the same slot
foreach($newArray[$slot] as $element) { //add them to the result array
$Result[$slot][] = $element;//For output 1
//$Result[] = $element; //For output 2
}
}
var_dump($Result);
Output 1:
array (size=2)
1 =>
array (size=3)
0 =>
array (size=2)
'id' => int 7
'slot' => int 1
1 =>
array (size=2)
'id' => int 9
'slot' => int 1
2 =>
array (size=2)
'id' => int 8
'slot' => int 1
5 =>
array (size=1)
0 =>
array (size=2)
'id' => int 9
'slot' => int 5
Output 2:
array (size=4)
0 =>
array (size=2)
'id' => int 7
'slot' => int 1
1 =>
array (size=2)
'id' => int 9
'slot' => int 1
2 =>
array (size=2)
'id' => int 8
'slot' => int 1
3 =>
array (size=2)
'id' => int 9
'slot' => int 5
I want which is the biggest array from following array.
[13] => Array
(
[0] => 1
[1] => 3
[2] => 9
)
[15] => Array
(
[0] => 1
[1] => 5
[2] => 8
)
[33] => Array
(
[0] => 1
[1] => 9
[2] => 13
)
I want a code that would return last array with key 33.
Please Help.
Use max to get the maximum from the keys of your array
$yourarray=array(13 => Array
(
0 => 1,
1 => 3,
2 => 9,
),
15 => Array
(
0 => 1,
1 => 5,
2 => 8,
),
33 => Array
(
0 => 1,
1 => 9,
2 => 13,
));
$arr=max(array_keys($yourarray));
print_r($yourarray[$arr]);
Output:
Array
(
[0] => 1
[1] => 9
[2] => 13
)
This should do the trick...
<?php
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$array_totals = array_map('array_sum', $tests);
krsort($array_totals);
$maxArray = each($array_totals);
var_dump($maxArray);
Gives
array (size=4)
1 => int 23
'value' => int 23
0 => int 33
'key' => int 33
Not the most beautiful thing, but readable ;)
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$max = -1;
$max_key = -1;
foreach ($tests as $k => $v) {
$cur_max = max($v);
if ($cur_max >= $max) {
$max = $cur_max;
$max_key = $k;
}
}
echo "max: $max; max_key: $max_key";
Gives:
max: 13; max_key: 33
To make it more beautiful: use array_map and sorting.
I have a multidimensional array that looks like this:
Array (
[0] => Array (
[date] => August
[mozrank] => 2
[domain_authority] => 41
[external_links] => 9
[unique_visitors] => 14
)
[1] => Array (
[date] => August
[post_count] => 70
[comment_count] => 53
[theme] => yes
[plugins] => 3
)
[2] => Array (
[date] => September
[mozrank] => 4
[domain_authority] => 42
[external_links] => 10
[unique_visitors] => 20
)
[3] => Array (
[date] => September
[post_count] => 71
[comment_count] => 56
[theme] => yes
[plugins] => 5
)
)
You'll notice that there are two arrays that have the same key/value pair of August and two arrays that have the same key/value pair of September. However in each case they have different keys associated with them. I'm trying to group each array on the date key where the value is the same and merge the other keys together. For example, the output would be:
Array (
[0] => Array (
[date] => August
[mozrank] => 2
[domain_authority] => 41
[external_links] => 9
[unique_visitors] => 14
[post_count] => 70
[comment_count] => 53
[theme] => yes
[plugins] => 3
)
[1] => Array (
[date] => September
[mozrank] => 4
[domain_authority] => 42
[external_links] => 10
[unique_visitors] => 20
[post_count] => 71
[comment_count] => 56
[theme] => yes
[plugins] => 5
)
)
Any ideas?
First thing that cross my mind:
$merged = array();
foreach ($array as $item)
{
$date = $item['date'];
if (!isset($merged[$date]))
{
$merged[$date] = array();
}
$merged[$date] = array_merge($merged[$date], $item);
}
As result there will be an array where key is a month. If you want standard index (begin from 0) you can always use shuffle().
Result:
array (size=2)
'August' =>
array (size=9)
'date' => string 'August' (length=6)
'mozrank' => int 2
'domain_authority' => int 41
'external_links' => int 9
'unique_visitors' => int 14
'post_count' => int 70
'comment_count' => int 53
'theme' => string 'yes' (length=3)
'plugins' => int 3
'September' =>
array (size=9)
'date' => string 'September' (length=9)
'mozrank' => int 4
'domain_authority' => int 42
'external_links' => int 10
'unique_visitors' => int 20
'post_count' => int 71
'comment_count' => int 56
'theme' => string 'yes' (length=3)
'plugins' => int 5
P.S. I have feeling that it can be done better than this...