$a = ["a","b","c","a","b"];
$b = ["a","c"];
array_diff($a,$b);
//output will be [b,b]
but that is not a proper difference it can also be
//output [b,a,b] <- this is what I am trying to achieve
I tried foreach loop and for loop but failed to get it...
Foreach example I tried
$a = ["a","b","c","a","b"];
$b = ["a","c"];
echo array_diff_custom($a,$b),"<br>";
function array_diff_custom($a,$b){
$result =0;
foreach($a as $key=>$val){
foreach($b as $key2=>$val2){
if($val == $val2){
unset($a[$key]);
}
}
}
$result = count($a);
return $result;
}
echo array_diff_custom($b,$a);
for loop example, I tried
$a = ["a","b","c","a","b"];
$b = ["a","c"];
echo array_diff_custom($a,$b),"<br>";
function array_diff_custom($a,$b){
$result =0;
for($i=0;$i<count($a);$i++){
for($j=0;$j<count($b);$j++){
//echo $a[$i]."-".$b[$j]."<br>";
if($a[$i] == $b[$j]){
unset($a[$i]);
}
}
}
$result = count($a);
return $result;
}
echo array_diff_custom($b,$a);
I am using count($resut) in example function I created but you can just simply return $a and can print_R(array_Diff_custom)
to check the output...
You can just unset items, presented in the 2nd array, from the first one only once
function array_diff_custom($a,$b){
foreach($b as $x){
if($k = array_keys($a, $x)) {
unset($a[$k[0]]);
}
}
return $a;
}
print_r(array_diff_custom($a,$b));
demo
Related
$a = ['Ava', 'Emma', 'Olivia']; $b = ['Olivia', 'Sophia', 'Emma'];
I want the output to be ['Emma', 'Olivia', 'Ava', 'Sophia'] in any particular order without using array functions.
This is what i tried
<?php
//function unique_names($a,$b){
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$z= $a;
$c = count($b);
$d = count($a);
//loop for b
$e = 0;
for($i=0;$i<$c;$i++){ //b
for($j=0;$j<$d;$j++){
if($b[$i] != $a[$j]){
$z[$d+1] = $b[$i];
break;
}else{
//$z[$e] = $a[$j];
}
}
}
echo"<pre>ans";print_r($z);
die;
//return $z;
//}
//echo"<pre>ans"; print_r(unique_names($a,$b));
?>
Also i made it work using in_array but was later told that even that function is not allowed.
<?php
function unique_names($a,$b){
$z= $a;
$c = count($b);
$d = count($a);
for($i=0;$i<$c;$i++){
if(! in_array($b[$i], $a)){
$z[$d+1] = $b[$i];
}
}
return $z;
}
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
print_r(unique_names($a,$b));
?>
You can use next code:
<?php
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$values = [];
// Add values from first array
foreach($a as $v) {
$values[$v] = true;
}
// Add values from second array
// all exists names will be overwrited
// new values will be addded
foreach($b as $v) {
$values[$v] = true;
}
// Transform keys to plain result
foreach($values as $key=>$val) {
$result[] = $key;
}
var_dump($result);
Execute PHP online
I want to combine two arrays into a dictionary.
The keys will be the distinct values of the first array, the values will be all values from the second array, at matching index positions of the key.
<?php
$a=[2,3,4,5,6,7,8,9,10];
$b=[1,1,3,2,1,2,6,8,8];
?>
array_combine($b,$a);
Expected result as
<?php
/*
Value '1' occurs at index 0, 1 and 4 in $b
Those indices map to values 2, 3 and 6 in $a
*/
$result=[1=>[2,3,6],3=>4,2=>[5,7],6=>8,8=>[9,10]];
?>
There are quite a few PHP array functions. I'm not aware of one that solves your specific problem. you might be able to use some combination of built in php array functions but it might take you a while to weed through your choices and put them together in the correct way. I would just write my own function.
Something like this:
function myCustomArrayFormatter($array1, $array2) {
$result = array();
$num_occurrences = array_count_values($array1);
foreach ($array1 AS $key => $var) {
if ($num_occurrences[$var] > 1) {
$result[$var][] = $array2[$key];
} else {
$result[$var] = $array2[$key];
}
}
return $result;
}
hope that helps.
$a=[2,3,4,5,6,7,8,9,10];
$b=[1,1,3,2,1,2,6,8,8];
$results = array();
for ($x = 0; $x < count($b); $x++) {
$index = $b[$x];
if(array_key_exists ($index, $results)){
$temp = $results[$index];
}else{
$temp = array();
}
$temp[] = $a[$x];
$results[$index] = $temp;
}
print_r($results);
Here's one way to do this:
$res = [];
foreach ($b as $b_index => $b_val) {
if (!empty($res[$b_val])) {
if (is_array($res[$b_val])) {
$res[$b_val][] = $a[$b_index];
} else {
$res[$b_val] = [$res[$b_val], $a[$b_index]];
}
} else {
$res[$b_val] = $a[$b_index];
}
}
var_dump($res);
UPDATE: another way to do this:
$val_to_index = array_combine($a, $b);
$result = [];
foreach ($val_to_index as $value => $index) {
if(empty($result[$index])){
$result[$index] = $value;
} else if(is_array($result[$index])){
$result[$index][] = $value;
} else {
$result[$index] = [$result[$index], $value];
}
}
var_dump($result);
Let's say I want to check for simple mathematical progression. I understand I can do it like this:
if ($a<$b and $b<$c and $c<$d and $d<$e and $e<$f) { echo OK; }
Is there a way to do it in a more convenient way? Like so
if ($a..$f isprog(<)) { echo OK; }
I don 't know if I get your problem right. But propably the solution for your progression could be the SplHeap object of the SPL delivered with php.
$stack = new SplMaxHeap();
$stack->insert(1);
$stack->insert(3);
$stack->insert(2);
$stack->insert(4);
$stack->insert(5);
foreach ($stack as $value) {
echo $value . "\n";
}
// output will be: 5, 4, 3, 2, 1
I havent heard of something like this, but how about using simple function:
function checkProgress($vars){ //to make it easie i assume that vars can be given in an array
$result = true;
for ($i=0; $i<= count($vars); $i++){
if ($i>0 && $vars[$i] > $vars[$i-1]) continue;
$result = false;
}
return $result;
}
Solved it quick and dirty:
function ispositiveprogression($vars) {
$num=count($vars)-1;
while ($num) {
$result = true;
if ($vars[$num] > $vars[$num-1]) {
$num--;
}
else { $result = false; break; }
}
return $result;
}
Create an array of values, iterate over them and maintaining a flag that checks if the current element value is greater than / less than that of the next value. Unlike some of the solutions in this thread, this doesn't loop through the whole array. It stops looping when it discovers the first value that's not a progression. This will be a lot more faster if the operation involves a lot of numbers.
function checkIfProg($arr, $compare) {
$flag = true;
for ($i = 0, $c = count($arr); $i < $c; $i++) {
if ($compare == '<') {
if (isset($arr[$i + 1]) && $arr[$i] > $arr[$i + 1]) {
$flag = false;
break;
}
} elseif ($compare == '>') {
if (isset($arr[$i + 1]) && $arr[$i] < $arr[$i + 1]) {
$flag = false;
break;
}
}
}
return $flag;
}
Usage:
$a = 2;
$b = 3;
$c = 4;
$d = 5;
$e = 9;
$f = 22;
$arr = array($a, $b, $c, $d, $e, $f);
var_dump(checkIfProg($arr, '<')); // => bool(true)
If you want the array to be created dynamically, you could use some variable variable magic to achieve this:
$arr = array();
foreach (range('a','f') as $v) {
$arr[] = $$v;
}
This will create an array containing all the values of variables from $a ... $f.
For example, I have a function like this:
function loopValues()
{
$a = array('a','b','c');
foreach($a as $b)
{
$c = $b.'e';
echo $c;
}
}
How could I return its value aebece in an array like ('ae','be','ce')?
$a = array('a','b','c');
$b = array_map(function($ele) {
return $ele .= 'e';
}, $a);
See it in action
Try
function loopValues()
{
$a = array('a','b','c');
$result = array();
foreach($a as $b){
$result[] = $b.'e';
}
return $result;
}
$r = loopValues();
print_r($r);
See demo here
Simple, try this:
function loopValues()
{
$a = array('a','b','c');
$r = array();
foreach($a as $b)
{
$c = $b.'e';
$r[] = $c;
}
return $r;
}
function loopValues(){
$a = array('a','b','c');
for($i=0;$i<count($a);$i++){
$a[$i] .= 'e';
}
return $a;
}
Instead of just 1, how can I pick the 4 highest values from an array using max()?
You could use an SplMaxHeap
function maxN(array $numbers, $n)
{
$maxHeap = new SplMaxHeap;
foreach($numbers as $number) {
$maxHeap->insert($number);
}
return iterator_to_array(
new LimitIterator($maxHeap, 0, $n)
);
}
Usage (demo):
print_r( maxN( array(7,54,2,4,26,7,82,4,34), 4 ) );
You could try this:
$a = array(3,5,6,1,23,6,78,99);
asort($a);
var_dump(array_slice($a, -4));
HTH.
This will do it in Θ(n) time:
$a = $b = $c = $d = null;
foreach($array as $v) {
if(!isset($a) || $v > $a) {
$d = $c;
$c = $b;
$b = $a;
$a = $v;
}elseif(!isset($b) || $v > $b) {
$d = $c;
$c = $b;
$b = $v;
}elseif(!isset($c) || $v > $c) {
$d = $c;
$c = $v;
}elseif(!isset($d) || $v > $d) {
$d = $v;
}
}
$result = array($a, $b, $c, $d);
function maxs($ar, $count=4)
{
$res = array();
foreach ($ar as $v)
{
for ($i = 0;$i < $count;$i++)
{
if ($i >= count($res) || $v > $res[$i])
{
do
{
$tmp = $res[$i];
$res[$i] = $v;
$v = $tmp;
$i++;
}
while ($i < $count);
break;
}
}
}
return $res;
}
A simple method using php predefined functions.
<?php
$arr = array(6, 8, 3, 2, 7, 9);
rsort($arr);
$first = array_shift($arr);
$second = array_shift($arr);
$third = array_shift($arr);
echo $first; // print 9
echo $second; // print 8
echo $third; // print 7
?>
While storing itself you can maintain another array as soon as the new item is inserted check with the max value in the inner array if the item being inserted is greater insert this item. During the item pop do viceversa. From the inner maintained array you can get as many max numbers as possible.