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
Related
I got many values from html rows table and I want to submit with XML Array, but I don't know how to change to array multidimension in php array.
and this is my array
Array
(
[RSVRS_TRX_H_ID] => Array
(
[0] => 2
[1] => 2
)
[RSVRS_TRX_D_ID] => Array
(
[0] => 3
[1] => 4
)
[PROCESS_STAT] =>
[IMG_CODE] => Array
(
[0] => KTP_IMG
[1] => KWIT_IMG
)
[IMG_DATA] => Array
(
[0] => iniktpimg
[1] => inilkwit
)
[NEED_REVISION] => Array
(
[0] => 1
[1] => 0
)
[NOTES] => Array
(
[0] => ya
[1] => tidak
)
[USR_CRT] => 30305
)
and i want to multidimensional array like this or it's just to looping?
Array
(
Array(
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 3
[PROCESS_STAT] =>
[IMG_CODE] => KTP_IMG
[IMG_DATA] => iniktpimg
[NEED_REVISION] => 1
[NOTES] => ya
[USR_CRT] => 30305
),
Array(
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 4
[PROCESS_STAT] =>
[IMG_CODE] => KWIT_IMG
[IMG_DATA] => inilkwit
[NEED_REVISION] => 0
[NOTES] => tidak
[USR_CRT] => 30305
)
)
loop over all array get key and value of array. loop over each value of array, assign to each key its values at numbers index of result number index come from value of array. if value is not array assign value with key to each number index of result.
array_walk($array, function($value, $key)use (&$result){
if(is_array($value) && !is_null($value)){
foreach ($value as $k => $v) {
$result[$k][$key]=$v;
}
}else
{
foreach ($result as $rkey => $rvalue) {
$result[$rkey][$key] =$value;
}
}
});
print_r($result);
Output
Array (
[0] => Array (
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 3
[PROCESS_STAT] =>
[IMG_CODE] => KTP_IMG
[IMG_DATA] => iniktpimg
[NEED_REVISION] => 1
[NOTES] => ya
[USR_CRT] => 30305 )
[1] => Array (
[RSVRS_TRX_H_ID] => 2
[RSVRS_TRX_D_ID] => 4
[PROCESS_STAT] =>
[IMG_CODE] => KWIT_IMG
[IMG_DATA] => inilkwit
[NEED_REVISION] => 0
[NOTES] => tidak
[USR_CRT] => 30305 ) )
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++;
}
}
}
Problem solved thanks guys :
foreach($FIMP as &$value){
foreach($value as &$values){
$values[0]=$values[3].$values[2].$values[1];
}
I have the following arrays:
$FIMP=array(
1 => array(
1 => array('a','b','c')
2 => array('a1','b1','c1')
2 => array(
1 => array('a2','b2','c2')
3 => array(
1 => array('a3','b3','c3')
2 => array('a4','b4','c4')
3 => array('a5','b5','c5')
)
I want to add the the values: b and c and overwrite a with the 'bc'
result like this
$FIMP=array(
1 => array(
1 => array('bc','b','c')
2 => array('b1c1','b1','c1')
2 => array(
1 => array('b2c2','b2','c2')
...
I already tried $FIMP
foreach($FIMP as $value){
foreach($value as $values){
$values[0]=$values[3].$values[2].$values[1];
}
}
in the out put there is no change to the previous $FIMP ,
From the PHP foreach documentation:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
So to change the values in $FIMP, you need to change your foreach loop to the following:
foreach($FIMP as &$value){
foreach($value as &$values){
// alter $value or $values here
}
}
<?php
$FIMP=array(
1 => array(
1 => array('a','b','c'),
2 => array('a1','b1','c1'),
),
2 => array(
1 => array('a2','b2','c2'),
),
3 => array(
1 => array('a3','b3','c3'),
2 => array('a4','b4','c4'),
3 => array('a5','b5','c5'),
),
);
foreach($FIMP as & $v1){
foreach($v1 as & $v2){
$v2[0] = $v2[1].$v2[2];
}
}
var_dump($FIMP);
You can do it like this:
foreach($FIMP as &$first){
foreach($first as &$second){
$second[0] = $second[1].$second[2];
}
}
OUTPUT
Array
(
[1] => Array
(
[1] => Array
(
[0] => bc
[1] => b
[2] => c
)
[2] => Array
(
[0] => b1c1
[1] => b1
[2] => c1
)
)
[2] => Array
(
[1] => Array
(
[0] => b2c2
[1] => b2
[2] => c2
)
)
[3] => Array
(
[1] => Array
(
[0] => b3c3
[1] => b3
[2] => c3
)
[2] => Array
(
[0] => b4c4
[1] => b4
[2] => c4
)
[3] => Array
(
[0] => b5c5
[1] => b5
[2] => c5
)
)
)
$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];
}
}
I'm a beginner at php and was searching for a solution all day long without success.
I have the following array:
$data = Array
(
[0] => Array
(
[my_id] => 1
[my_post_id] => 123
[my_status] => 1
[my_rating] => 5
)
[1] => Array
(
[my_id] => 2
[my_post_id] => 123
[my_status] => 1
[my_rating] => 4
)
[2] => Array
(
[my_id] => 3
[my_post_id] => 123
[my_status] => 1
[my_rating] => 5
)
[3] => Array
(
[my_id] => 4
[my_post_id] => 456
[my_status] => 1
[my_rating] => 5
)
[4] => Array
(
[my_id] => 5
[my_post_id] => 456
[my_status] => 1
[my_rating] => 3
)
)
and would like to merge the arrays with the same 'my_post_id' and count the values for 'my_status' and 'my_rating' which have the same 'my_post_id'.
At the end, I would like to have the following array:
$data = Array
(
[0] => Array
(
[my_post_id] => 123
[my_status] => 3
[my_rating] => 14
)
[1] => Array
(
[my_post_id] => 456
[my_status] => 2
[my_rating] => 8
)
)
I could get arrays with unique 'my_post_id' with the following code but I couldn't find out how to count the other values.
$out = array();
foreach( $data as $row ) {
$out[$row['my_post_id']] = $row;
}
$array = array_values( $out );
Any help would be much appreciated.
Daniel
This will produce the array you are looking for:
$out = array();
foreach( $data as $row ) {
if (!isset($out[$row['my_post_id']])) {
$out[$row['my_post_id']] = Array( "my_id"=>$row['my_id'],
"my_status" => $row["my_status"],
"my_rating" => $row["my_rating"]);
}
else {
$out[$row['my_post_id']]["my_status"] += $row["my_status"];
$out[$row['my_post_id']]["my_rating"] += $row["my_rating"];
}
}
results in:
Array
(
[123] => Array
(
[my_id] => 1
[my_status] => 3
[my_rating] => 14
)
[456] => Array
(
[my_id] => 4
[my_status] => 2
[my_rating] => 8
)
)
try the following - untested code
foreach($data as $key => $val){
if(!array_search($val['my_post_id'],$newArray)){
$newArray[]=array('my_post_id' => $val['my_post_id'],
'my_status' => $val['my_status'],
'my_rating' => $val['my_rating']);
}else{
$myIndex=array_search($val['my_post_id'],$newArray);
$newArray[$myIndex]['my_status']+=$val['my_status'];
$newArray[$myIndex]['my_rating']+=$val['my_rating'];
}
}