PHP Array Sum By same key value pair needs to Calculated - php

<?php
error_reporting(E_ALL);
$test_array = Array(Array
(
"pid" => 1,
"encounter" => 20,
"code" => abc,
"fee" => 300.00
),
Array
(
"pid" => 1,
"encounter" => 20,
"code" => abc,
"fee" => 300.00
),
Array
(
"pid" => 2,
"encounter" => 20,
"code" => abc,
"fee" => 80
),
Array
(
"pid" => 3,
"encounter" => 20,
"code" => xyz,
"fee" => 90
),
Array
(
"pid" => 5,
"encounter" => 40,
"code" => xyz,
"fee" => 100
),
Array
(
"pid" => 3,
"encounter" => 40,
"code" => xyz,
"fee" => 100
),
Array
(
"pid" => 2,
"encounter" => 20,
"code" => abc,
"fee" => 80
),
Array
(
"pid" => 1,
"encounter" => 20,
"code" => xyz,
"fee" => 40
));
//Declaration...
$pre_pid = "";
$pre_encounter = "";
$pre_code = "";
$pre_fee = "";
$sum_charges = 0;
/*Foreach loop*/
$i=0;
foreach($test_array as $my_arr){
$pre_pid = $my_arr['pid'];
$pre_encounter = $my_arr['encounter'];
$pre_code = $my_arr['code'];
if($pre_pid == $my_arr['pid'] && $pre_encounter == $my_arr['encounter'] && $pre_code == $my_arr['code']){
echo "FEE-AMOUNT=".$my_arr['fee'];
$sum_charges+=$my_arr['fee'];
echo '<br/>';
}
$i++;
}
//Getting Sum = 1090
//Actual Sum I needed = 710
?>
Hello Friends I am trying above code where i want fee should be calculated of those who having same 3 key value pair.
For Example IF each array 3 key values are same then calculate those fee amount only.

if i understood your problem, you need to sum the fee column everytime the 3 other columns have the same value. I customised your code a bit so you can have the total cost for each of your elements.
//Declaration...
$pre_pid = "";
$pre_encounter = "";
$pre_code = "";
$pre_fee = "";
$sum_charges = 0;
$sum_array = array();
$total_fees = 0;
/*Foreach loop*/
foreach($test_array as &$my_arr){
$pre_pid = $my_arr['pid'];
$pre_encounter = $my_arr['encounter'];
$pre_code = $my_arr['code'];
$pre_fee = $my_arr['fee'];
$fee_ammount = $pre_fee;
$duplicates_check = array();
foreach($test_array as $value) {
if($pre_pid == $value['pid'] && ($pre_encounter != $value['encounter'] || $pre_code != $value['code'])){
$duplicate = false;
foreach($duplicates_check as $duplicate_array) {
if($duplicate_array == $value)
$duplicate =true;
}
if(!$duplicate) {
$fee_ammount += $value['fee'];
$duplicates_check[] = $value;
}
}
}
$my_arr['total_fee'] = $fee_ammount;
if(!isset($sum_array[$my_arr['pid']])) {
$sum_array[$my_arr['pid']] = $my_arr['total_fee'];
echo 'pid => '.$my_arr['pid'].', total fees => '.$my_arr['total_fee'].'<br />';
$total_fees += $my_arr['total_fee'];
}
}
echo 'Total : '.$total_fees;
var_dump($sum_array);

Related

PHP array key sum value if exist in loop with dynamic value

I create of Tree (Bill of Materials) from SQL and i'm make calculation of product. I have few if in if and if in if etc. I want to display sumamry of all individual part number with total quantitny which i'm calculating dynamicly. So, before loop and function if. i'm created three variables:
$TablicaMiH = array();
$TablicaBo = array();
$TablicaAss = array();
when I meet the conditions, which depend on the if function, each of them performs operations of adding to the array. this is:
$TablicaMiH += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];
or
$TablicaBo += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];
or
$TablicaAssy += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];
The commands execute correctly as needed, but they do not add the sum value from the $ multipler variable to me, they only do individual PartNumbers and take the last value from the $ multipler variable
here is the result of what was received from the script:
Tablica MiH:
Array ( [333/E8093] => 2 [332/F2592] => 3 [332/F3144] => 9 [332/F3147] => 21 [332/F2684] => 8 [333/D1641] => 12 [333/D1202] => 22 [332/F2588] => 1 [333/E7883] => 1 [333/E8131] => 1 )
Tablica BO:
Array ( [826/10381] => 12 [331/30854] => 7 [332/F3213] => 4 [123/06090] => 84 [1315/0307Z] => 1 [823/10874] => 1 [333/E7939] => 4 [813/10186] => 2 [332/H3476] => 3 [32/920300] => 11 [332/F3282] => 1 [32/926051] => 1 )
Tablica Ass:
Array ( [2th] => 1 [3TH] => 1 [4th] => 1 [5th] => 1 [6th] => 1 [7th] => 1 [8th] => 1 [9th] => 1 [10Th] => 1 [IN_1TH] => 1 )
and result what i need to have:
$TablicaMiH
332/F2588||5
332/F2592||10
332/F2684||9
332/F3144||27
332/F3147||38
333/D1202||40
333/D1641||16
333/E7883||1
333/E8093||12
333/E8131||1
Tablica BO:
123/06090||85
1315/0307Z||1
32/920300||11
32/926051||1
331/30854||20
332/f3213||29
332/F3282||1
332/H3476||3
333/E7939||4
813/10186||3
823/10874||1
826/10381||13
Tablica Ass:
10Th||1
1TH||1
2TH||1
3TH||1
4th||1
5th||1
6th||1
7th||1
8th||1
9th||1
IN_1TH||1
I Hope you understand me what i mean, and you can help me, thank you
Example to understand my problem:
<?php
$exampleArraY = array(
"PartNumber1" => 1,
"PartNumber2" => 1,
"PartNumber3" => 1,
"PartNumber4" => 1,
"PartNumber5" => 1,
"PartNumber6" => 1,
);
$value = "PartNumber1";
$value2 = "PartNumber2";
$value3 = "PartNumber3";
$value4 = "PartNumber4";
$value5 = "PartNumber5";
$value6 = "PartNumber6";
$value7 = "PartNumber7";
$multipler = 1;
if(in_array($value, $exampleArraY)){
$exampleArraY[$value][1] += $multipler;
}else {
$exampleArraY += [$value => $multipler];
}
if(in_array($value2, $exampleArraY)){
$exampleArraY[$value2][1] += $multipler;
}else {
$exampleArraY += [$value2 => $multipler];
}
if(in_array($value3, $exampleArraY)){
$exampleArraY[$value3][1] += $multipler;
}else {
$exampleArraY += [$value3 => $multipler];
}
if(in_array($value4, $exampleArraY)){
$exampleArraY[$value4][1] += $multipler;
}else {
$exampleArraY += [$value4 => $multipler];
}
if(in_array($value5, $exampleArraY)){
$exampleArraY[$value5][1] += $multipler;
}else {
$exampleArraY += [$value5 => $multipler];
}
if(in_array($value6, $exampleArraY)){
$exampleArraY[$value6][1] += $multipler;
}else {
$exampleArraY += [$value6 => $multipler];
}
if(in_array($value7, $exampleArraY)){
$exampleArraY[$value7][1] += $multipler;
}else {
$exampleArraY += [$value7 => $multipler];
}
print_r($exampleArraY);
?>
Result:
Array ( [PartNumber1] => 1 [PartNumber2] => 1 [PartNumber3] => 1 [PartNumber4] => 1 [PartNumber5] => 1 [PartNumber6] => 1 )
Desired Result:
Array ( [PartNumber1] => 2 [PartNumber2] => 2 [PartNumber3] => 2 [PartNumber4] => 2 [PartNumber5] => 2 [PartNumber6] => 2 [PartNumber7] => 1 )
If I understand the problem correctly, you might be looking for something like this:
// create the starting array
$TablicaMiH = [];
// ... do interesting things... in a loop most likely....
// if we dont have a value for this key yet, set it to 0
if (!isset($TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']])) {
$TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']] = 0;
}
// now add the multiplier for this part number
$TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']] += $multipler;
The problem like i explained was in construction of arrays. Here is my solution to your problem:
<?php
$exampleArraY = array(
"PartNumber1" => 1,
"PartNumber2" => 1,
"PartNumber3" => 1,
"PartNumber4" => 1,
"PartNumber5" => 1,
"PartNumber6" => 1,
);
$values = array(
"value" => "PartNumber1",
"value2" => "PartNumber2",
"value3" => "PartNumber3",
"value4" => "PartNumber4",
"value5" => "PartNumber5",
"value6" => "PartNumber6",
"value7" => "PartNumber7");
$multipler = 1;
foreach($values as $key => $item){
if(isset($exampleArraY[$item])){
echo $exampleArraY[$item] += $multipler;
}else {
$exampleArraY[$item] = $multipler;
}
}
Output:
array(7) {
["PartNumber1"]=>int(2)
["PartNumber2"]=>int(2)
["PartNumber3"]=>int(2)
["PartNumber4"]=>int(2)
["PartNumber5"]=>int(2)
["PartNumber6"]=>int(2)
["PartNumber7"]=>int(1)
}

How can I achieve this in Array in PHP

am having problem achieving this result with array, I want to update another empty array with data in first array in such a way that the first two rows is ommitted and the first 3rows is added to the index 0 of the empty array, and the next 3 rows is also updated to the second index and so on. I have this Array in
`$arr = [
'tqty' => 9,
'tPrice' => 18700,
'food_name_1' => 'Black Coffee',
'food_quanty_1' => 1,
'food_price_1' => 1000,
'food_name_2' => 'Sub Combo',
'food_quanty_2' => 2,
'food_price_2' => 3000
];`
I want to use this array data and update another empty array this way, removing the first two rows
$arr2 = [
0 => [
'food_name_1' => 'Black Coffee',
'food_quanty_1' => 1,
'food_price_1' => 1000
],
1 => [
'food_name_2' => 'Sub Combo',
'food_quanty_2' => 2,
'food_price_2' => 3000
]
];
here is my code so far
$arr = [
'tqty' => 9,
'tPrice' => 18700,
'food_name_1' => 'Black Coffee',
'food_quanty_1' => 1,
'food_price_1' => 1000,
'food_name_2' => 'Sub Combo',
'food_quanty_2' => 2,
'food_price_2' => 3000
];
$newdat = [];
$count = 0;
$oldcount = 1;
foreach($arr as $key => $value){
if(preg_match_all('!\d+!', $key, $matches)){
if($oldcount == $matches[0][0]){
$newdat[$matches[0][0]] = [
$count => [
$key => $value
]
];
} else{
$count = 0;
$oldcount = $matches[0][0];
}
}
$count++;
}
I hope I get help soon. thanks
Assuming the array keys and order stay consistent you could use array_chunk
<?php
$inArray = [
'tqty' => 9,
'tPrice' => 18700,
'food_name_1' => 'Black Coffee',
'food_quanty_1' => 1,
'food_price_1' => 1000,
'food_name_2' => 'Sub Combo',
'food_quanty_2' => 2,
'food_price_2' => 3000,
];
$outArray = [];
// Remove first 2 values.
$inArray = array_slice( $inArray, 2 );
// 'Chunk' the rest of the values.
// true preserves keys.
$outArray = array_chunk( $inArray, 3, true );
echo '<pre>' . print_r( $outArray, true ) . '</pre>';
/**
Output:
<pre>Array
(
[0] => Array
(
[food_name_1] => Black Coffee
[food_quanty_1] => 1
[food_price_1] => 1000
)
[1] => Array
(
[food_name_2] => Sub Combo
[food_quanty_2] => 2
[food_price_2] => 3000
)
)
</pre>
*/
If I have understood you correctly,
$newdata = array();
for ($i = 1; $i++;) { // Intentionally no condition set.
if (array_key_exists('food_name_' . $i, $arr)) {
$temparray = array();
$temparray['food_name_' . $i] = $arr['food_name_' . $i];
if (array_key_exists('food_quanty_' . $i, $arr)) {
$temparray['food_quanty_' . $i] = $arr['food_quanty_' . $i];
}
if (array_key_exists('food_price_' . $i, $arr)) {
$temparray['food_price_' . $i] = $arr['food_price_' . $i];
}
$newdata[] = $temparray;
} else {
break; // break out of the loop
}
}
echo "<pre>";
print_r($newdata);
echo "</pre>";
die();
Loop through it and build the array index string via variables.
<?php
$arr = [
'tqty' => 9,
'tPrice' => 18700,
'food_name_1' => 'Black Coffee',
'food_quanty_1' => 1,
'food_price_1' => 1000,
'food_name_2' => 'Sub Combo',
'food_quanty_2' => 2,
'food_price_2' => 3000
];
foreach(array("food_name_","food_quanty_","food_price_") as $v){
// replace the set value of 2 here with a count() on the $arr
// and some basic math - IF you are always sure you'll have 3 fields
for($i=0;$i<2;$i++){
$newarr[$i][$v.($i+1)]=$arr[$v.($i+1)];
}
}
print_r($newarr);
?>
Here's a solution that will locate the _x at the end to check its a digit
This solution does not worry about how many non numbered fields you have, or how many fields there are per numbered "row", they are also indexed based on the _x number.
$arr = [
'tqty' => 9,
'tPrice' => 18700,
'food_name_1' => 'Black Coffee',
'food_quanty_1' => 1,
'food_price_1' => 1000,
'food_name_2' => 'Sub Combo',
'food_quanty_2' => 2,
'food_price_2' => 3000
];
$arr2 = array();
foreach( $arr as $key => $value )
{
$explode = explode( '_', $key );
if( ctype_digit( $index = end( $explode ) ) === true)
{
if( isset( $arr2[ $index ] ) === false )
{
$arr2[ $index ] = array();
}
$arr2[ $index ][ substr( $key, 0, strlen( $key ) - 1 - strlen( $index ) ) ] = $value;
}
}
print_r( $arr2 );
Output:
Array
(
[1] => Array
(
[food_name] => Black Coffee
[food_quanty] => 1
[food_price] => 1000
)
[2] => Array
(
[food_name] => Sub Combo
[food_quanty] => 2
[food_price] => 3000
)
)
If you already know that you need always the same indexes from $arr, you can use the array_keys function in order to index the associative array.
Example:
$keys = array_keys($arr);
echo '<br><br>'.$arr[$keys[1]];
Here is a complete example:
$keys = array_keys($arr); #stores the associative keys by index
$arr2 = array();
/* For each array $arr do the following */
$limit = 5; #substitute this with: count($arraylist)
for( $n_array=0; $n_array<limit; $n_array++ ){
$cur_arr = array(); #substitute with your: $arraylist[ $n_array ]
for( $a = 2; $a<count($arr); $a++ ){
$cur_arr[ $keys[$a] ] = $arr[ $keys[$a] ];
}
$arr2[ $n_array ] = $cur_arr;
}
Hope it will be helpful

Extracting value from multidimensional array

$arrayDif = array();
$arrayDif[] = array('employer' => $employer,
'comment' => $comment,
'value' => $resultValue);
Filling in from a loop.
Below is the array that I have filled up. I need to be able to find a match by the 'employer' and 'comment' and extract the value, so I can re-update this value.
Array
(
[0] => Array
(
[employer] => Albury-Wodonga
[comment] => allOtherMembers
[value] => 7
)
[1] => Array
(
[employer] => Albury-Wodonga
[comment] => associateMembers
[value] => 1
)
One command to extract and re-update the value, I suggest to use foreach loop
<?php
$arrayDif = array();
$arrayDif[] = array('employer' => "AAA", 'comment' => "comment 1", 'value' => "1");
$arrayDif[] = array('employer' => "BBB", 'comment' => "comment 2", 'value' => "2");
$arrayDif[] = array('employer' => "CCC", 'comment' => "comment 3", 'value' => "3");
// function for setting the value or returning the value
// notice the $array here is a reference to the real array
function func(&$array, $employer, $comment, $value = ''){
// $v is also a reference
foreach ($array as $k => &$v) {
if($v['employer'] == $employer && $v['comment'] == $comment) {
if(empty($value)) {
return $v['value'];
} else {
$v['value'] = $value;
}
}
}
return "Not Found.";
}
//update
func($arrayDif, 'AAA', 'comment 1', "123123");
//search
echo func($arrayDif, 'AAA', 'comment 1');
?>
Not sure if this is what you are looking for but here you go. I would use a function and simple loop.
<?php
$arrayDif = array();
$arrayDif[] = array(
array('employer' => "Albury-Wodonga", 'comment' => "allOtherMembers", 'value' => "1 star"),
array('employer' => "Employer2", 'comment' => "Good Job", 'value' => "2 stars"),
array('employer' => "Employer3", 'comment' => "Smart", 'value' => "3 stars")
);
// Function for searching the array for the matches and returning the value of the match.
function SearchMe($array, $searchEmployer, $searchComment){
for($i = 0; $i < count($array); $i++){
for($j = 0; $j < count($array[$i]); $j++){
if(
$array[$i][$j]["employer"] == $searchEmployer &&
$array[$i][$j]["comment"] == $searchComment
){
return $array[$i][$j]["value"];
}
}
}
return "No Match";
}
echo SearchMe($arrayDif, "Albury-Wodonga", "allOtherMembers");
?>

Combine multidimentional array and ignore the array inside

I have a multidimentional array below in php.
$resultdata[0] = array(
"daynumber" => 15,
"dayname" =>'Tue',
"infomation" => array('baller', 'ROller')
);
$resultdata[1] = array(
"daynumber" => 15,
"dayname" =>'Tue',
"infomation" => array('nomal', 'Goildt')
);
$resultdata[2] = array(
"daynumber" => 24,
"dayname" =>'Thur',
"infomation" => array('Volley', 'Foller')
);
I want to combine the similar dates to form the following result.
$resultdata[0] = array(
"daynumber" => 15,
"dayname" =>'Tue',
"infomation" => array('baller', 'ROller'), array('nomal', 'Goildt')
);
$resultdata[1] = array(
"daynumber" => 24,
"dayname" =>'Thur',
"infomation" => array('Volley', 'Foller')
);
Thanx in advance
I tried using this code but it requies me to convert array to string which i dont want
function combineAndIgnore($result_arr){
$arr = array();
foreach($result_arr as $val){
$item = $val[$key];
foreach($val as $k=>$v){
$arr[$item][$k][] = $v;
}
}
// Combine unique entries into a single array
// and non-unique entries into a single element
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
$field = array_unique($v);
if(count($field) == 1){
$field = array_values($field);
$field = $field[0];
$arr[$key][$k] = $field;
} else {
$arr[$key][$k] = $field;
}
}
}
return $arr;
}
I gave in to the convert to sting option which has worked anyway
<?php
$resultdata[0] = array(
"daynumber" => 15,
"dayname" =>'Tue',
"infomation" => array('baller', 'ROller','','gtk')
);
$resultdata[1] = array(
"daynumber" => 15,
"dayname" =>'Tue',
"infomation" => array('nomal', 'Goildt')
);
$resultdata[2] = array(
"daynumber" => 24,
"dayname" =>'Thur',
"infomation" => array('goon' => 'Volley', 'Foller')
);
$counter = 0;
foreach($resultdata as $veliu){
$newinfo = implode(':*:', $veliu["infomation"]);
$veliu["infomation"] = $newinfo;
$temporryry[$counter] = $veliu;
$counter++;
}
function multiarray_merge($result_arr, $key){
foreach($result_arr as $val){
$item = $val[$key];
foreach($val as $k=>$v){
$arr[$item][$k][] = $v;
}
}
// Combine unique entries into a single array
// and non-unique entries into a single element
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
$field = array_unique($v);
if(count($field) == 1){
$field = array_values($field);
$field = $field[0];
$arr[$key][$k] = $field;
} else {
$arr[$key][$k] = $field;
}
}
}
return $arr;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet">
<style type="text/stylesheet">
font-family: 'Varela Round', sans-serif;
</style>
</head>
<body>
<pre>
<?php
$newarray = multiarray_merge($temporryry, 'daynumber');
$count = 0;
$counte = 0;
foreach($newarray as $vel){
$notin = $vel["infomation"];
if(is_array($notin)){
foreach($notin as $jin){
$reve = explode(":*:", $jin);
$good[$counte] = $reve;
$counte++;
}
}else{
$reve = explode(":*:", $notin);
$good= $reve;
}
$vel["infomation"] = $good;
$finalsolution[$count] = $vel;
$count++;
}
print_r($finalsolution);
?>
</pre>
</body>
</html>
RESULT
Array
(
[0] => Array
(
[daynumber] => 15
[dayname] => Tue
[infomation] => Array
(
[0] => Array
(
[0] => baller
[1] => ROller
[2] =>
[3] => gtk
)
[1] => Array
(
[0] => nomal
[1] => Goildt
)
)
)
[1] => Array
(
[daynumber] => 24
[dayname] => Thur
[infomation] => Array
(
[0] => Volley
[1] => Foller
)
)
)
Here's a quick-and-dirty solution to your unique use-case which you may as well Quick-T3st here:
<?php
$strJson = '[
{
"daynumber" :15,
"dayname" :"Tue",
"infomation" :["baller","ROller"]
},
{
"daynumber" :15,
"dayname" :"Tue",
"infomation" :["nomal","Goildt"]
},
{
"daynumber" :24,
"dayname" :"Thur",
"infomation" :["Volley","Foller"]
}
]';
function arrayBlend($jsonData){
$resultData = json_decode($jsonData, true);
$arrAll = [];
foreach($resultData as $data){
if(is_array($data)){
$arrAll[] = $data;
}
}
foreach($arrAll as $iCount=>&$arr){
$dayNum = $arr['daynumber'];
$dayName = $arr['dayname'];
$prev = ($iCount>0) ? $arrAll[$iCount-1] : null;
if($prev['daynumber'] == $dayNum && $prev['dayname'] == $dayName){
$arr['infomation'] = [ $prev['infomation'], $arr['infomation']];
unset($arrAll[$iCount-1]);
}
}
return $arrAll;
}
var_dump( arrayBlend($strJson) );
The var_dump() above yieds:
array:2 [
1 => array:3 [
"daynumber" => 15
"dayname" => "Tue"
"infomation" => array:2 [
0 => array:2 [
0 => "baller"
1 => "ROller"
]
1 => array:2 [
0 => "nomal"
1 => "Goildt"
]
]
]
2 => array:3 [
"daynumber" => 24
"dayname" => "Thur"
"infomation" => array:2 [
0 => "Volley"
1 => "Foller"
]
]
]

remove duplicate array php

I have script like this
foreach ($onerow as $onekey => $dt ){
$arr = array();
foreach($row as $classkey => $classfoto):
$cek_class_foto = explode("/",$classfoto->name);
if($dt->foto_naam!=$cek_class_foto[1]){
$arr = array($dt->foto_naam);
print_r(array_unique($arr));
}
endforeach;
}
the output like this
Array ( [0] => _b101203.jpg )
Array ( [0] => _b101203.jpg )
my question is, how to remove this duplicates array?
Thank you
You are overwriting $arr for each iteration.
foreach ($onerow as $onekey => $dt ){
$arr = array();
foreach($row as $classkey => $classfoto):
$cek_class_foto = explode("/",$classfoto->name);
if($dt->foto_naam!=$cek_class_foto[1]){
$arr[] =$dt->foto_naam;
}
endforeach;
}
$arr = array_unique($arr);
$array1 = Array
(
'0' => Array
(
'messageId' => 9,
'userId' => 47,
'petId' => 68,
'message' => 'how hello',
'senderId' => 48,
'senderPetId' => 66,
'messageCreateTime' => '2015-07-31 11:44:59'
),
'1' => Array
(
'messageId' => 8,
'userId' => 49,
'petId' => 69,
'message' => 'pppppppppp',
'senderId' => 48,
'senderPetId' => 67,
'messageCreateTime' => '2015-07-31 11:15:16'
),
'2' => Array
(
'messageId' => 6,
'userId' => 48,
'petId' => 67,
'message' => 'gggg',
'senderId' => 49,
'senderPetId' => 69,
'messageCreateTime' => '2015-07-31 11:13:42'
),
'3' => Array
(
'messageId' => 2,
'userId' => 48,
'petId' => 67,
'message' => 'aaaaaaaa',
'senderId' => 47,
'senderPetId' => 68,
'messageCreateTime' => '2015-07-31 11:15:33'
)
);
/* code for removing last duplicate array within result array by KS */
/* matching between : userId, petId, senderId, senderPetId */
$temp_array = array();
$i = 0;
foreach($array1 as $key=>$value)
{
$FLAG = FALSE;
$temp_array = $array1[$key];
if($i==0)
{
$final_array[] = $temp_array;
}
else
{
for($j=0;$j<count($final_array);$j++)
{
if(($final_array[$j]['userId']==$temp_array['userId'] && $final_array[$j]['petId']==$temp_array['petId'] && $final_array[$j]['senderId']==$temp_array['senderId'] && $final_array[$j]['senderPetId']==$temp_array['senderPetId']) ||
($final_array[$j]['userId']==$temp_array['senderId'] && $final_array[$j]['petId']==$temp_array['senderPetId'] && $final_array[$j]['senderId']==$temp_array['userId'] && $final_array[$j]['senderPetId']==$temp_array['petId']))
{
$FLAG = TRUE;
}
}
if($FLAG == FALSE){
$final_array[] = $temp_array;
}
}
$i++;
}
print('<pre>');
print_r($final_array);
enter code here

Categories