PHP Classes
elePHPant
Icontem

File: examples/CrossReference.php

Recommend this page to a friend!
  Classes of Christian Vigh  >  PHP Array Helpers  >  examples/CrossReference.php  >  Download  
File: examples/CrossReference.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP Array Helpers
Perform several array manipulation operations
Author: By
Last change:
Date: 3 years ago
Size: 1,532 bytes
 

 

Contents

Class file image Download
<?php
   
/***********************************************************************************************************

        The following example demonstrates the use of the ArrayHelpers::CrossReference function.

     ***********************************************************************************************************/
   
require ( '../ArrayHelpers.phpclass' ) ;

    if (
php_sapi_name ( ) != 'cli' )
        echo (
"<pre>" ) ;

   
// Example 1 : we want to know :
    // 1) which items, present in $keys, are missing from $array
    // 2) which items, present in $array, are missing from $keys
    // $array is an associative array.
   
$keys = [ 'a', 'b', 'd', 'e' ] ;
   
$array = [ 'a' => 'a value', 'b' => 'b value', 'c' => 'c value', 'd' => 'd value' ] ;
   
$status = ArrayHelpers::CrossReference ( $keys, $array, $missing, $extra ) ;

    echo
"********** Example using associative array :\n" ;
    echo
"Keys that are missing in array : " ; print_r ( $missing ) ;
    echo
"Values contained in array but not in keys : " ; print_r ( $extra ) ;

   
// Example 2 is the same as example 1, but it uses a non-associative array instead
   
$keys = [ 0, 1, 3, 10 ] ;
   
$array = [ 'a value', 'b value', 'c value', 'd value' ] ;
   
$status = ArrayHelpers::CrossReference ( $keys, $array, $missing, $extra ) ;

    echo
"********** Example using associative array :\n" ;
    echo
"Keys that are missing in array : " ; print_r ( $missing ) ;
    echo
"Values contained in array but not in keys : " ; print_r ( $extra ) ;