To understand an array in PHP when setting another variable in an array

Please see Cha's answer, where we are pretty close to the final decision. We are debugging the code with this data.

-

Issue with unresolved code issue

How can you reference a php element in the following array?

I have the following data

Array ( [1] => Array ( [tag] => Array ( [0] => php [1] => scripts ) ) [2] => Array ( [tag] => Array ( [0] => ssh [1] => network ) ) ) 

where arrays are tags_and_id , question_id and tag respectively. In other words, the $tags_and_id variable has a question_id column and still has a tag and that still has every single tag.

I would like to know how you can reference the php tag.

I run nothing as an exit unsuccessfully

 echo $tags_and_id[1]['tag'][0]; 
+1
source share
2 answers

If you want to add additional information about each question, you can simply add to $end_array[1] . This is the array that my first foreach creates:

 // The end result turns out to be something like this: array( 1 => array( 'tags' => array( 0 => "php", 1 => "sql"), ) ); 

 <?php $dbconn = pg_connect("host=localhost port=5432 dbname=noa user=noa password=123"); if( empty($_GET) ) { // to get titles and question_ids $result_titles_tags = pg_prepare( $dbconn, "query777", "SELECT question_id, title FROM questions WHERE question_id IN ( SELECT question_id FROM questions ORDER BY was_sent_at_time DESC LIMIT 50 ) ORDER BY was_sent_at_time DESC LIMIT 50;" ); $result_titles = pg_execute( $dbconn, "query777", array()); // TAGS $result_tags = pg_prepare( $dbconn, "query9", "SELECT question_id, tag FROM tags WHERE question_id IN ( SELECT question_id FROM questions ORDER BY was_sent_at_time DESC LIMIT 50 );" ); $result_tags = pg_execute( $dbconn, "query9", array()); 

and the code in question initially

  // Go through each Tag while( $tags_and_Qid = pg_fetch_array( $result_tags )) { // Add the Tag to an array of tags for that question $end_array [ $tags_and_Qid['question_id'] ] ['tag'] [] = $tags_and_Qid['tag']; } // First compile the Data // Go through each question while( $titles_and_Qid = pg_fetch_array( $result_titles ) ) { echo ("<div class='question_summary'>" . $titles_and_Qid['title'] // Problem here! // How can you print the titles such that // they are assigned to each tag -set? ); $i = 0; // Then Loop Through each question foreach( $end_array as $tags_and_Qid['question_id'] => $tags_and_Qid['tag'] ) { echo ("\n\nITERATION NUMBER IS " . $i); // The code is buggy here // For instance, we get 9 iterations for 3 questions // Create the starting HTML echo ("<div class='tags'>"); // Go through each tag // not sure about this foreach( $end_array[$tags_and_Qid['question_id']] ['tag'] as $tag ) { echo ( "<a class='post_tag' href='?tag=" . $tag . "'>" . $tag . "</a>" ); } // end the html echo '</div>'; $i++; } echo ("</div>"); } // to end the list of questions echo ("</div>" ); } ?> 
+4
source

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 } 
+1
source

All Articles