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 renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Get all the unique numbers from two-dimensional array

Last accessed pages

  1. Adding text with ActionScript 3 (5638)
  2. CSS cursor property - Custom Cursors (6372)
  3. Zodiac Signs PHP code (7271)
  4. Using openssl_encrypt and openssl_decrypt in PHP (1312)
  5. CSS Box Model (768)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (458)
  2. CSS cursor property - Custom Cursors (68)
  3. Read Excel file data in PHP - PhpExcelReader (46)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (44)
  5. PHP Unzipper - Extract Zip, Rar Archives (41)