Thursday, 24 July 2014

PHP in_array test (checking for an array in an array or array)

This pleased me no end:

<?php
    // Our original array
    $things = array(
        array(
            "One" => 1,
            "Two" => 2,
            "Three" => 3
        ),
        array(
            "Four" => 4,
            "Five" => 5,
            "Six" => 6
        )
    );
    // Isn't it lovely
    var_dump($things);
    // An array which isn't in our array
    $thing3 = array(
        "Seven" => 7,
        "Eight" => 8,
        "Nine" => 9
    );
    var_dump(in_array($thing3, $things)); // boolean false
    // An array which is in our array
    $thing1 = array(
        "One" => 1,
        "Two" => 2,
        "Three" => 3
    );
    var_dump(in_array($thing1, $things)); // boolean true
    // An array which is in our array but is in the wrong order
    $thing2 = array(
        "Six" => 6,
        "Five" => 5,
        "Four" => 4
    );
    var_dump(in_array($thing2, $things)); // boolean true
?>