Skip to main content

How to search by multiple key => value in PHP array

<?php

// PHP program to search for multiple
// key=>value pairs in array

function search($array, $search_list) {

    // Create the result array
    $result = array();

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

        // Iterate over each search condition
        foreach ($search_list 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)
            {
               
                // Skip two loops
                continue 2;
            }
        }
   
        // Append array element's key to the
        //result array
        $result[] = $value;
    }

    // Return result
    return $result;
}

// Multidimensional array for students list
$arr = array(
    1 => array(
        'rollNo' => 44,
        'name' => 'Alice',
        'section' => 'B'
    ),
    2 => array(
        'rollNo' => 3,
        'name' => 'Amit',
        'section' => 'B'
    ),
    3 => array(
        'rollNo' => 3,
        'name' => 'Bob',
        'section' => 'A'
    ),
    4 => array(
        'rollNo' => 5,
        'name' => 'Gaurav',
        'section' => 'B'
    ),
    5 => array(
        'rollNo' => 5,
        'name' => 'Gaurav',
        'section' => 'A'
    )
);

// Define search list with multiple key=>value pair
$search_items = array('rollNo'=>5, 'section'=>"A");

// Call search and pass the array and
// the search list
$res = search($arr, $search_items);

// Print search result
foreach ($res as $var) {
    echo 'RollNo: ' . $var['rollNo'] . '<br>';
    echo 'Name: ' . $var['name'] . '<br>';
    echo 'Section: ' . $var['section'] . '<br>';       
}

?>
Output:
RollNo: 5
Name: Gaurav
Section: A

Comments

Popular posts from this blog

bootstrap slider back to top

<script src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" > </script> <link rel = "stylesheet" id = "font-awesome-css" href = "//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" type = "text/css" media = "screen" > copy anywhere in your page <div class="scroll-top-wrapper "> <span class="scroll-top-inner"> <i class="fa fa-2x fa-arrow-circle-up"></i> </span> </div> <style> .scroll-top-wrapper {     position: fixed; opacity: 0; visibility: hidden; overflow: hidden; text-align: center; z-index: 99999999;     background-color: #2E9BDC; color: #eeeeee; width: 50px; height: 48px; line-height: 48px; right: 30px; bottom: 30px; padding-top: 2px; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-rad...

embed play youtube video just from url

<? $videolink='https://www.youtube.com/watch?v=6SDlG8T2KRE'; $ytarray=explode("/", $videolink); $ytendstring=end($ytarray); $ytendarray=explode("?v=", $ytendstring); $ytendstring=end($ytendarray); $ytendarray=explode("&", $ytendstring); $ytcode=$ytendarray[0]; echo "<iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$ytcode\" frameborder=\"0\" allowfullscreen></iframe>"; ?>