Combining arrays with common values - php

I have an array of dates that looks like this:
Array
(
[0] => '2014-01-01'
[1] => '2014-01-02'
[2] => '2014-01-03'
[3] => '2014-01-04'
[4] => '2014-01-05'
[5] => '2014-01-06'
[6] => '2014-01-07'
)
and I have another array of dates and counts that looks like this:
[All] => Array
(
[0] => Array
(
[count] => 2
[date] => 2014-01-06
)
[1] => Array
(
[count] => 1
[date] => 2014-01-03
)
[2] => Array
(
[count] => 43
[date] => 2013-12-11
)
[3] => Array
(
[count] => 103
[date] => 2013-12-10
)
[4] => Array
(
[count] => 128
[date] => 2013-12-09
)
[5] => Array
(
[count] => 75
[date] => 2013-12-08
)
[6] => Array
(
[count] => 107
[date] => 2013-12-07
)
I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".
So for instance, the new array would look like this:
Array
(
[2014-01-01] => 0
[2014-01-02] => 0
[2014-01-03] => 1
[2014-01-04] => 0
[2014-01-05] => 0
[2014-01-06] => 2
[2014-01-07] => 0
)
Does that make sense? Please feel free to ask any questions you may have. Thank you!

Try this code:
$result = array();
foreach($firstArray as $f){
foreach($secondArray as $s){
if($s['date'] == $f) $result[$f] = $s['count'];
}
if(!array_key_exists($f, $result)) $result[$f] = 0;
}

$result = array();
foreach($secondArray as $s){
if(in_array($s['date'], $firstArray) {
unset($firstArray[$s['date']]);
$result[$s['date']] = $s['count'];
}
}
// if items left in first array that are not found within foreach:
if (!empty($firstArray))
$result = array_merge($result, array_fill_keys($firstArray, 0));
// sort by key so dates go ascending
ksort($result);

$new = array();
foreach($all as $row)
{
$new[$row['date']] = $row['count'];
}
array_merge ($new, $old);
Here $all is the array with the date and count indices.
$old is the exisisting array.

That's a 2-liner:
// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});

Related

How to add values to an array inside an array obtained through AJAX

In my PHP file, I'm receiving a total of 4 variables $data, $date, $shift and $val1.
$data is an array and the other 3 are date and 2 strings obtained through AJAX with no problem.
What I'm trying to do is to insert these 3 values inside my $data variable.
I tried using array merge, and a For each loop with multiple instances but no luck so far.
I obtained my variables like this:
if (isset($_POST['date'])){
$date = $_POST['date'];
$date = json_encode($date);
$date = json_decode($date);
}
if (isset($_POST['shift'])){
$shift = $_POST['shift'];
$shift = json_encode($shift);
$shift = json_decode($shift);
}
if (isset($_POST['val1'])){
$val1 = $_POST['val1'];
$val1 = json_encode($val1);
$val1 = json_decode($val1);
}
if (isset($_POST['data'])){
$dat = $_POST['data'];
$data = json_decode($dat, true);
}
$values = array($date,$shift,$val1);
$r = (array_merge($data, $values));
My data array looks something like this:
Array (
[0] => Array (
[data] => Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => Mat1
[5] => Box1
[6] => 100
[7] => 100
[8] => Piece1
[9] => Loc1
[10] => Mach1
[11] => 1000
[12] => Accepted
)
)
)
[1] => 2019-04-09
[2] => First
[3] => Value1
)
But what I want to achieve is this:
Array (
[0] => Array (
[data] => Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => Mat1
[5] => Box1
[6] => 100
[7] => 100
[8] => Piece 1
[9] => Suc1
[10] => Mach1
[11] => 1000
[12] => Accepted
[13] => 2019-04-09
[14] => First
[15] => Value1
)
)
)
)
What am I doing wrong? Or How can I achieve what I'm trying to do?
Edit: Since I can get more than one array at my array, something like this
Array (
[0] => Array (
[data] => Array (
[0] => Array (...)
[1] => Array (...)
[2] => Array (...)
[3] => Array (...)
)
)
)
I just added this code to #HelgeB answer, I'm leaving it here in case someone might need it in the future.
$count = count($data[0]['data']);
for ($i=0; $i < $count ; $i++) {
$data[0]['data'][$i][] = $date;
$data[0]['data'][$i][] = $shift;
$data[0]['data'][$i][] = $val1;
}
As far as I can see from your merged output, your $data array structure is $data[0]['data'][0] = [1,2,3,...,'Accepted'].
So in my opinion you need to insert the values exactly on the level $data[0]['data'][0] to obtain your result.
The simplest way to achieve this would be:
$data[0]['data'][0][] = $date;
$data[0]['data'][0][] = $shift;
$data[0]['data'][0][] = $val1;
If you want to use your merge approach you need to merge on the correct level like this:
$r = [0 => ['data' => [0 => (array_merge($data[0]['data'][0], $values))]]];

Different Arrays convert into multi-dimension array

I want to combine multiple arrays output in single array. Below is the array I am getting when I do this.
print_r($getData_milestone);
I have arrays as below:
[milestone] => Array
(
[0] => milestone 1
[1] => milestone 2
[2] => milestone 3
)
[date] => Array
(
[0] => 10/25/2015
[1] => 10/30/2015
[2] => 11/25/2015
)
[status] => Array
(
[0] => 1
[1] => 1
[2] => 0
)
And I want to get output such as below:
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
I have tried by this code
foreach($getData_milestone['milestone'] as $miledata)
{
$allDatamile[$i]=$getData_milestone;
$allDatamile[$i]=$getData_milestone['date'];
$allDatamile[$i]=$getData_milestone['status'];
$i++;
}
Try this out, and let me know the result. It should work.
I am considering the given array as an associative array with keys "milestone", "date" and "status" .. Correct me if I'm wrong.
$outputArray = array();
foreach($givenArray['milestone'] as $key=>$val){
$outputArray[$key]['milestone'] = $val;
$outputArray[$key]['date'] = $givenArray['date'][$key];
$outputArray[$key]['status'] = $givenArray['status'][$key];
}
print_r($outputArray)
try this,
$a["milestone"][] = "milestone 1";
$a["milestone"][] = "milestone 2";
$a["milestone"][] = "milestone 3";
$a["date"][] = "10/25/2015";
$a["date"][] = "10/30/2015";
$a["date"][] = "11/25/2015";
$a["status"][] = "1";
$a["status"][] = "1";
$a["status"][] = "0";
foreach ($a['milestone'] as $key => $val) {
$a1[$key]["milestone"] = $val;
$a1[$key]["date"] = $a['date'][$key];
$a1[$key]["status"] = $a['status'][$key];
}
output is
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
array_column (PHP 5 >= 5.5.0) might help -
$keys = array_keys($arr);
// if the number of element increases(to make it more dynamic)
$count = count($arr['milestone']);
$i= 0;
while($i < $count) {
$new[] = array_column($arr, $i);
$i++;
}
foreach($new as $k => $n) {
$new[$k] = array_combine($keys, $n);
}
var_dump($new);
DEMO
Try it out the bellow code
$out= array();
$milestone=array
(
"milestone 1",
"milestone 2",
"milestone 3"
);
$m_date=array
(
"10/25/2015",
"10/25/2015",
"10/25/2015"
);
$status=array
(
0,1,1
);
for($i=0;$i<count($milestone);$i++){
$comArray=array
(
"milestone" => $milestone[$i],
"date" => $m_date[$i],
"status" => $status[$i]
)
$out[]=$comArray;
}
Hope it will solve your problem.

ReCreate Readable Array

I have an array look like this via CSV:
Array
(
[0] => Array
(
[0] => Agent Name
[1] => Total Calls
[2] => Customer Services
[3] => Voicemail
)
[1] => Array
(
[0] => Paul
[1] => 53
[2] => 0
[3] => 0
)
[2] => Array
(
[0] => Sarah
[1] => 51
[2] => 0
[3] => 0
)
)
I would like to tidy an array to make it to more readable and easy to use.
I want an array to look something like this:
Array
(
[Paul] => Array
(
[Agent Name] => Paul
[Total Calls] => 53
[Customer Services ] => 30
[Voicemail ] => 0
)
[Sarah] => Array
(
[Agent Name] => Sarah
[Total Calls] => 51
[Customer Services ] => 0
[Voicemail ] => 0
)
)
I have came up with this solution and it work:
$fields = $report[0];
array_shift($report);
$data = array();
foreach($report as $row) {
$data[$row[0]] = array();
foreach($row as $k => $val) {
$data[$row[0]][$fields[$k]] = $val;
}
}
return $data;
Is there a better shorter way doing this?
Try
$result =array();
for($i=1;$i<count($arr);$i++){
$result[$arr[$i][0]] = array_combine(array_values($arr[0]),$arr[$i]);
}
See demo here
You could perhaps do it like this:
$f = array_shift($report);
$a = array_combine(array_column($t = array_map(function ($r) use ($f)
{
return array_combine($f, $r);
}
, $report), 'Agent Name'), $t);

PHP combine array based on value of particular element

I have an array that looks like this
Array (
[0] => Array
(
[id] => 123
[count] => 1
)
[1] => Array
(
[id] => 123
[count] => 3
)
[2] => Array
(
[id] => 513
[count] => 0
)
[3] => Array
(
[id] => 561
[count] => 1
)
[4] => Array
(
[id] => 613
[count] => 7
)
)
What I want to do is create a new array, that totals the count where the id values are the same. So for example, the new array would look like this:
Array (
[0] => Array
(
[id] => 123
[count] => 4
)
[1] => Array
(
[id] => 513
[count] => 0
)
[2] => Array
(
[id] => 561
[count] => 1
)
[3] => Array
(
[id] => 613
[count] => 7
)
)
Would anyone know a good method to do this?
Thank you!
Short and simple:
$new_array = array();
foreach($data_array as $value) {
if(array_key_exists($value['id'], $new_array)) {
$new_array[$value['id']] += $value['count'];
} else {
$new_array[$value['id']] = $value['count'];
}
}
echo print_r($new_array,true);
I made it id => value since there didn't seem to need another array for data when the id could be used as the array's key.
Any reason why you are not making an associative array of id => count
You can do it with the following function (I didn't tested the code, but it should work ):
function get_count_array($arr) {
$new_array = array();
foreach($arr as $item) {
$found = false;
//loop through all the current new items to check if we already have it
for($i = 0; $i < count($new_array); $i++) {
//do we have it?
if($new_array[$i]['id'] == $item['id']) {
$new_array[$i]['count'] += 1;
$found = true;
break;
}
}
if(!$found) {
$new_array[] = array('id' => $item['id'], 'count' => 0);
}
}
return $new_array;
}

how to push data into array in php

Dear All,
I want to push the data into array. i am using flowing code. There are two arrays. one holding keys and 2nd values. i am using flowing code
while($data=mysql_fetch_array($result))
{
foreach ($arrTemp as $val)
{
array_push($arrKeys, $val);
array_push($arrValues, $data[$val]);
}
}
print_r($arrKeys);
print_r($arrValues);
$arrReturn = array_combine($arrKeys,$arrValues);
...................................
and get flowing results of two arrays.
Array ( [0] => due_date [1] => flag_code [2] => due_date [3] => flag_code [4] => due_date [6] => flag_code )
Array ( [0] => 12:04:2011 [1] => 0 [2] => 13:04:2011 [3] => 0 [4] => 14:04:2011 [6] => 0 )
when i try to combined the array using array_combined function it only return an array of two values like: Array (due_date => 14:04:2011 flag => 0)
how i can get all values in single array.....!
Its because your have multiple the same array keys. So first it inserts due_date, then flag_code, then it will try and insert another due_date but since this already exists in the array it will overwrite it. Thus the only values left in the array will be the last pair.
The solution is to not have multiple keys that are the same in one array (your due_date and flag_code)
You could do:
foreach ($arrTemp as $val) {
$arrReturn[] = array($val => $data[$val];
}
This will give you each set of results keyed in an array like so:
$arrReturn[0] = array (due_date => 14:04:2011 flag => 0);
$arrReturn[1] = array (due_date => 14:04:2011 flag => 0);
$arrReturn[2] = array (due_date => 14:04:2011 flag => 0);
...
$ctr = 0;
foreach ($arrKeys as $id => $key) {
$res_array[$ctr][$key] = $arrValues[$id];
if ($key == 'flag_code') $ctr++;
}
print_r($res_array);
Output:
Array
(
[0] => Array
(
[due_date] => 12:04:2011
[flag_code] => 0
)
[1] => Array
(
[due_date] => 13:04:2011
[flag_code] => 0
)
[2] => Array
(
[due_date] => 14:04:2011
[flag_code] => 0
)
)

Categories