Skip to content

Instantly share code, notes, and snippets.

@nmmmnu
Created August 5, 2011 09:28
Show Gist options
  • Save nmmmnu/1127192 to your computer and use it in GitHub Desktop.
Save nmmmnu/1127192 to your computer and use it in GitHub Desktop.
Get PHP associative array from Redis search result
$r = new Redis();
$r->connect($redis_ip, $redis_port);
$data = $r->sort("all:srv", array(
"get" => array("srv:*->port", "srv:*->mem" )
)
);
print_r(redis_transpose_sort($data, array("port", "mem") ) );
// ========================================================
function redis_transpose_sort($data, $fields){
$m = array();
if (count($data)){
$record = 0 - 1;
$index = 0;
foreach($data as $d){
if ($index == 0){
$record++;
$m[$record] = array();
}
$m[$record][$fields[$index]] = $d;
$index++;
if ($index >= count($fields))
$index = 0;
}
}
return $m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment