I have two arrays like below :
Array_1 : ($array1 - after print the array)
Array ( [companyid] => 589 [company_name] => log_tracking_24 [username] => admin#log24.com [emp_count] => 0 [user_id] => 764 [module_expenses_benefit] => 1 [module_time_attendance] => 1 [module_bank_ftp] => 0 )
Array_2 : ($array2 - after print the array )
Array ( [company_name] => log_tracking_241 [username] => admin#log241.com [password] => [cpassword] => [emp_count] => [module_expenses_benefit] => on [user_id] => 764 [companyid] => 589 )
I compare the two arrays and get different values . for this i try the following,
array_diff($array1,$array2);
Finally i get the following result :
Array ( [company_name] => log_tracking_24 [username] => admin#log24.com [emp_count] => 0 [module_expenses_benefit] => 1 [module_time_attendance] => 1 [module_bank_ftp] => 0 )
but what i want is ,
In my first array : $array1['emp_count'] having the value 0 and my second array : $array2['emp_count'] having the value Null ( '' ).
In this type of situation i want to remove the key and vlaue while the time of array_diff.
How to do this. i try the unset() function . but no use.
You can use array_filter to remove empty elements:
$emptyRemoved = array_filter($linksArray);
To remove value 0 and empty. you may use the following:
$emptyRemoved = remove_empty($linksArray);
function remove_empty($array) {
return array_filter($array, '_remove_zero');
}
function _remove_zero($value) {
return !empty($value) && $value === 0;
}
Pass your both array to remove_empty() function then use array_diff()
$arr1 = remove_empty($array1);
$arr2 = remove_empty($array2);
array_diff($arr1, $arr2);
Yes array_filter did the work for you. Just use:
array_diff(array_filter($array1), array_filter($array2));
See the example:
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => 0
);
print_r(array_filter($entry)); //Array ( [0] => foo [2] => -1 )
So array_filter removes the false, null, ''and 0.
Related
This is how my array looks like :
Array
(
[0] => Array
(
[unit] => 10
[harga] => 15000
)
[1] => Array
(
[unit] => 7
[harga] => 10000
)
[2] => Array
(
[unit] => 12
[harga] => 123123
)
)
I want to unset the 0 key array when the unit is 0 and rearrange the key so the 1 key will replace the 0.
This is how I do it :
$jumlah_penjualan = $data - > unit;
while ($jumlah_penjualan > 0) {
$persediaan_pertama = $persediaan[0]['unit'];
$harga_persediaan = $persediaan[0]['harga'];
if ($persediaan_pertama < $jumlah_penjualan) {
$dijual = $persediaan_pertama;
$penjualan[] = array(
'unit' => $dijual,
'harga' => $harga_persediaan,
'total' => $dijual * $harga_persediaan);
$persediaan[0]['unit'] = $persediaan[0]['unit'] - $dijual;
} else {
$dijual = $jumlah_penjualan;
$penjualan[] = array(
'unit' => $dijual,
'harga' => $harga_persediaan,
'total' => $dijual * $harga_persediaan);
$persediaan[0]['unit'] = $persediaan[0]['unit'] - $dijual;
}
if ($persediaan[0]['unit'] == 0) {
unset($persediaan[0]);
$persediaan = array_values($persediaan);
}
$jumlah_penjualan = $jumlah_penjualan - $dijual;
}
But the result looks like it continues looping before rearranging the array.
This is how the array should look like after unset:
Array(
[0] => Array
(
[unit] => 9
[harga] => 123123
)
)
If you want to remove the first elements until the unit is not 0, you can
$arr = array
(
array
(
"unit" => 0,
"harga" => 15000
),
array
(
"unit" => 0,
"harga" => 10000
),
array
(
"unit" => 12,
"harga" => 123123
)
);
while( $arr[0]["unit"] == 0 ) { //Loop until $arr[0]["unit"] is not 0
unset($arr[0]); //Remove $arr[0] since unit is 0
$arr = array_values($arr); //Make Make element 1 to element 0
}
echo "<pre>";
print_r( $arr );
echo "</pre>";
This will result to:
Array
(
[0] => Array
(
[unit] => 12
[harga] => 123123
)
)
To remove the first element of an array and reindex the elements, use array_shift. First, check that the number of units are zero, then remove the first element if that's the case.
if ($arr[0]['unit'] == 0) {
array_shift($arr);
}
Since it's impossible to say what your other variables even mean because of the language, you probably want to move this outside of your while loop, so that the first element is only removed after you've processed the array.
I'm trying to merge/sums 2 arrays that can contain integers or more arrays (themselves containing integer).
When the values are integers, I need to sum them in the final array.
When the values are arrays, I need to loop through the values and sum them in the final array.
If a value or a sub-array exists only in 1 of the base array, it needs to be added in the sub-array of the final array. (This is what I can't do)..)
My arrays are like this:
ARRAY 1
[1466859600] => Array
(
[TOTAL] => 27217
[AAA] => Array
(
[FD_CDP] => 1746
[LO_SC_MIC] => 4654
[FD_ATS] => 893
[CDP] => 40
[SUPERVISION] => 9
[CONTROL] => 4
[ATS] => 4
[EVT_ACK] => 3
)
[BBB] => Array
(
[FD_CDP] => 1376
[LO_SC_MIC] => 4606
[FD_ATS] => 826
[FD_ATSS] => 451
[LO_SFRC] => 4
[FD_S2] => 259
[2_LOSC] => 2
)
[CCC] => Array
(
[FD_CDP] => 1333
[LO_SC_MIC] => 4725
[FD_ATS] => 856
[CONTROL] => 4
[ATS] => 2
[EVT_ACK] => 5
)
ARRAY 2
[1466859600] => Array
(
[TOTAL] => 95406
[AAA] => Array
(
[FD_ATSS] => 1719
[LO_SC_MIC] => 16830
[CONTROL] => 16
[NEW] => 7
[NOEL] => 206
)
[BBB] => Array
(
[SUPERVISION] => 23
[CDP] => 158
[CONTROL] => 40
[2_LOSC] => 14
[ATS] => 6
[EVT_ACK] => 4
)
[CCC] => Array
(
[EVT_ACK] => 167
[LO_SFRC] => 248
[SUPERVISION] => 23
)
I wrote a function like this :
function sumArrayValues($array1, $array2)
{
foreach ($array1 as $key => $value)
{
if (is_array($array1[$key]))
{
echo "it's an array\n I need to reloop\n";
sumArrayValues($array1[$key], $array2[$key]);
}
else
{
echo "FIRST VALUE TO SUM\n";
print_r($array1[$key]."\n");
echo "SECOND VALUE TO SUM\n";
print_r($array2[$key]."\n");
$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
echo "--------RESULT of SUM array1&2----------\n";
}
}
return $array1;
}
But this function doesn't take into account 2 (and probably more) cases: if the sub-array are not in the same order, if a sub-array or a value only exist in second array.
A example of function would be a good help, but on a more fundamental level, I even can't figure the algorithm to do that.
Any ideas ?
You can get all the keys for the foreach loop, live demo.
Note, you also can check if a key of any array is undefined, then save the defined value for the key.
function sumArrayValues($array1, $array2)
{
$keys = array_keys($array1 + $array2);
foreach ($keys as $key)
{
if (is_array($array1[$key]) || is_array($array2[$key]))
$array1[$key] = sumArrayValues($array1[$key], $array2[$key]);
else
#$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
}
return $array1;
}
I have 2 arrays: Array1 and Array2. As you can see in Array1 I have 2 duplicate values. So what I want to do, is unset one of dublicates (doesn't matter which one) and as a result I need to unset value from Array2 with the same key as already unset value in Array1
Array1
(
[0] => 1331-14-2-45
[1] => 1344-1-4-22
**[2] => 1409-1-1-4**
[4] => 1312-14-1-23
**[5] => 1409-1-1-4**
[6] => 1365-10-3-30
)
AND
Array2
(
[0] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[1] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[2] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[4] => deviceNotActive#nemodel.GPON.4.6
[5] => deviceNotActive#nemodel.GPON.4.6
[6] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
)
<?php
$array1 = array
(
0 => '1331-14-2-45',
1 => '1344-1-4-22',
2 => '1409-1-1-4',
4 => '1312-14-1-23',
5 => '1409-1-1-4',
6 => '1365-10-3-30',
);
$array1_tmp = $array1;
$array2 = array
(
0 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
1 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
2 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
4 => 'deviceNotActive#nemodel.GPON.4.6',
5 => 'deviceNotActive#nemodel.GPON.4.6',
6 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
);
$array1 = array_unique($array1);
$remove_keys = array_keys(array_diff_key($array1_tmp, $array1));
foreach($remove_keys as $k => $v) {
unset($array2[$v]);
}
You can use the array_unique() function, and maybe take a look at the Array functions list.
Use array_unique function and array_diff
$uniqueValues = array_unique($inputArray); //your first array
$deletedValues = array_diff($inputArray, $uniqueValues);
foreach($deletedValues as $key => $deletedValue){
unset($secondIput[$key]); //you second array
}
I have the following structure for my array which is dynamically generated:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
I would like to update the first array value [friend_request_accepted] => 0 to this: [friend_request_accepted] => 1 which i have achieved by doing the following:
$test = get_user_meta($current_user->ID,'get_user_friends', true );
foreach($test as &$values){
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
}
However, i would like to actually save this new value into the database, overwriting the existing value. The array should then look like this:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 1
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
How should I do this?
this line doesn't work:
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
because
[friend_request_recipient_id] => 5
is an integer I think you just need to sure in condition because '===' is also checked the types of variable.
I have two multi-dimensional array I want to take only those array whose
key values are different from the first array
Here is my two array:
$array1 = Array
(
[0] => Array
(
[id] => 1
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => uouuououou
[job_type] => ouuou
[job_title] => u
[job_appointment_date] => 2013-07-15
[job_duration] => ouu
)
[1] => Array
(
[id] => 2
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => DDC
[job_type] => Manger
[job_title] => Manager
[job_appointment_date] => 2013-07-17
[job_duration] => one year
)
)
and this is my second array
$array2 = Array
(
[0] => Array
(
[id] => 1
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => uouuououou
[job_type] => ouuou
[job_title] => u
[job_appointment_date] => 2013-07-15
[job_duration] => ouu
)
[1] => Array
(
[id] => 2
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => ouo
[job_type] => uououo
[job_title] => udds
[job_appointment_date] => 2013-07-17
[job_duration] => uo
)
);
I tried array_diff and array_diff_assoc it also not worked for me
i get this error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: history/home.php
Line Number: 729
Something like this should get you there, depending on what exactly you want:
$diff = array_udiff($array1, $array2, function (array $a, array $b) {
return (int)array_diff($a, $b);
});
Adjust the comparison function to compare what exactly you want to compare.
http://php.net/array_udiff
foreach, array_unique and possibly array_udiff should help you here.
PHP Manual:
Unique Arrays
Data Comparison
For Each
For a simple array:
$result = array_unique($array);
In your case there's a function from PHP Manual for this:
<?php
function specified_array_unique($array, $value)
{
$count = 0;
foreach($array as $array_key => $array_value)
{
if ( ($count > 0) && ($array_value == $value) )
{
unset($array[$array_key]);
}
if ($array_value == $value) $count++;
}
return array_filter($array);
}
?>
There's been a post that is similar to what you're asking;
Stack Overflow - array_udiff