To understand an array in PHP when given the other variable in the array

前端 未结 2 1702
庸人自扰
庸人自扰 2020-12-20 07:55

Please, see Cha\'s answer where we are pretty close the final solution. We are debugging the code with these data

  1. database in PostgreSQL
  2. test sql-quer
2条回答
  •  既然无缘
    2020-12-20 08:42

    If print_r is failing, then something is wrong before that. Your data is probably not being structured in the way you think it is being structured.

    And can I just recommend that if you only have the tag and the question name, you just store as a single level array that looks like the following:

    array(
    'php' => '7',
    'sql' => '7',
    'python' => '3'
    )
    

    If you have other things you're going to store then yeah, you'll have to do what you're doing. In that case, if you can't access the array with the keys in brackets, then your keys are probably not what you think they are, or there is something wrong with your code somewhere.

    I looked at your code, and why are you accessing query results like $row[0], instead of $row['question_id'] ?

    Also, you can't print_r($result_tags) , because data from a query is not actually stored by PHP, it is only referenced. therefore you have to use a 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 doesn't make sense, because there is no value for $result_tags except for a reference to the query that can be used to call one row of results at a time. Instead you want to do:

    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
    }
    

提交回复
热议问题