Php-mysql Course

PHP recursive function that returns a two-dimensional array with all the unique numbers from a two-level array with numbers.
This function is useful when you have an array with other arrays with numbers, and you want to get the child arrays with numbers that aren't in the other arrays.
For example, if you have the following array:
$arr = array(
  'n1'=>array(1, 2, 3),
  'n2'=>array(1, 20, 13),
  'n3'=>array(11, 22, 8),
  'n4'=>array(10, 22, 7)
);
The getUniqueNr() function presented in this page will return this array (remowing child arrays with numbers that are in other array):
$arr = array(
  'n1'=>array(1, 2, 3),
  'n3'=>array(11, 22, 8)
);

- Code of the function:
// recursive function that returns an array with the unique numbers from a two-dimensional array
// receives: $arr (two-level array with numbers)
// no need to pass: $arc (array to compare), and: $re (array with returned result)
function getUniqueNr($arr, $arc=array(), $re=array()) {
  // From: https://coursesweb.net/php-mysql/
  // if its the first call
  if(count($arr) > 0) {
    if(count($arc) == 0) {
      $arc = current($arr);
      $re[key($arr)] = $arc;
    }

    $x = 0;
    foreach($arr AS $k=>$arv) {
      if(!array_intersect($arc, $arv)) {
        if($x == 0) {
          $next_arc = $re[$k] = $arv;
          $x = 1;
        }
      }
      else unset($arr[$k]);
    }
    if(count($arr) > 1 && isset($next_arc)) {
      array_shift($arr);
      return getUniqueNr($arr, $next_arc, $re);
    }
    else return $re;
  }
}
- Example (test it yourself):
<?php
// recursive function that returns an array with the unique numbers from a two-dimensional array
// receives: $arr (two-level array with numbers)
// no need to pass: $arc (array to compare), and: $re (array with returned result)
function getUniqueNr($arr, $arc=array(), $re=array()) {
  // From: https://coursesweb.net/php-mysql/
  // if its the first call
  if(count($arr) > 0) {
    if(count($arc) == 0) {
      $arc = current($arr);
      $re[key($arr)] = $arc;
    }

    $x = 0;
    foreach($arr AS $k=>$arv) {
      if(!array_intersect($arc, $arv)) {
        if($x == 0) {
          $next_arc = $re[$k] = $arv;
          $x = 1;
        }
      }
      else unset($arr[$k]);
    }
    if(count($arr) > 1 && isset($next_arc)) {
      array_shift($arr);
      return getUniqueNr($arr, $next_arc, $re);
    }
    else return $re;
  }
}

// two-level array with numbers
$arr = array(
  'arr1' => array(2, 6),
  'arr2' => array(4, 10),
  'arr3' => array(2, 12),
  'arr4' => array(50, 63),
 'arr5' => array(45, 50)
);

$unique_nrs = getUniqueNr($arr);

// test
var_export($unique_nrs);

// Result: array (0=>arra ('arr1' => 2, 1 => 6), 'arr2'=>array(0 => 4, 1 => 10), 'arr4'=>array(0 => 50, 1 => 63))
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Get all the unique numbers from two-dimensional array

Last accessed pages

  1. JavaScript Chronometer / Stopwatch (7355)
  2. Node.js Move and Copy file (28421)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141759)
  4. Upload Script for Gallery of Images and Audio files (9754)
  5. Ajax-PHP Chat Script (49484)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (483)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (73)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)