Search
 
SCRIPT & CODE EXAMPLE
 

PHP

PHP multidimensional array search by value

$key = array_search('100', array_column($userdb, 'uid'));
Comment

PHP multidimensional array search by value

<?php
// PHP program to carry out multidimensional array search
  
  
// Multidimensional array
$gfg_array = array(
    array(
        'score'   => '100',
        'name'    => 'Sam',
        'subject' => 'Data Structures'
    ),
    array(
        'score'   => '50',
        'name'    => 'Tanya',
        'subject' => 'Advanced Algorithms'
    ),
    array(
        'score'   => '75',
        'name'    => 'Jack',
        'subject' => 'Distributed Computing'
    )
);
  
$id = array_search('50', array_column($gfg_array, 'score'));
echo $id;
  
?>
Comment

array search multidimensional php

function find_customer_mobile($customers, $mobile) {
    foreach($customers as $index => $cust) {
        if($cust['mobile'] == $mobile) return $index;
    }
    return FALSE;
}
Comment

php search multidimensional array for multiple values

  /**
   * PHP Search an Array for multiple key / value pairs
   */

  function multi_array_search($array, $search) {
    // Create the result array
    $result = array();

    // Iterate over each array element
    foreach ($array as $key => $value){

      // Iterate over each search condition
      foreach ($search as $k => $v){

        // If the array element does not meet the search condition then continue to the next element
        if (!isset($value[$k]) || $value[$k] != $v){
          continue 2;
        }
      }
      // Add the array element's key to the result array
      $result[] = $key;
    }

    // Return the result array
    return $result;
  }

  // Output the result
  print_r(multi_array_search($list_of_phones, array()));

  // Array ( [0] => 0 [1] => 1 )

  // Output the result
  print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple')));

  // Array ( [0] => 0 )

  // Output the result
  print_r(multi_array_search($list_of_phones, array('Manufacturer' => 'Apple', 'Model' => 'iPhone 6')));

  // Array ( )
Comment

PHP multidimensional array search by value

<?php
// PHP program to carry out multidimensional array search
  
  
// Function to iteratively search for a given value
function searchForId($search_value, $array, $id_path) {
  
    // Iterating over main array
    foreach ($array as $key1 => $val1) {
  
        $temp_path = $id_path;
          
        // Adding current key to search path
        array_push($temp_path, $key1);
  
        // Check if this value is an array
        // with atleast one element
        if(is_array($val1) and count($val1)) {
  
            // Iterating over the nested array
            foreach ($val1 as $key2 => $val2) {
  
                if($val2 == $search_value) {
                          
                    // Adding current key to search path
                    array_push($temp_path, $key2);
                          
                    return join(" --> ", $temp_path);
                }
            }
        }
          
        elseif($val1 == $search_value) {
            return join(" --> ", $temp_path);
        }
    }
      
    return null;
}
  
// Multidimensional array 
$gfg_array = array(
    array(
        'score' => '100',
        'name' => 'Sam',
        'subject' => 'Data Structures'
    ),
    array(
        'score' => '50',
        'name' => 'Tanya',
        'subject' => 'Advanced Algorithms'
    ),
    array(
        'score' => '75',
        'name' => 'Jack',
        'subject' => 'Distributed Computing'
    )
);
  
$search_path = searchForId('Advanced Algorithms',
                    $gfg_array, array('$'));
  
print($search_path);
  
?>
Comment

PHP multidimensional array search by value

<?php
// PHP program to carry out multidimensional array search
  
  
// Function to recursively search for a given value
function array_search_id($search_value, $array, $id_path) {
      
    if(is_array($array) && count($array) > 0) {
          
        foreach($array as $key => $value) {
  
            $temp_path = $id_path;
              
            // Adding current key to search path
            array_push($temp_path, $key);
  
            // Check if this value is an array
            // with atleast one element
            if(is_array($value) && count($value) > 0) {
                $res_path = array_search_id(
                        $search_value, $value, $temp_path);
  
                if ($res_path != null) {
                    return $res_path;
                }
            }
            else if($value == $search_value) {
                return join(" --> ", $temp_path);
            }
        }
    }
      
    return null;
}
  
// Multidimensional (Three dimensional) array
$gfg_array = array(
    "school1" => array(
        "year" => "2017",
        "data" => array(
            'score' => '100',
            'name' => 'Sam',
            'subject' => 'Data Structures'
        )
    ),
      
    "school2" => array(
        "year" => "2018",
        "data" => array(
            'score' => '50',
            'name' => 'Tanya',
            'subject' => 'Advanced Algorithms'
        )
    ),
      
    "school3" => array(
        "year" => "2018",
        "data" => array(
            'score' => '75',
            'name' => 'Jack',
            'subject' => 'Distributed Computing'
        )
    )
);
  
$search_path = array_search_id('Jack', $gfg_array, array('$'));
print($search_path);
  
?>
Comment

PREVIOUS NEXT
Code Example
Php :: mail sending setting magneto for mailhog 
Php :: laravel collection max 
Php :: make exception laravel 
Php :: get node id in twig drupal 
Php :: convert_uudecode (PHP 5, PHP 7, PHP 8) convert_uudecode — Decode a uuencoded string 
Php :: merge two objects php laravel 
Php :: generate entities symfony 
Php :: laravel routes not working on production server 
Php :: php check if parameter exists in url 
Php :: Change WordPress Login Logo Url 
Php :: doctrine querybuilder print sql 
Php :: laravel request get parameter 
Php :: operators in php 
Php :: laravel echo html 
Php :: category title in post 
Php :: slugify text in php 
Php :: json stringify to php array 
Php :: laravel capsule schema datatime CURRENT_TIMESTAMP 
Php :: php regex replace to remove special characters and accented 
Php :: parsing html in php 
Php :: run codeigniter 4 with spark 
Php :: php last item of array 
Php :: laravel valet refresh env 
Php :: real time update using ajax php 
Php :: php all date arguments 
Php :: get current locale laravel 
Php :: php difference between two dates in seconds 
Php :: symfony messenger transport 
Php :: picture on picture php 
Php :: debian install php 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =