If print_r does not work, then something is wrong before that. Your data is probably not structured the way you think it is structured.
And can I just recommend that if you only have the tag and the name of the question, you just store it as a sibling array that looks like this:
array( 'php' => '7', 'sql' => '7', 'python' => '3' )
If you have other things that you are going to store, then yes, you will have to do what you do. In this case, if you cannot access the array using the keys in brackets, your keys are probably not what you think, or something is wrong with your code.
I looked at your code and why are you requesting the results of a query, for example $row[0] , instead of $row['question_id'] ?
Also, you cannot print_r($result_tags) , because the data from the request is not actually stored by PHP, this is just a link. therefore, you must use the while loop to go through it, as the script will call one row of results at a time.
You say: foreach( $result_tags as $question_id => $data ) This does not make sense because there is no value for $result_tags , except for a query link, which can be used to invoke a single row of results at the same time. Instead, you want:
while( $row2 = pg_fetch_array( $result_tags )) { $question_id = $row2['questions_question_id']; $data = $row2['data']; $end_array[$question_id]['tags'][] = $data; // you can't have your $data['tag'] as that refers to the // value of an array that doesn't exist }