subtracting array values from another unequal array - php

I have the following which works just fine when the arrays are of an equal length:
(example)
$highNums = array(10,20,30,40,50,60);
$lowNums = array(0,1,2,3,4,5);
$result = array();
for($i=0;$i<count($highNums);$i++)
{
$result[$i] = $highNums[$i]-$lowNums[$i];
}
The problem lies in that the array keys are dates (months) pulled from the database and where there is, say, 'january' and a value in the $lowNums array there won't always be a 'january' record in the $highNums.
Is there any way to detect any missing values in each array and fill them with 0?
}

foreach ($highNums as $key=>$val) {
if(array_key_exists($key, $lowNums)){
$result[$key] = $highNums[$key]-$lowNums[$key];
}else{
$result[$key]=0;
}
}

Related

Randomly Unset Element in Multidimensional Array if key-value Exists

I have a multidimensional array in PHP taking on the form of the following:
$data = array(
array('spot'=>1,'name'=>'item_1'),
array('spot'=>2,'name'=>'item_2'),
array('spot'=>1,'name'=>'item_3'),
);
If more than one array element contains a duplicate for the 'spot' number I would want to randomly select a single one and unset all other elements with the same 'spot' value. What would be the most efficient way to execute this? The resulting array would look like:
$data = array(
array('spot'=>2,'name'=>'item_2'),
array('spot'=>1,'name'=>'item_3'),
);
Store the values of spot in another array. Using array_count_values check which values occur more than once. Find the keys for those values. Select a random key. Delete all keys except the selected key from the original array. Here is the code:
$data = array(
array('spot'=>1,'name'=>'item_1'),
array('spot'=>2,'name'=>'item_2'),
array('spot'=>1,'name'=>'item_3'),
);
$arr = array();
foreach($data as $val){
$arr[] = $val['spot'];
}
foreach(array_count_values($arr) as $x => $y){
if($y == 1) continue;
$keys = array_keys($arr, $x);
$rand = $keys[array_rand($keys)];
foreach($keys as $key){
if($key == $rand) continue;
unset($data[$key]);
}
}

Replace duplicate words in array (not remove)

I have an array that have multiple set of words, some of them might be duplicated, and i want to replace the duplicated words from array with word: duplicate, and also keep one original.
So if i have 5 duplicates, i want 4 of them to be replaced with duplicate and keep original one
$my_array = (0=>'test', 1=>'test2',2=>'test3',3=>'test');
As you see in my array, the array keys 0 and 3 has same value, i want to replace the last value with word 'duplicate'
$my_array = (0=>'test', 1=>'test2',2=>'test3',3=>'duplicate');
I tried different methods but without success:(
Here's one way to do it:
<?php
$my_array = array(0=>'a', 1=>'a',2=>'b',3=>'c');
print_r($my_array);
$my_array2 = array_unique($my_array);
foreach($my_array as $key => $value) {
if (!array_key_exists($key, $my_array2)) {
$my_array[$key] = 'duplicate';
}
}
print_r($my_array);
Try this, just remember what values you have visited.
$visited = array();
foreach($my_array as $key=>$val) {
if(isset($visited[$val])) {
$my_array[$key] = 'duplicate';
} else {
$visited[$val] = true;
}
}

Removing successive duplicate occurrences in an array

Is there any way that I can remove the successive duplicates from the array below while only keeping the first one?
The array is shown below:
$a=array("1"=>"go","2"=>"stop","3"=>"stop","4"=>"stop","5"=>"stop","6"=>"go","7"=>"go","8"=>"stop");
What I want is to have an array that contains:
$a=array("1"=>"go","2"=>"stop","3"=>"go","7"=>"stop");
Successive duplicates? I don't know about native functions, but this one works. Well almost. Think I understood it wrong. In my function the 7 => "go" is a duplicate of 6 => "go", and 8 => "stop" is the new value...?
function filterSuccessiveDuplicates($array)
{
$result = array();
$lastValue = null;
foreach ($array as $key => $value) {
// Only add non-duplicate successive values
if ($value !== $lastValue) {
$result[$key] = $value;
}
$lastValue = $value;
}
return $result;
}
You can just do something like:
if(current($a) !== $new_val)
$a[] = $new_val;
Assuming you're not manipulating that array in between you can use current() it's more efficient than counting it each time to check the value at count($a)-1

Compare two arrays and merging in PHP

I have two arrays:
$array1 = array(1=>1,10=>1,12=>0,13=>13);
$array2 = array(1=>"Hello",10=>"Test",12=>"check",13=>"error");
Here $array1 has keys and values. Now I want to take the first value from $array1(as 1) and I want to check if this is repeated in this array .
Here 1 is repeated two times so I want to take the two keys 1,10 and display the corresponding values of these keys from $array2. If the value in $array1 is not repeated then I want to just display the value of this key from $array2.
I want to get the output as follows:
Hello Test
check
error
That means in $array1 1,10 keys have the same value so the value of 1 and the value of 10 from $array2 is merged then displayed.
Like 12 has 0 this is not repeated so simply take value of 12 from $array2.
Like 13.
How can I do this?
<?php
$array1 = array(1=>1,10=>1,12=>0,13=>13);
$array2 = array(1=>"Hello",10=>"Test",12=>"check",13=>"error");
$groupedKeys = array();
foreach($array1 as $key=>$arr){
$groupedKeys[$arr][] = $key;
}
foreach($groupedKeys as $key => $groupedKeyArr){
foreach($groupedKeyArr as $groupedKey){
echo $array2[$groupedKey];
}
echo "<br /> ";
}
?>
http://codepad.org/9R9s5lTM
There is a built in function that returns an array with the number of times a value is repeated http://php.net/manual/en/function.array-count-values.php
This is really rough, but a simple way of doing it could be:
<?
$array1 = array(1=>1,10=>1,12=>0,13=>13);
$array2 = array(1=>"Hello",10=>"Test",12=>"check",13=>"error");
$prev = $array1[1];
foreach($array1 as $key => $val)
{
if($val != $prev && $key != 1)
{
echo '<br />';
}
echo $array2[$key].' ';
$prev = $val;
}
?>
Example: http://codepad.org/OpLdtStp
This assumes that you're first key is always going to be 1 by the way.
I am providing you a function returns an array with the number of times a value is repeated in an array(as values) and values as the keys. Further task is not difficult.
function check_number_of_times_elements_occur_in_array($a)//returns values of array as keys, associating values being their total occurences in the array
{
$r=array();
foreach($a as $v)
++$r[$v];
return $r;
}
I think this will do for you..
function test($array1,$array2) {
$repeated_values = array_count_values($array1);
foreach($repeated_values as $key => $value){
if($value > 1) {
foreach($array1 as $key1 => $value1){
if($key == $value1){
$repeated_values_keys[] = $key1;
}
}
}
}
$str_top = "";
foreach($repeated_values_keys as $k){
$str_top .= $array2[$k]." ";
}
echo $str_top.'<br/>';
foreach($array2 as $key2 => $value){
if(!in_array($key2,$repeated_values_keys)){
echo $value.'<br/>';
}
}
}

Comparing arrays and giving out numbers depending on what value is in what array

This is more a kind of logical Question. Sometimes i think my Brain is not for programming ;(
What i want to do is.
IF array1 and array2 have the same values setup a new array with keyname to the value that the both array have in common and value = 3 OK i already got that.
Now i want:
IF a value is ONLY in array1 set new array value = 1
IF a value is ONLY in array2 set new array value = 2
$beidesgeht = array_intersect($acc_conf, $ano_conf);
foreach ( $beidesgeht as $be ) {
$fertig[ $be ] = 3;
}
I guess thats a easy one for you pros. ;)
After what you've got up there:
foreach ($acc_conf as $el) {
if (!isSet($fertig[$el])) {
$fertig[$el] = 1;
}
}
foreach ($ano_conf as $el) {
if (!isSet($fertig[$el])) {
$fertig[$el] = 2;
}
}

Categories