Simple recursion returns null in PHP [duplicate] - php

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Where am I wrong?
The code looks OK, but the function returns NULL.
$ar = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = 6;
$new_n = chckn($n, $ar);
echo $new_n;
function chckn($n, $ar) {
if(!in_array($n, $ar)) {
echo "===$n===\n";
return $n;
} else {
$n = rand(1, 10);
chckn($n, $ar);
}
}

$ar = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = 6;
$new_n = chckn($n, $ar);
echo $new_n;
function chckn($n, $ar){
if(!in_array($n, $ar)){
//echo "===$n===\n";
return $n;
} else {
$n = rand(1,10);
return chckn($n, $ar);
}
}

Related

How to output all array elements after a certain element?

I have this array:
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
It has 8 value , i want to output all value after the fifth value which is 5.
so I want to output:
6 7 8
This is my code:
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$i = 0;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
Use array_slice()
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$output = array_slice($arr, 0, 3);
Array_slice or starting your loops from the desired index:
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$i = 5;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$arr = array_slice($arr,5);
$i = 0;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
$i = 5;
while ($i < count($arr)) {
$a = $arr[$i];
echo $a ."\n";
$i++;
}
?>
Simply use array_slice along with implode function like as
$arr = array(1, 2, 3, 4, 5, 6, 7, 8);
echo implode("\n",array_slice($arr,5));
If you were testing it within browser you simply replace \n along with <br/>
Demo

finding the index of last occurrence of an element in an array using binary search PHP

The array given has duplicate elements, So basically, I want to find the index of the last occurrence of the element I've searched.
$arr = array(2, 3, 4, 4, 5, 6, 4, 8);
$x = 4; // number to search
$low = 0;
$high = count($arr)-1;
// I want this function to return 6 which is the index of the last occurrence of 4, but instead I'm getting -1 .
function binary_search_last_occ($arr, $x, $low, $high)
{
while ($low <=$high)
{
$mid = floor(($low+$high)/2);
$result = -1;
if ($arr[$mid] == $x)
{
// we want to find last occurrence, so go for
// elements on the right side of the mid
$result = $mid;
$low = $mid+1;
}
else if($arr[$mid] > $x)
{
$high = $mid-1;
}
else
$low = $mid+1;
}
return $result;
}
echo binary_search_last_occ($arr, $x, $low, $high); // outputs -1 ?
I'm not sure, why I'm getting -1. Any suggestions?
I didn't seen your loop but I think this'll really simple to use to gain such functionality
$arr = array(2, 3, 4, 4, 5, 6, 4, 8);
$result = [];
foreach($arr as $key => $val){
$result[$val][] = $key;
}
echo end($result[4]);//6
Or you can simply use the asort function along with array_search like as
$arr = array(2, 3, 4, 4, 5, 6, 4, 8);
asort($arr);
echo array_search(4,$arr);//6
First of all for binary search your array must be sorted, if your array is not sorted you can use simple method like
function binary_search_last_occ($arr, $x, $low, $high)
{
$last_occ = -1;
while ($low <=$high)
{
if($arr[$low] == $x)
$last_occ = $low;
$low++;
}
return $last_occ ;
}
And also define $result above while() to avoid overriding every time with -1. Hence you get the result as -1.
$result = -1;
while ($low <=$high)
{
$mid = floor(($low+$high)/2);
if ($arr[$mid] == $x)
{
// we want to find last occurrence, so go for
// elements on the right side of the mid
$result = $mid;
$low = $mid+1;
}
else if($arr[$mid] > $x)
{
$high = $mid-1;
}
else
$low = $mid+1;
}
return $result;

Remove duplicates of array

I was just going through these questions for PHP and got stuck at one of them. The question is:
You have a PHP 1 dimensional array. Please write a PHP function that
takes 1 array as its parameter and returns an array. The function must
delete values in the input array that shows up 3 times or more?
For example, if you give the function
array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9)the function will returnarray(1, 3, 5, 2, 3, 1, 9)
I was able to check if they are repeating themselves but I apply it to the array I am getting as input.
function removeDuplicate($array){
$result = array_count_values( $array );
$values = implode(" ", array_values($result));
echo $values . "<br>";
}
$qArray = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
removeDuplicate($qArray);
One more thing, we cannot use array_unique because it includes the value which is repeated and in question we totally remove them from the current array.
Assuming the value may not appear 3+ times anywhere in the array:
$array = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
// create array indexed by the numbers to remove
$remove = array_filter(array_count_values($array), function($value) {
return $value >= 3;
});
// filter the original array
$results = array_values(array_filter($array, function($value) use ($remove) {
return !array_key_exists($value, $remove);
}));
If values may not appear 3+ times consecutively:
$results = [];
for ($i = 0, $n = count($array); $i != $n;) {
$p = $i++;
// find repeated characters
while ($i != $n && $array[$p] === $array[$i]) {
++$i;
}
if ($i - $p < 3) {
// add to results
$results += array_fill(count($results), $i - $p, $array[$p]);
}
}
This should work :
function removeDuplicate($array) {
foreach ($array as $key => $val) {
$new[$val] ++;
if ($new[$val] >= 3)
unset($array[$key]);
}
return $array;
}
run this function i hope this help..
function removeDuplicate($array){
$result = array_count_values( $array );
$dub = array();
$answer = array();
foreach($result as $key => $val) {
if($val >= 3) {
$dub[] = $key;
}
}
foreach($array as $val) {
if(!in_array($val, $dub)) {
$answer[] = $val;
}
}
return $answer;
}
You can use this function with any number of occurrences you want - by default 3
function removeDuplicate($arr, $x = 3){
$new = $rem = array();
foreach($arr as $val) {
$new[$val]++;
if($new[$val]>=$x){
$rem[$val]=$new[$val];
}
}
$new = array_keys(array_diff_key($new, $rem));
return $new;
}
I think it is getting correct output. just try once.
$array=array(1,2,3,7,4,4,3,5,5,6,7);
$count=count($array);
$k=array();
for($i=0;$i<=$count;$i++)
{
if(!in_array($array[$i],$k))
{
$k[]=$array[$i];
}
}
array_pop($k);
print_r($k);
in this first $k is an empty array,after that we are inserting values into $k.
You should use array_unique funciton.
<?php
$q = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
print_r(array_unique($q));
?>
Try it and let me know if it worked.

PHP Array, remove only duplicates next to eachother

$arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10);
array_unique creates: array(1, 2, '...', 6, 7, 8, 10);
I want the following: array(1, 2, '...', 6, 7, 8, '...', 10);
So basically, I'm looking for a fast way to remove only duplicates next to eachother.
$result = array();
$first = true;
foreach ($array as $x) {
if ($first || $x !== $previous) $result[] = $x;
$previous = $x;
$first = false;
}
Alter the $x !== $previous condition to suit your preferred definition of "duplicate". For example, array_unique does a loosely-typed comparison.
I would do it this way:
<?php
$arr = array(1, 2, '...', '...', '...', 6, 7, 8, '...', 10);
$el = $arr[0];
$out = $arr;
for ($i=1, $c = count($out); $i<$c; ++$i) {
if ($arr[$i] == $el) {
unset($out[$i]);
}
else {
$el = $out[$i];
}
}
$out = array_values($out);
foreach ($out as $i) {
echo $i."<br />";
}
Output:
1
2
...
6
7
8
...
10

Cumulative array

I have this array:
$a = array(1, 2, 3, 4, 5, 7, 8, 10, 12);
Is there a function to convert this to:
$b = array(1, 1, 1, 1, 2, 1, 2, 2);
So basicaly:
$b = array ($a[1]-$a[0], $a[2]-$a[1], $a[3]-$a[2], ... ,$a[n]-$a[n-1]);
Here is the code I have so far:
$a = $c = array(1, 2, 3, 4, 5, 7, 8, 10, 12);
array_shift($c);
$d = array();
foreach ($a as $key => $value){
$d[$key] = $c[$key]-$value;
}
array_pop($d);
There isn't a built-in function that can do this for you, but you could turn your code into one instead. Also, rather than making a second array, $c, you could use a regular for loop to loop through the values:
function cumulate($array = array()) {
// re-index the array for guaranteed-success with the for-loop
$array = array_values($array);
$cumulated = array();
$count = count($array);
if ($count == 1) {
// there is only a single element in the array; no need to loop through it
return $array;
} else {
// iterate through each element (starting with the second) and subtract
// the prior-element's value from the current
for ($i = 1; $i < $count; $i++) {
$cumulated[] = $array[$i] - $array[$i - 1];
}
}
return $cumulated;
}
I think php has not a build in function for this. There are many ways to solve this, but you already wrote the answer:
$len = count($a);
$b = array();
for ($i = 0; $i < $len - 1; $i++) {
$b[] = $a[$i+1] - $a[$i];
}

Categories