1

I have a large SQL query which returns aggregate data.

My returned array is something like this:

array(37) { // I get 37 arrays returned 
    [0] => array(10) {
        ["groupId"] => string(1) "3"  // the first param is just an id  
        ["sessionCount84"] => string(1) "0"   
        ["sessionCount1"] => string(1) "1" 
    } ...

Each sub-array will contains multiple keys with the word 'sessionCount' and a number following that and there could be many of them.

Is there a way to get all the values for keys which contain the words 'sessionCount[someNumber]" ?

I tried the array_keys function in php, but that requires an exact word match, meaning I would need to know the number following 'sessionCount' in the key, so I'd need to get all the keys that contain that word first somehow.

Antony
  • 14,900
  • 10
  • 46
  • 74
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98
  • loop over the array ^^ – ficuscr Mar 26 '13 at 16:19
  • @Steve thanks a bunch, I wasn't able to find that myself for some reason – SoluableNonagon Mar 26 '13 at 16:20
  • This returned data suggests poor table design. Why isn't `sessionCount` a dependent table instead of dozens of columns? – Barmar Mar 26 '13 at 16:42
  • sessionCount is not its own table, the sessionCount is a result of an aggregate function on other existing tables that span across multiple schemas conditionally and are grouped by conditional primary keys. Basically, I have a large amount of schemas and a large amount of tables in each schema and this result is a 'report' being run across many of them at once, joining many tables together. – SoluableNonagon Mar 26 '13 at 17:59

3 Answers3

2
$tmp = array();
foreach ($array as $sub)
    foreach ($sub as $k => $v)
        if (substr($k, 0, 12) == 'sessionCount')
            $tmp[$k] = $v;
David Chan
  • 7,347
  • 1
  • 28
  • 49
1

Perhaps something like this will help.

$myArray = array(37) { [0]=>  // I get 37 arrays returned  
    array(10) { ["groupId"]=> string(1) "3"  // the first param is just an id  
    ["sessionCount84"]=> string(1) "0"   
    ["sessionCount1"]=> string(1) "1" } ...

$sessions = array();
array_map(function($session) use (&$sessions) {
    foreach ($session as $key => $value) {
        if ($key starts with "sessionCount") // I'm going to leave that up to you
            $sessions[$key] = $value;
    }
}, $myArray);
000
  • 26,951
  • 10
  • 71
  • 101
0

without changing the array you can only do this by brute force.. aka. try "sessionCount[0-999]"..

a different approach would be to use strcmp() on the array keys like so:

foreach($array as $key => $value) 
    if(!strcmp($key,"sessionCount")) 
        dosomething($key,$value);

or loop through your array once and restructure it to something like this:

array() {
  ["sessionCount"] => array() {
      [84] => "",
      [1] => "",
  }
}

..after that finding all the keys you require should be a walk in the park. ;)

Gung Foo
  • 13,392
  • 5
  • 31
  • 39
  • I agree the structure should be better, perhaps in the SQL.. but being able to restructure your array is also the answer to the original problem. – David Chan Mar 26 '13 at 16:23
  • I wish I could structure it better, but my experience with SQL is not that great, I assume that the above structure would require some sub-querying? – SoluableNonagon Mar 26 '13 at 16:37
  • it would require you to look at the foreach() above the structuring. ;) – Gung Foo Mar 26 '13 at 16:39