Search a Multidimensional Array

Searching an array was not as simple as I had thought. Sure, you can use array_search() but that only finds one value off an array the last time I tried it out. I needed something to search a multidimensional array for example - tried many things before I finally figured it out!

Searching a PHP array with ARRAY_FILTER()

I was fooling around with an Online Users script in PHP. Since I was determined to get it done using just a text file, I needed to get my arrays, well... all in an array! :p

Sample array data from the Online Users script

Here's sample of the data the Online Users script I wrote in PHP collected in an array:

php:


<?php

// FILENAME: ONLINE.PHP

// Some dummy data in the main multidimensional array
   # $users['ip']=array(
   #                'ts'=>[int timestamp],
   #                'ctr'=>[int counter],
   #                'pg'=>[str viewing page] );

$users['161.142.1.10'] = array(
                           'ts'=>1052649255,
                           'ctr'=>1,
                           'pg'=>'/forum/' );
$users['202.188.1.5'] = array(
                           'ts'=>1052649289,
                           'ctr'=>2,
                           'pg'=>'/webdsn/' );
$users['192.101.10.55'] = array(
                           'ts'=>1052649300,
                           'ctr'=>1,
                           'pg'=>'/arrays/filter.html' );
$users['65.101.111.58'] = array(
                           'ts'=>1052649899,
                           'ctr'=>2,
                           'pg'=>'/' );

?>


So, according to this data, there are 4 users online; each user being tracked by their IP of course. Now everytime someone views a page, this data / array is updated - as well as adding a new IP to the array, I need to compare the timestamp now with those already in the array, in other words, users online and remove any user that has 'timed out'! This is where I needed to use array_filter().

Let's say the unix timestamp now is 1052649900, so our constant below, TIMED_OUT, would hold the value 1052649300 which means 10 minutes ago i.e. 1052649900-600.

php:


<?php
define( 'TIMED_OUT', time() - 10*60 );
?>


Sample callback function for ARRAY_FILTER()

The array_filter() function requires two parameters, the array to search i.e. $users and a callback function that we shall create next. We'll name this callback function update_users():

php:


<?php
// FUNCTION: UPDATE_USERS( $array )
// A callback function used in array filter()
function update_users( $users )
{
  // compare each $users['ts'] value with the constant
  // TIMED_OUT and return only those that have
  // an equal or higher value.
  return ( $users['ts'] >= TIMED_OUT );
}
?>


Getting the results from a search with ARRAY_FILTER()

Next we create a 'new' array, $users_STILL_online by using the callback function we created above, like this:

php:


<?php
$users_STILL_online = array_filter( $users, 'update_users' );

// print the results...
print_r( $users_STILL_online );
?>


Ouput:

Array
(
    [192.101.10.55] => Array
        (
            [ts] => 1052649300
            [ctr] => 1
            [pg] => /arrays/filter.html
        )

    [65.101.111.58] => Array
        (
            [ts] => 1052649899
            [ctr] => 2
            [pg] => /
        )

)

Meaning that we have 2 users online in the last 10 mins!

TOP

Do you like the new design?

 Yes

 No

 Don't know