-1

When Using Medoo advanced select like so:

$medoo->query('SQL Goes HERE')->fetchAll();

results are returned in the following structure:

Array ( 
    [0] => Array ( 
        [Column 0 name] => "VALUE OF Record 0, Column 0" 
        [0] => "VALUE OF Record 0, Column 0" 
        [Column 1 name] => "VALUE OF Record 0, Column 1" 
        [1] => "VALUE OF Record 0, Column 1"
        ...
        ...
        [Column 99 name] => "VALUE OF Record 0, Column 99" 
        [99] => "VALUE OF Record 0, Column 99"
        ) 
    [1] => Array ( 
        [Column 0 name] => "VALUE OF Record 1, Column 0" 
        [0] => "VALUE OF Record 1, Column 0" 
        [Column 1 name] => "VALUE OF Record 1, Column 1" 
        [1] => "VALUE OF Record 1, Column 1"
        ...
        ...
        [Column 99 name] => "VALUE OF Record 1, Column 99" 
        [99] => "VALUE OF Record 1, Column 99"
        )
    ...
    ...
    ... 
    [99] => Array ( 
        [Column 0 name] => "VALUE OF Record 99, Column 0" 
        [0] => "VALUE OF Record 99, Column 0" 
        [Column 1 name] => "VALUE OF Record 99, Column 1" 
        [1] => "VALUE OF Record 99, Column 1"
        ...
        ...
        [Column 99 name] => "VALUE OF Record 99, Column 99" 
        [99] => "VALUE OF Record 99, Column 99"
        )
)

every array has both an associative key and and indexed key value pair for some reason, not 100% why they built it that way. Any suggestions on how to drop all of the indexed results and leave just the associative array?

TheRealMrCrowley
  • 976
  • 7
  • 24
  • 1
    with pdo try `$row = $stmt->fetch(PDO::FETCH_ASSOC);` Although I dont see where your query is, so just guessing at what driver you are using. – ArtisticPhoenix Jun 22 '16 at 00:18
  • `I've no idea how fputcsv actually works when it comes to multidimensional arrays` as far as I know it doesn't, you should loop through the top level with foreach, and output the `$row` into the file. Typically that's how its done. – ArtisticPhoenix Jun 22 '16 at 00:21
  • I'm not using PDO, I'm using medoo: http://medoo.in – TheRealMrCrowley Jun 22 '16 at 00:25
  • I have the loop through the top level, it's how to push the data in each record that I have a problem with. (e.g. I can move from record 0 to record 99, but I'm not sure how to push the data in column 0 - 99) – TheRealMrCrowley Jun 22 '16 at 00:28
  • use foreach instead, and whats this `$result.length` PHP, Javascript ? PHP is this way `count( $result )` – ArtisticPhoenix Jun 22 '16 at 00:31
  • do you know how to grab the associative key?, as mentioned above, the data in the array is duplicated, both and indexed numerical key, and an associative key value pair. that is what i'm struggling with most – TheRealMrCrowley Jun 22 '16 at 00:32
  • `foreach( $result as $key => $value )` - not hard. That is what foreach is for – ArtisticPhoenix Jun 22 '16 at 00:33
  • i know how to use foreach, but that will put all of the data twice – TheRealMrCrowley Jun 22 '16 at 00:34
  • i guess if $key is an int and drop it if so, than that would work actually, thanks – TheRealMrCrowley Jun 22 '16 at 00:36
  • also good catch on the count(), it's been a long day – TheRealMrCrowley Jun 22 '16 at 00:38
  • Its ok, what you have in your array is fetch both, which is the default for one of the DB drivers ( cant remember if its MySqli or PDO ) but anyway you want to 'fetch' only the associate part. it's easy to fix in either one. Mostly I been doing PDO for the last 3 years so for MySqli Id have to look up. It's better to let the DB driver remove it otherwise your loop spends half it's cycles on junk. And your array uses 2x as much memory as it needs to. You can also set the default fetch mode in PDO then you never have the issue again, http://php.net/manual/en/pdostatement.setfetchmode.php – ArtisticPhoenix Jun 22 '16 at 01:06
  • holy shit bingo! you're right, I forgot when I was testing the query builder I set fetchAll. problem solved – TheRealMrCrowley Jun 22 '16 at 01:21
  • if you want to make a duplicate of the answer i put below, i'll accept it and delete mine – TheRealMrCrowley Jun 22 '16 at 01:34
  • It's ok not really worried about it. thanks anyway. – ArtisticPhoenix Jun 22 '16 at 02:22

2 Answers2

1

fetchAll needs to be filled out properly:

$medoo->query('Query String')->fetchAll(PDO::FETCH_ASSOC);
TheRealMrCrowley
  • 976
  • 7
  • 24
0

You'll probably need something along these lines. This is on the assumption the data types you outline are accurate

$columns = array(array());
foreach ($arr[0] as $key => $value) {
    if (!is_int($key)) {        
        $columns[0][] = $value;
    }
}

$rows = array();
foreach ($arr as $results) {
    $thisRow = array();
    foreach ($results as $key => $value) {
        if (is_int($key)) {
            $thisRow[] = $value;
        }
    }
    $rows[] = $thisRow;
}

$output = array_merge($columns, $rows);
Ian
  • 590
  • 2
  • 19