Find to closest higher and lower number in an array in loop - php

I have an array. Given a number X (that must not be contained in the array), I want to search for both the next greater and next lower number of X within the array in a single loop. My code is:
<?php
$a = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
$v = 104;
sort($a);
$nearestGreater = null;
$nearestLower = null;
foreach ($a as $key => $val) {
if ( $v<=$val) {
$nearestGreater = (isset($a[$key + 1])) ? $a[$key + 1]: $nearestGreater;
$nearestLower = (isset($a[$key - 1])) ? $a[$key - 1]: $nearestLower;
break;
}
}
var_dump($nearestLower);
echo "<br/>".$v."<br/>";
var_dump($nearestGreater);
unset($a);
?>

Write your code in clean and readable way using Code Block
$nearestGreater=null;
$nearestLower = null;
$a = array(1, 8, 23, 25, 40,41,22,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
$v = 104;
foreach( $a as $val)
{
if($val < $v)
{
if(isset($nearestLower))
{ if($nearestLower < $val)
$nearestLower=$val;
}
else
{
$nearestLower=$val;
}
}
if($val > $v)
{
if(isset($nearestGreater))
{ if($nearestGreater > $val)
$nearestGreater=$val;
}
else
{
$nearestGreater=$val;
}
}
}

Try this:
?>
<?php
$a = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
sort($a);
$v = 58;
$lesser = null;
$greater = null;
foreach($a as $key => $current){
if($current <= $v){
$lesser = $current;
$greater = $a[($key+1)];
}else{
}
}
echo "<pre>";
print_r(array(
"lesser" => $lesser,
"greater" => $greater,
));
echo "</pre>";
?>
Edited TO HAVE TWO LOWER AND TWO HIGHER EVEN WHEN MATCH
<?php
$allNumbers = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
sort($allNumbers);
$input = 55;
$index = array_search($input, $allNumbers);
if(empty($index)){
foreach($allNumbers as $key => $value){
if($value < $input){
$index = ($key + 0.5);
}else{
break;
}
}
}
$below = array(
$allNumbers[ceil($index - 2)],
$allNumbers[ceil($index - 1)],
);
$above = array(
$allNumbers[floor($index + 1)],
$allNumbers[floor($index + 2)],
);
echo "<pre>";
print_r(array(
"input" => $input,
"index" => $index,
"below" => $below,
"above" => $above,
));
echo "</pre>";
?>

Related

How can i coloring Rubix Cube By three-dimensional array in php

I want in a three-dimensional array, I just want to paint the cells of the array that are on the outside of the array, like the image below.
I want by function color() get an array (call by reference) change the outer Rubix of the array convert to 1 and interior Rubix convert to 0
i create function color() But it does not work Rubik's walls do not change
$matrix = [
[
[3, 5, 13, 56],
[0, 1, 165, 1],
[-8, 78, 5, 8],
[6, 5, 23, 45]
],
[
[1, 17, 5, 3],
[1, 5, 1, 65],
[6, 5, 5, -4],
[0, 4, 3, 90]
],
[
[9, 9, 8, 0],
[3, 5, 4, 8],
[0, 5, 3, 9],
[1, 4, 5, 7]
]
];
function color(&$matrix){
for ($i = 0; $i < count($matrix); ++$i) {
echo 'layer ' . ($i + 1) . ':' . PHP_EOL;
foreach ($matrix as $j) {
if($i == 1){
$y=0;
foreach ($j as $k) {
if($y == 0 )
echo $f = 1 . ' ';
elseif($y == 1)
echo $f = 0 . ' ';
elseif($y == 2)
echo $f = 1 . ' ';
$y++;
}
}else{
foreach ($j as $k) {
echo $k = 1 . ' ';
}
}
echo PHP_EOL;
}
}
}
how solving coloring Rubic Cube By three-dimensional array?????
<?php
function color(&$ls) {
foreach ($ls as $xs => $xl) {
foreach ($xl as $ys => $yl) {
foreach ($yl as $zs => $cell) {
$ls[$xs][$ys][$zs] = (int)($xs == 0 || $xs == count($ls) - 1 || $ys == 0 || $ys == count($xl) - 1 or $zs == 0 or $zs == count($yl) - 1);
}
}
}
}

Finding higher and lower numbers in an array and storing in one array

I need to get the closest lowest and highest to a number.
Attempt
<?php
$a = array(1, 8, 23,42,47, 52, 55, 66, 74,75, 76,77,78, 95,);
sort($a);
$v = 58;
$lesser = null;
$greater = null;
foreach($a as $key => $current){
if($current <= $v){
$lesser = $current;
$greater = $a[($key+1)];
}else{
}
}
print_r(array(
"lesser" => $lesser,
"greater" => $greater,
));
?>
/** output :
Array
(
lesser => 55
greater => 66
)
**/
My aim is to get all the numbers greater to the given number, and the same with the lesser:
greater => 66, 74, 75, 77, 78, 95
lesser => 55, 52, 47, 42, 23, 8, 1
How do I solve this problem?
Make $lesser and $greater arrays that you push onto, instead of replacing.
<?php
$a = array(1, 8, 23,42,47, 52, 55, 66, 74,75, 76,77,78, 95,);
$v = 58;
$lesser = [];
$greater = [];
foreach($a as $key => $current){
if ($current < $v) {
$lesser[] = $current;
} elseif ($current > $v) {
$greater[] = $current;
}
}
print_r(array(
"lesser" => $lesser,
"greater" => $greater,
));
?>

For-each loop, larger than 20 into another array

I'm stuck on this exercise that I can't seem to get for the life of me.
$numbers2 = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
foreach ($numbers2 as &$value) {
$largeNumbers[] = $value > 20;
}
That's the code I have so far. What I am trying to do here is use a for-each loop to add all the numbers that are larger than 20 into another Array, which I have named $largeNumbers. The code I have inserted above seems to be printing out true and false values, which is not what I was going for. I'd really appreciate it someone could tell me what I'm possibly doing wrong or even show me a better way. I have to use a for-each loop.
For each item, you are checking if it's larger than 20, which results in a boolean value (it either is or is not), and you then store this value to the result array. Instead, you could use an if statement` to only take the elements that answer the condition:
foreach ($numbers2 as &$value) {
if ($value > 20) {
$largeNumbers[] = $value;
}
}
<?php
$nums = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
$less_than_or_equal_to_20 = [];
foreach($nums as $v)
if($v <= 20)
$less_than_or_equal_to_20[] = $v;
$out = array_diff($nums, $less_than_or_equal_to_20);
var_export($out);
Output:
array ( 0 => 21, 4 => 76, 7 => 85, 8 => 55, )
<?php
$nums = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
foreach([$nums] as $v)
$out = array_filter($v, function($v) {
return $v > 20;
});
var_export($out);
Output:
array ( 0 => 21, 4 => 76, 7 => 85, 8 => 55, )

Foreach loop + recursion

I am trying to put this code in a more flexible manner so it can work whatever the size of $sets array is. I suppose it can be done with recursion but cannot find the correct php syntax.
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
$combinations = array();
foreach($sets[0] as $s1)
foreach($sets[1] as $s2)
foreach($sets[2] as $s3)
foreach($sets[3] as $s4)
$combinations[] = array($s1, $s2, $s3, $s4);
print_r($combinations);
You can do it in recursion like this. The output is identical to your loops
<?php
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
function get_combinations($sets, &$combinations = array(), &$row = array()) {
if (count($sets) == 0) {
$combinations[] = $row;
return $combinations;
}
foreach ($sets[0] as $s) {
$row[] = $s;
get_combinations(array_slice($sets, 1), $combinations, $row);
array_pop($row);
}
return $combinations;
}
$combinations = get_combinations($sets);
print_r($combinations);

limit data with same amount per page on library fpdf

I am using PHP and fpdf to generate my view in pdf. My problem is how to limit amount of data per page?
If I have 100 rows data, the result is 10 page. So I must Limit 10 rows per page.
I have this code :
<?php
define('FPDF_FONTPATH', 'font/');
require_once("../../includes/initialize.php");
class PDF extends FPDF {
function Header() {
$this->Image("../icon/banner.jpg", 40, 10, 15);
}
function Footer() {
$this->SetY(-15);
$this->SetFont("Arial", "I", 10);
$this->Cell(0, 10, "Page " . $this->PageNo() . "/{nb}", 0, 0, "C");
}
}
$filter = $_GET["filter"];
$value = $_GET["value"];
if (!empty($_GET["filter"]) && !empty($_GET["value"])) {
$query = "Select id_detail_ruang, id_ruang, id_barang, no_seri from detail_ruang
where ".$filter." = '".$value."'";
} else {
$query = "Select id_detail_ruang, id_ruang, id_barang, no_seri from detail_ruang";
}
$sql = mysql_query($query);
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
array_push($data, $row);
}
$judul = "LAPORAN KERUSAKAN";
$header = array(
array("label" => "No. Detail Kerusakan", "length" => 25, "align" => "C"),
array("label" => "Kode Barang", "length" => 25, "align" => "C"),
array("label" => "Nama Barang", "length" => 50, "align" => "C"),
array("label" => "No Ruang", "length" => 50, "align" => "C")
);
$pdf = new PDF("L");
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 7);
$pdf->Cell(0, 20, $judul, 0, 1, "C");
$pdf->SetFillColor(87, 190, 224);
$pdf->SetTextColor(33, 71, 84);
$pdf->SetDrawColor(255, 0, 25);
$pdf->SetAutoPageBreak(true, 30);
foreach ($header as $kolom) {
$pdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
}
$pdf->Ln();
$pdf->SetFillColor(87, 190, 224);
$pdf->SetTextColor(33, 71, 84);
$pdf->SetFont('');
$fill = false;
foreach ($data as $baris) {
$i = 0;
foreach ($baris as $cell) {
$pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
$i++;
}
$fill = !$fill;
$pdf->Ln();
}
$pdf->Output();
?>
I'm try using SetAutoPageBreak()
Is it Possible to do it..?
How to do it..?
Thanks
Since you've already go the data in a PHP array, the simplest solution would be to use array_chunk():
define('TABLE_SIZE', 100);
$pages=array_chunk($data, TABLE_SIZE, true);
foreach ($page as $table) {
foreach ($table as $baris) {
$i = 0;
foreach ($baris as $cell) {
$pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
$i++;
}
}
if (count($table)==TABLE_SIZE) {
// do page break
}
}

Categories