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. querySelector and querySelectorAll (30136)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  3. JavaScript strip_tags and stripslashes (8825)
  4. Date and Time in ActionScript 3 (10086)
  5. Bind Tool and Control Points (4430)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)