Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my code making PDO return nested arrays?

I'm sure I must be doing something wrong, but I can't seem to find an answer anywhere (at least neither in the documentation nor on SO as of yet). I am a beginner in PHP and SQL.

I have this simple table called "mObject_has_media" with only two columns "media_idMedia" and "mObject_idmObject"

When I try to query the table with this code,

public function selectMediaIds (MObject $object) {
    $medias = array ();
    $req = $this -> _db -> query ('SELECT media_idMedia FROM mObject_has_media WHERE mObject_idmObject = ' . $object -> id ());
    while ($data = $req -> fetch (PDO::FETCH_ASSOC)) {
        $medias[] = $data;
    }
    $req -> closeCursor ();
    return $medias;
}

the problem is this: when I var_dump the contents of the method's result, I get:

array (size=2)
  0 => 
    array (size=1)
      'media_idMedia' => string '52' (length=2)
  1 => 
    array (size=1)
      'media_idMedia' => string '53' (length=2)

I'd like to get rid of those nested arrays and just get something like:

array (size=2)
  0 => 'media_idMedia' => string '52' (length=2)
  1 => 'media_idMedia' => string '53' (length=2)

so I can simply run this array into a foreach loop right afterwards... I'm sure that this problem has everything to do with the fetch() method, but in spite of experimenting with different options, I haven't been able to get the desired behavior.

A similar question (I think) was asked here: PDO::FETCH_ASSOC Does not return the correct table columns but the answer mentioned the Zend framework, and I am NOT using any framework at all, this is just vanilla PHP.

like image 781
ReinaDelSur Avatar asked Dec 18 '25 01:12

ReinaDelSur


1 Answers

Let's create a multi-dimensional array just for testing purposes:

$array = array(
    array('foo' => 'value1'),
    array('foo' => 'value2')
);

Now consider the following code:

$result = array();

while ($row = array_shift($array)) {
    // $row will contain one nested array at a time
    $result[] = $row;
}

print_r($result);

$row is an array, and with the statement $result[] = $row;, you're pushing the entire array to $result thereby making it multi-dimensional.

This would output:

Array
(
    [0] => Array
        (
            [foo] => value1
        )

    [1] => Array
        (
            [foo] => value2
        )

)

Now, consider the following code:

$result = array();

while ($row = array_shift($array)) {
    // $row will contain one nested array at a time
    $result[] = $row['foo'];
}

print_r($result);

With the statement $result[] = $row['foo'];, we are pushing the value $row['foo'] to the $result array. At the end of loop iteration, $result will hold an array of foo row values.

This would output:

Array
(
    [0] => value1
    [1] => value2
)

Demo


To fix your original code, you can modify it like so:

$medias[] = $data['media_idMedia'];
like image 82
Amal Murali Avatar answered Dec 20 '25 16:12

Amal Murali