Skip to content

Instantly share code, notes, and snippets.

@Mario-Duarte
Forked from kjbrum/search_for_value.php
Created January 8, 2019 17:11
Show Gist options
  • Save Mario-Duarte/d9de8b9338331feeac17b07b9766e27f to your computer and use it in GitHub Desktop.
Save Mario-Duarte/d9de8b9338331feeac17b07b9766e27f to your computer and use it in GitHub Desktop.

Revisions

  1. Kyle Brumm revised this gist Nov 11, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions search_for_value.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php
    /**
    * Check if an array is a multidimensional array
    * Check if an array is a multidimensional array.
    *
    * @param array $arr The array to check
    * @return boolean Whether the the array is a multidimensional array or not
    @@ -11,7 +11,7 @@ function is_multi_array( $x ) {
    }

    /**
    * Convert an object to an array
    * Convert an object to an array.
    *
    * @param array $object The object to convert
    * @return array The converted array
    @@ -22,7 +22,7 @@ function object_to_array( $object ) {
    }

    /**
    * Check if a value exists in the array/object
    * Check if a value exists in the array/object.
    *
    * @param mixed $needle The value that you are searching for
    * @param mixed $haystack The array/object to search
  2. Kyle Brumm revised this gist Sep 15, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions search_for_value.php
    Original file line number Diff line number Diff line change
    @@ -11,26 +11,26 @@ function is_multi_array( $x ) {
    }

    /**
    * Check if an array is a multidimensional array
    * Convert an object to an array
    *
    * @param array $object The object to convert
    * @return array The converted array
    */
    function objToArr( $object ) {
    function object_to_array( $object ) {
    if( !is_object( $object ) && !is_array( $object ) ) return $object;
    return array_map('objToArr', (array) $object);
    return array_map( 'object_to_array', (array) $object );
    }

    /**
    * Check if a value exists in an array/object
    * Check if a value exists in the array/object
    *
    * @param mixed $needle The value that you are searching for
    * @param mixed $haystack The array/object to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function search_for_value( $needle, $haystack, $strict=true ) {
    $haystack = objToArr( $haystack );
    $haystack = object_to_array( $haystack );

    if( is_array( $haystack ) ) {
    if( is_multi_array( $haystack ) ) { // Multidimensional array
  3. Kyle Brumm revised this gist Sep 15, 2015. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions search_for_value.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,26 @@
    <?php
    /**
    * Check if an array is a multidimensional array
    *
    * @param array $arr The array to check
    * @return boolean Whether the the array is a multidimensional array or not
    */
    function is_multi_array( $x ) {
    if( count( array_filter( $x,'is_array' ) ) > 0 ) return true;
    return false;
    }

    /**
    * Check if an array is a multidimensional array
    *
    * @param array $object The object to convert
    * @return array The converted array
    */
    function objToArr( $object ) {
    if( !is_object( $object ) && !is_array( $object ) ) return $object;
    return array_map('objToArr', (array) $object);
    }

    /**
    * Check if a value exists in an array/object
    *
  4. Kyle Brumm revised this gist Sep 15, 2015. 2 changed files with 40 additions and 30 deletions.
    30 changes: 0 additions & 30 deletions in_any_array.php
    Original file line number Diff line number Diff line change
    @@ -1,30 +0,0 @@
    <?php
    /**
    * Check if a value exists in any type of array
    *
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The array to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function in_any_array( $needle, $haystack, $strict=false ) {
    if( count( $haystack ) != count( $haystack, 1 ) ) {
    foreach( $haystack as $subhaystack ) {
    if( in_any_array( $needle, $subhaystack, $strict ) ) {
    return true;
    }
    }
    } elseif( is_array( $haystack ) ) {
    if( in_array( $needle, $haystack, $strict ) ) {
    return true;
    }
    } else {
    if( $needle == $haystack && !$strict ) {
    return true;
    } elseif( $needle === $haystack && $strict ) {
    return true;
    }
    }

    return false;
    }
    40 changes: 40 additions & 0 deletions search_for_value.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php
    /**
    * Check if a value exists in an array/object
    *
    * @param mixed $needle The value that you are searching for
    * @param mixed $haystack The array/object to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function search_for_value( $needle, $haystack, $strict=true ) {
    $haystack = objToArr( $haystack );

    if( is_array( $haystack ) ) {
    if( is_multi_array( $haystack ) ) { // Multidimensional array
    foreach( $haystack as $subhaystack ) {
    if( search_for_value( $needle, $subhaystack, $strict ) ) {
    return true;
    }
    }
    } elseif( array_keys( $haystack ) !== range( 0, count( $haystack ) - 1 ) ) { // Associative array
    foreach( $haystack as $key => $val ) {
    if( $needle == $val && !$strict ) {
    return true;
    } elseif( $needle === $val && $strict ) {
    return true;
    }
    }

    return false;
    } else { // Normal array
    if( $needle == $haystack && !$strict ) {
    return true;
    } elseif( $needle === $haystack && $strict ) {
    return true;
    }
    }
    }

    return false;
    }
  5. Kyle Brumm revised this gist Sep 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion in_any_array.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    function in_any_array( $needle, $haystack, $strict=false ) {
    if( count( $haystack ) != count( $haystack, 1 ) ) {
    foreach( $haystack as $subhaystack ) {
    if( in_multi_array( $needle, $subhaystack, $strict ) ) {
    if( in_any_array( $needle, $subhaystack, $strict ) ) {
    return true;
    }
    }
  6. Kyle Brumm revised this gist Aug 30, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions in_any_array.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /**
    * Check if a value exists in any type of array
    *
  7. Kyle Brumm revised this gist Aug 11, 2015. No changes.
  8. Kyle Brumm renamed this gist Aug 11, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. Kyle Brumm revised this gist Aug 11, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    /**
    * Check if a value exists in a multidimensional array
    * Check if a value exists in any type of array
    *
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The array/multidimensional array to search
    * @param array $haystack The array to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function in_multi_array( $needle, $haystack, $strict=false ) {
    function in_any_array( $needle, $haystack, $strict=false ) {
    if( count( $haystack ) != count( $haystack, 1 ) ) {
    foreach( $haystack as $subhaystack ) {
    if( in_multi_array( $needle, $subhaystack, $strict ) ) {
  10. Kyle Brumm revised this gist Aug 11, 2015. 1 changed file with 18 additions and 5 deletions.
    23 changes: 18 additions & 5 deletions in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,29 @@
    /**
    * Check if a value exists in a multi-dimensional array
    * Check if a value exists in a multidimensional array
    *
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The multi-dimensional array to search
    * @param array $haystack The array/multidimensional array to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function in_multi_array( $needle, $haystack, $strict=false ) {
    foreach( $haystack as $subhaystack ) {
    if( in_array( $needle, $subhaystack, $strict ) )
    if( count( $haystack ) != count( $haystack, 1 ) ) {
    foreach( $haystack as $subhaystack ) {
    if( in_multi_array( $needle, $subhaystack, $strict ) ) {
    return true;
    }
    }
    } elseif( is_array( $haystack ) ) {
    if( in_array( $needle, $haystack, $strict ) ) {
    return true;
    }
    } else {
    if( $needle == $haystack && !$strict ) {
    return true;
    } elseif( $needle === $haystack && $strict ) {
    return true;
    }
    }

    return false;
    }
    }
  11. Kyle Brumm revised this gist Jul 6, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function in_multi_array($needle, $haystack, $strict=false) {
    foreach ($haystack as $subhaystack) {
    if(in_array($needle, $subhaystack, $strict))
    function in_multi_array( $needle, $haystack, $strict=false ) {
    foreach( $haystack as $subhaystack ) {
    if( in_array( $needle, $subhaystack, $strict ) )
    return true;
    }

  12. Kyle Brumm revised this gist Mar 12, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -11,5 +11,6 @@ function in_multi_array($needle, $haystack, $strict=false) {
    if(in_array($needle, $subhaystack, $strict))
    return true;
    }

    return false;
    }
  13. Kyle Brumm revised this gist Mar 12, 2015. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    /**
    * Check if a value exists in a multi-dimensional array
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The multi-dimensional array to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    * Check if a value exists in a multi-dimensional array
    *
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The multi-dimensional array to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function in_multi_array($needle, $haystack, $strict=false) {
    foreach ($haystack as $subhaystack) {
  14. Kyle Brumm revised this gist Mar 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * Check if a value is in a multi-dimensional array
    * Check if a value exists in a multi-dimensional array
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The multi-dimensional array to search
    * @param boolean $strict Whether to use strict search or not
  15. Kyle Brumm created this gist Mar 12, 2015.
    14 changes: 14 additions & 0 deletions in_multi_array.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    /**
    * Check if a value is in a multi-dimensional array
    * @param mixed $needle The value that you are searching for
    * @param array $haystack The multi-dimensional array to search
    * @param boolean $strict Whether to use strict search or not
    * @return boolean Whether the value was found or not
    */
    function in_multi_array($needle, $haystack, $strict=false) {
    foreach ($haystack as $subhaystack) {
    if(in_array($needle, $subhaystack, $strict))
    return true;
    }
    return false;
    }