$groupLevelOne = array();
foreach ($data['display'] as $item) {
$key = implode($data['data'][$i]['eid'])
.date('Y-m',strtotime($item['date_d'][0]));
if (!isset($groupLevelOne[$key])) {
$groupLevelOne[$key] = array(
'employeeId' => $data['data'][$i]['eid'],
'efullname' => $item['fullname'][0],
'date_d' => date('Y-m',strtotime($item['date_d'][0])),
'hrsdiff' => $item['hrsdiff'][0],
'dayspresent' => $item['key'][0],
);
}
else {
$groupLevelOne[$key]['dayspresent'] = $groupLevelOne[$key]['dayspresent'] + $item['key'][0];
}
$i++;
}
Already have this code displaying this they are group with same id's and dates
Array
(
[12014-01] => Array
(
[employeeId] => Array
(
[0] => 1
)
[efullname] => IBARDOLAZA VIRGILIO
[date_d] => 2014-01
[hrsdiff] => 5.0333
[dayspresent] => 2
)
[12014-02] => Array
(
[employeeId] => Array
(
[0] => 1
)
[efullname] => IBARDOLAZA VIRGILIO
[date_d] => 2014-02
[hrsdiff] => 8.0333
[dayspresent] => 24
)
[12014-03] => Array
(
[employeeId] => Array
(
[0] => 1
)
[efullname] => IBARDOLAZA VIRGILIO
[date_d] => 2014-03
[hrsdiff] => 8.0667
[dayspresent] => 26
)
)
And what to have my next level op groupings to be merge with the same employeeID and different idexes of dates that will be 1 array that look like this
[1] => Array
(
[employeeId] => Array
(
[0] => 1
)
[efullname] => IBARDOLAZA VIRGILIO
[date_d1] =>2014-01
[dayspresent1] => 2
[date_d2] =>2014-02
[dayspresent2] => 24
[date_d3] =>2014-03
[dayspresent3] => 26
)
------------ follow up question -------------
[0] => Array
(
[employeeId] => 21
[efullname] => MANOGURA EDGAR
[hrsdiff] => 2.2331
[days] => Array
(
[2014-02] => 23
[2014-03] => 26
[2014-04] => 23
)
)
[1] => Array
(
[employeeId] => 1
[efullname] => IBARDOLAZA VIRGILIO
[hrsdiff] => 5.0333
[days] => Array
(
[2014-01] => 2
[2014-02] => 24
[2014-03] => 26
[2014-04] => 26
)
)
i want my first index to have [2014-01] => 0
so they will have all same days index
You could do all of this in a single run:
$groups = array();
foreach ($data['display'] as $item) {
$eid = implode($data['data'][$i]['eid']);
$date = date('Y-m',strtotime($item['date_d'][0]));
if (!isset($groups[$eid])) {
$groups[$eid] = array(
'employeeId' => $eid,
'efullname' => $item['fullname'][0],
'hrsdiff' => $item['hrsdiff'][0],
'days' => array(
$date => $item['key'][0],
)
);
} else {
#$groups[$eid]['days'][$date] += $item['key'][0];
}
}
Related
Actual Array
Array
(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
[2] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
Expected Result
Array
(
[3] => Array(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
)
[4] => Array(
[0] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
)
You can create a new array and set value of master id as the index and put the value in it.
$data = array();
foreach($array as $key=>$value){
$data[$value['master_id']][] = $value;
}
$actualArray = array(array('sub_id' => 3, 'sub_name' => 'tttt', 'master_id' => 3),array('sub_id' => 4, 'sub_name' => 'yyyy', 'master_id' => 3),array('sub_id' => 5, 'sub_name' => 'kkkk', 'master_id' => 4));
$tempArray = array_unique(array_column($actualArray, 'master_id'));
$uniqueArray = array_intersect_key($actualArray, $tempArray);
foreach ($uniqueArray as $key => $masters) {
$count = 0;
foreach ($actualArray as $key1 => $actuals) {
if($masters['master_id'] == $actuals['master_id']){
$expectedArray[$key][$count] = $actuals;
$count++;
}
}
}
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)
I'd like to merge two arrays on same key. I've tried my best to do it but unable to get success. I've added both the Arrays below. Kindly check both the Arrays in detail and help me how to merge it using same key.
Here's the 1st array :
Array
(
[1] => Array
(
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[costprice3] => 700
[margin3] => 25
)
)
Here's the 2 array :
Array
(
[1] => Array
(
[entityType1] => Products1
)
[2] => Array
(
[entityType2] => Products2
)
[3] => Array
(
[entityType3] => Products3
)
)
i want to need like that array please suggestion me
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice3] => 700
[margin3] => 25
)
)
please help me how to merge two array
Try this :
foreach($array1 as $key => $value) {
$array1[$key]['entityType'.$key] = $array2[$key]['entityType'.$key];
}
print_r($array1);
<?php
$array1 = [
1 => [
'costprice1' => 500,
'margin1' => 20
],
2 => [
'costprice2' => 600,
'margin2' => 15
],
3 => [
'costprice2' => 700,
'margin2' => 25
],
];
$array2 = [
1 => ['entityType1' => 'Products1'],
2 => ['entityType2' => 'Products2'],
3 => ['entityType3' => 'Products3'],
];
array_walk($array2, function(&$v, $k)use($array1){
$v = array_merge($v, $array1[$k]);
});
print_r($array2);
Output:
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice2] => 700
[margin2] => 25
)
)
https://eval.in/618561
I have an array in which i want only lineNo and Isdirty field in each array .
My demo code is
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[ObjectType] => 12
[LineNo] => 1
[CompanyID] => 1
[BranchID] => 46
[ModifiedDate] => 2014-04-25 05:10:15
[RevisionNumber] => 6
[IsDirty] =>
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 430
[RevisionNumber] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 372
[RevisionNumber] => 1
)
)
)
Only LineNo And Isdirty field want in every index array .So please suggest me solution.
You can sue following;
$finalArr = array();
foreach ($arr as $key => $item) {
foreach ($item as $k => $v) {
$finalArr[$key][] = array(
"LineNo" => $v["LineNo"],
"IsDirty" => $v["IsDirty"]
);
}
}
Here is a working demo: Demo
This is my array of timestamps. I would like to eliminate values within 30 seconds of each other, only keeping the value if there is not another value within 30 sec. Any help would be greatly appreciated!
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356399005
[3] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356399007
[2] => 1356398871
[3] => 1356398876
)
[99996] => Array
(
[0] => 1356399003
[1] => 1356399004
[2] => 1356399008
)
[99995] => Array
(
[0] => 1356399009
)
)
My expected output:
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356398871
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)
Any solutions/advice would be greatly appreciated! Thanks!
Your output is wrong .. because 1356398971 + 30 = 1356399001 which is grater than 1356399000 if i understand you clearly this i what it should look like
$data = array(
99999 => array(
0 => 1356399000,
1 => 1356398971,
2 => 1356399005,
3 => 1356413406,
),
99997 => array(
0 => 1356399002,
1 => 1356399007,
2 => 1356398871,
3 => 1356398876,
),
99996 => array(
0 => 1356399003,
1 => 1356399004,
2 => 1356399008,
),
99995 => array(
0 => 1356399009,
),
);
echo "<pre>";
$data = array_map(function ($values) {
rsort($values);
$ci = new CachingIterator(new ArrayIterator($values));
$values = array();
foreach ( $ci as $ts ) {
if ($ci->hasNext()) {
abs($ci->current() - $ci->getInnerIterator()->current()) > 30 and $values[] = $ts;
} else {
$values[] = $ts;
}
}
sort($values);
return $values;
}, $data);
print_r($data);
Output
Array
(
[99999] => Array
(
[0] => 1356398971
[1] => 1356413406
)
[99997] => Array
(
[0] => 1356398871
[1] => 1356399002
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)