array difference returning edited values - php

The following code checks the first array(parameter1) aginst the second array(parameter2) and returns an array that tells which elements were modified.
$array_data = array(
"studDetails" => array(
"studDet" => array(
"studClass" => "V",
),
),
"email" => "kavya#opspl.com",
"systemNames" => array("EMR"),
);
$array_edited = $array_data;
//EDITING
$array_edited['email'] = "kam";
$array_edited['studDetails']['studDet']['studClass'] = "VV";
echo "<pre>";
print_r(array_diff_assoc2_deep($array_edited, $array_data));
function array_diff_assoc2_deep($array_edited, $array_data)
{
$difference = array();
foreach ($array_edited as $row => $value) {
if (!isset($array_data[$row]) && !empty($value)) {
$difference['added'][$row] = $value;
} else if (is_array($array_edited[$row]) && is_array($array_data[$row])) {
$difference[$row] = array_diff_assoc2_deep($array_edited[$row], $array_data[$row]);
} else if ((string)$value != (string)$array_data[$row]) {
$difference['edited'][$row] = array("old" => $array_data[$row], "new" => $value);
}
}
$difference = array_filter($difference);
return $difference;
}
OUTPUT:
Array (
[studDetails] => Array (
[studDet] => Array (
[edited] => Array (
[studClass] => Array (
[old] => V
[new] => VV
)
)
)
)
[edited] => Array (
[email] => Array (
[old] => kavya#opspl.com
[new] => kam
)
)
)
I want the output to be in a single edited key no matter how many internal arrays are there.
DESIRED OUTPUT:
Array (
[edited] => Array (
[email] => Array (
[old] => kavya#opspl.com
[new] => kam
)
[studClass] => Array (
[old] => V
[new] => VV
)
)
)

The reason this happens is the recursive call:
$difference[$row] = array_diff_assoc2_deep($array_edited[$row], $array_data[$row]);
With this line, you put each recursive comparison result into a separate key.
If you don't do this but keep the resulting array flat, you'll get what you want:
function array_diff_assoc2_deep($array_edited, $array_data, $difference = array())
{
foreach ($array_edited as $row => $value) {
if (!isset($array_data[$row]) && !empty($value)) {
$difference['added'][$row] = $value;
} else if (is_array($array_edited[$row]) && is_array($array_data[$row])) {
$difference = array_diff_assoc2_deep($array_edited[$row], $array_data[$row], $difference);
} else if ((string)$value != (string)$array_data[$row]) {
$difference['edited'][$row] = array("old" => $array_data[$row], "new" => $value);
}
}
$difference = array_filter($difference);
return $difference;
}
Three lines have changed:
function array_diff_assoc2_deep($array_edited, $array_data, $difference = array())
//{
## REMOVED: $difference = array();
// foreach ($array_edited as $row => $value) {
// if (!isset($array_data[$row]) && !empty($value)) {
// $difference['added'][$row] = $value;
// } else if (is_array($array_edited[$row]) && is_array($array_data[$row])) {
$difference = array_diff_assoc2_deep($array_edited[$row], $array_data[$row], $difference);
// } else if ((string)$value != (string)$array_data[$row]) {
// $difference['edited'][$row] = array("old" => $array_data[$row], "new" => $value);
// }
//
// }
// $difference = array_filter($difference);
// return $difference;
//}
You can see the output here.

Related

Remove items from multidimensional array in PHP

I need to remove empty items in a multidimensional array.
Is there a simple way I can remove the empty items easily?
I need to keep only 2010-06 and 2010-07.
Thank you very much!
Array
(
[2010-01] => Array
(
[2010-03] => Array
(
[0] =>
)
[2010-04] => Array
(
[0] =>
)
[2010-06] => Array
(
[0] => stdClass Object
(
[data_test] => value
[date] => 2010-05-01 12:00:00
)
)
[2010-07] => Array
(
[0] => stdClass Object
(
[data_test] => value
[date] => 2010-05-01 12:00:00
)
)
)
)
Try this Function. This will solve your issue.
function cleanArray($array)
{
if (is_array($array))
{
foreach ($array as $key => $sub_array)
{
$result = cleanArray($sub_array);
if ($result === false)
{
unset($array[$key]);
}
else
{
$array[$key] = $result;
}
}
}
if (empty($array))
{
return false;
}
return $array;
}
array_filter will not wrok with this array
so try this custom function
<?php
$array =array(
20 => array(
20 => array(
0=> ''
),
10 => array(
0=> 'hey'
)
)
);
function array_remove_empty($arr){
$narr = array();
while(list($key, $val) = each($arr)){
if (is_array($val)){
$val = array_remove_empty($val);
// does the result array contain anything?
if (count($val)!=0){
// yes :-)
$narr[$key] = $val;
}
}
else {
if (trim($val) != ""){
$narr[$key] = $val;
}
}
}
unset($arr);
return $narr;
}
print_r(array_remove_empty($array));
?>
found this answer here

Determine whether array key value is blank

I have an array like below, as you can see the last array [adad] value is blank, how can I write an if statement to tell whether this is blank.
Array
(
[K] => Array
(
[0] => mabel__chan
[1] => mabel chan
)
[B] => Array
(
[0] => kieron br
)
[C] => Array
(
[0] => a br
[1] => a
)
[adad] => Array
(
[0] =>
)
)
I have tried doing this
if (count(array_filter($array)) == 0) {}
Pseudo code
if(array[key] == blank) {
echo "is blank";
} else {
echo "isn't blank";
}
**PHP Script this is how I get my data from mongoDB*
The answer below is working correctly when I use echos but now when I'm trying to push into new arrays its broken somewhere I get no data back anymore.
$col = "A" . $user->agencyID;
$db = $m->rules;
$collection = $db->$col;
$id = $_POST['ruleID'];
$search = array(
'_id' => new MongoId($id)
);
$cursor = $collection->find($search);
$validTagsArray = array();
$validArray = array();
foreach ($cursor as $key => $value) {
$temp = array_walk($array, function($v, $k) {
if (count(array_filter($v)) === 0) {
foreach ($value['AutoFix'] as $keyTwo => $valTwo) {
$x = 0;
$validTagsArray['data'][] = array($keyTwo, $x);
}
} else {
foreach ($value['AutoFix'] as $keyTwo => $valTwo) {
$x = 0;
foreach ($valTwo as $key => $value) {
$x++;
}
$validTagsArray['data'][] = array($keyTwo, $x);
}
}
});
}
echo json_encode($validTagsArray);
You can try this -
$array = array
(
'K' => array('0' => 'mabel__chan','1' => 'mabel chan'),
'B' => array('0' => 'kieron br'),
'C' => array('0' => 'a br', '1' => 'a'),
'adad' => array('0' => '')
);
$temp = array_walk($array, function($v, $k) {
if(count(array_filter($v)) === 0) { // check the count of non-empty elements in the sub array
echo $k . ' is empty';
}
});
Output
adad is empty
You can write as:
if([adad][0]== " ")
{
}
You can also try this :
foreach($array as $key=>$value){
if(empty($value))
echo "empty";
}

Compare an array based on previous value of the array

I have an array that looks like this:
Array (
[0] => Array (
[START] => COI-COK
[RETURN] => CAI - DEL
)
[1] => Array (
[START] => COK - AMM
[RETURN] => CAI - DEL
)
)
I want to check if both 'start' and 'end' values of previous and current array are same or not. If not then, print some value. How can I do it?
This is my attempt:
foreach($data as $datas)
{
$old_start = $datas['START'];
$old_return = $datas['RETURN'];
...
if( ($old_start == $datas['START']) && ($old_return == $datas['RETURN']))
{
}
else
{
}
}
But it didn't work because all the time old_start value will be equal to $datas['START'].
print_r($data) shows this output:
Array (
[0] => Array (
[Sl] => 2
[TRAVELDAY] => 2015-11-11
[RETURNDAY] => 2015-11-27
[START] => COI-COK
[RETURN] => CAI - DEL
)
[1] => Array (
[Sl] => 1
[TRAVELDAY] => 2015-11-11
[RETURNDAY] => 2015-11-27
[START] => COK - AMM
[RETURN] => CAI - DEL
)
)
You have to put the assignment after the comparison, not before:
$old_start = '';
$old_return = '';
foreach($data as $datas)
{
//....
if($old_start=='' || $old_start == $datas['START'] && $old_return == $datas['RETURN'])
{
//....
}
else
{
//code to be executed
}
$old_start = $datas['START'];
$old_return = $datas['RETURN'];
}
Try like this..
$old_start = "";
$old_return = "";
foreach($data as $datas)
{
if( ($old_start == $datas['START']) && ($old_return == $datas['RETURN']))
{
//true code to be executed
}
else
{
//false code to be executed
}
$old_start = $datas['START'];
$old_return = $datas['RETURN'];
}
foreach($data as $sample) {
if (!isset($temp)) {
$temp = $sample;
} else {
if ($temp['START']==$sample['START'] && $temp['RETURN']==$sample['RETURN']) {
; //WHATEVER EQUAL
} else {
; //WHATEVER NOT EQUAL
}
$temp = $sample;
}
}
$array = Array ( 0 => Array ( "START" => "COI-COK", "RETURN" => "CAI - DEL"), 1 => Array ( "START" => "COK - AMM","RETURN" => "CAI - DEL" ),2=> Array ( "START" => "COK - AMM","RETURN" => "CAI - DEL" ) );
$old_start = "";
$old_return = "";
foreach($array as $ak=>$av){
if(empty($old_start)){
$old_start = $av['START'];
$old_return = $av['RETURN'];
}else{
if($old_start == $av['START'] && $old_return == $av['RETURN']){
echo "SAME\n";
}else{
echo "Varies\n";
}
$old_start = $av['START'];
$old_return = $av['RETURN'];
}
}
//Output
Varies //key 1 will not match with key 0 value
Same // key 2 will not match with key 1 value
Hope this clears your logic, add as many array value you want , I modified your array from 2 to 3 to show the difference. This will compare n with n+1 and show the result.

Sort and group array from timestamp

I have the following array:
Array
(
[0] => Array
(
[dest_in_id] => 1
[dest_user_id] => 37251
[dest_inv_user_id] => 37247
[dest_timestamp] => 1387168510
[dest_destination_id] => 64
)
[1] => Array
(
[gi_in_id] => 3
[gi_user_id] => 37251
[gi_inv_user_id] => 14564
[gi_timestamp] => 1345220045
[gi_group_id] => 2
)
[2] => Array
(
[dest_in_id] => 2
[dest_user_id] => 37251
[dest_inv_user_id] => 37257
[dest_timestamp] => 1387168510
[dest_destination_id] => 64
)
[3] => Array
(
[gi_in_id] => 3
[gi_user_id] => 37251
[gi_inv_user_id] => 14564
[gi_timestamp] => 1345220045
[gi_group_id] => 2
)
)
Need to review the difference in Timestamp each array and if it <= day (86400), then create an array type:
Array
(
[1387168510] => Array
(
[0] => Array
(
[dest_in_id] => 1
[dest_user_id] => 37251
[dest_inv_user_id] => 37247
[dest_timestamp] => 1387168510
[dest_destination_id] => 64
)
[1] => Array
(
[dest_in_id] => 2
[dest_user_id] => 37251
[dest_inv_user_id] => 37257
[dest_timestamp] => 1387168510
[dest_destination_id] => 64
)
)
[1345220045] => Array
(
[0] => Array
(
[gi_in_id] => 3
[gi_user_id] => 37251
[gi_inv_user_id] => 14564
[gi_timestamp] => 1345220045
[gi_group_id] => 2
)
[1] => Array
(
[gi_in_id] => 3
[gi_user_id] => 37251
[gi_inv_user_id] => 14564
[gi_timestamp] => 1345220045
[gi_group_id] => 2
)
)
)
Important note! Keys may be different!
At once appealed to the function usort. Wanted to do something like this:
usort($aInvitesRows, function($a, $b) {
$akey = array_keys($a);
$bkey = array_keys($b);
if (($b[preg_grep("/(.*?)_timestamp/", $bkey)[3]] - $a[preg_grep("/(.*?)_timestamp/", $akey)[3]]) <= 86400) {
return $b[preg_grep("/(.*?)_timestamp/", $bkey)[3]] = array($a, $b);
}
});
Success of this venture was not crowned :(
At this stage, using usort could only sort by Timestamp (*), what do you do - do not know the, address for the help!
*
usort($aInvitesRows, function($a, $b) {
$akey = array_keys($a);
$bkey = array_keys($b);
return strcmp($b[preg_grep("/(.*?)_timestamp/", $bkey)[3]], $a[preg_grep("/(.*?)_timestamp/", $akey)[3]]);
});
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// #TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
$arr = $aInvitesRows;
$result = array();
$item = array_shift($arr);
$akeys = array_keys($item);
$dt = new \DateTime();
$dt->setTimestamp($item[preg_grep("/(.*?)_timestamp/", $akeys)[3]]);
$dt->setTime(0, 0, 0);
$result[$dt->getTimestamp()] = array($item);
foreach ($arr as $item) {
$akeys = array_keys($item);
$f = false;
foreach ($result as $day => $items) {
$diff = ($item[preg_grep("/(.*?)_timestamp/", $akeys)[3]]) - $day;
if ($diff < 86400 && $diff > 0) {
$result[$day][] = $item;
$f = true;
break 1;
}
}
if (!$f) {
$dt = new \DateTime();
$dt->setTimestamp($item[preg_grep("/(.*?)_timestamp/", $akeys)[3]]);
$dt->setTime(0, 0, 0);
$result[$dt->getTimestamp()] = array($item);
}
}

How to get the value from serialized array by using preg_match in php

I need to get the value from the serialized array by matching the index value.My unserialized array value is like
Array ( [info1] => test service [price_total1] => 10
[info2] => test servicing [price_total2] => 5 )
I need to display array like
Array ( [service_1] => Array ([info]=>test service [price_total] => 10 )
[service_2] => Array ([info]=>test servicing [price_total] => 5 ))
buy i get the result like the below one
Array ( [service_1] => Array ( [price_total] => 10 )
[service_2] => Array ( [price_total] => 5 ) )
my coding is
public function getServices($serviceinfo) {
$n = 1;
$m = 1;
$matches = array();
$services = array();
print_r($serviceinfo);
if ($serviceinfo) {
foreach ($serviceinfo as $key => $value) {
if (preg_match('/info(\d+)$/', $key, $matches)) {
print_r($match);
$artkey = 'service_' . $n;
$services[$artkey] = array();
$services[$artkey]['info'] = $serviceinfo['info' . $matches[1]];
$n++;
}
if ($value > 0 && preg_match('/price_total(\d+)$/', $key, $matches)) {
print_r($matches);
$artkey = 'service_' . $m;
$services[$artkey] = array();
$services[$artkey]['price_total'] = $serviceinfo['price_total' . $matches[1]];
$m++;
}
}
}
if (empty($services)) {
$services['service_1'] = array();
$services['service_1']['info'] = '';
$services['service_1']['price_total'] = '';
return $services;
}
return $services;
}
I try to print the matches it will give the result as
Array ( [0] => info1 [1] => 1 ) Array ( [0] => price_total1 [1] => 1 )
Array ( [0] => info2 [1] => 2 ) Array ( [0] => price_total2 [1] => 2 )
Thanks in advance.
try this. shorted version and don't use preg_match
function getServices($serviceinfo) {
$services = array();
if (is_array($serviceinfo) && !empty($serviceinfo)) {
foreach ($serviceinfo as $key => $value) {
if(strpos($key, 'info') === 0){
$services['service_'.substr($key, 4)]['info']=$value;
}elseif (strpos($key, 'price_total') === 0){
$services['service_'.substr($key, 11)]['price_total']=$value;
}
}
}
return $services;
}
$data = array('info1'=>'test service','price_total1'=>10,'info2'=>'test servicing','price_total2'=>5);
$service = getServices($data);
print_r($service);

Categories