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 to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Get all the unique numbers from two-dimensional array

Last accessed pages

  1. Recursive function to create Multi-Level Menu in PHP (11769)
  2. Force Download files with PHP (5056)
  3. The Truth Is (575)
  4. Create simple Website with PHP (43824)
  5. HTML5 New Tags (447)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (250)
  2. Read Excel file data in PHP - PhpExcelReader (91)
  3. PHP Unzipper - Extract Zip, Rar Archives (76)
  4. The Four Agreements (75)
  5. The Mastery of Love (66)
Chat
Chat or leave a message for the other users
Full screenInchide