How can I get outside foreach loop value in this situation?

两盒软妹~` 提交于 2020-02-15 13:41:30

问题


I am trying to call all the value from a foreach loop from mysql data, and use there value input into another mysql select statement. Look at my code below. It only can gather one value. I was thinking to use the first foreach loop include the whole section, however, since foreach value is individual, and I need explode array for the PDO prepare. So, at last, I want to know a way to get all value from the outside foreach loop. Appreciate.

//mysql connection code, remove because it is not relative to this question
foreach ($tag_id as $term){
	$term=$term['term'].' ';//use space to seprate each one
   
 }
	echo $term:// only can can one, how can I get all result from above foreach.
	$term=explode(' ', $term);
	
	
	$stm =$db->prepare("SELECT user_id FROM sign WHERE term IN (:term_0,:term_1,:ts.id, s.term, s.counter, os.user_id, os.id, COUNT(oserm_2,:term_3,:term_4,:term_5,:term_6,:term_7,:term_8,:term_9,:term_10)");

$term_0="$term[0]";
	$term_1="$term[1]";
	$term_2="$term[2]";
	$term_3="$term[3]";
	$term_4="$term[4]";
//following code is not relevive to this question.

回答1:


If your last purpose to create an array from yourvalues why do you append data to a string , just add your data to an array then directly use it .

foreach ($tag_id as $term){
    $term_arr[]=$term['term'];

 }

 print_r($term_arr);

Then you can use

   $stm =$db->prepare("SELECT user_id FROM sign WHERE term IN (:term_0,:term_1,:ts.id, s.term, s.counter, os.user_id, os.id, COUNT(oserm_2,:term_3,:term_4,:term_5,:term_6,:term_7,:term_8,:term_9,:term_10)");

$term_0="$term_arr[0]";
    $term_1="$term_arr[1]";
    $term_2="$term_arr[2]";
    $term_3="$term_arr[3]";
    $term_4="$term_arr[4]";



回答2:


Assuming that $tag_id is the array output of your mysql query

$t = array();
foreach ($tag_id as $term){
    $t[]=$term['term']; // create array of terms 
 }
$str = "'".implode("','",$t)."'"; // make it a string, you can add here any other string you want to complete your query
    $stm =$db->prepare("SELECT `user_id` FROM `sign` WHERE `term` IN (".$str.");
$stm =$db->execute(); 



回答3:


Each time you make the loop you override the vale os $term, look at this:

      $string = ''

        foreach ($tag_id as $term){
            $string .= $term['term'] . ' ';//use space to seprate each one

         }

         $term = $string;
   echo $term:// only can can one, how can I get all result from above foreach.
    $term=explode(' ', $term);


    $stm =$db->prepare("SELECT user_id FROM sign WHERE term IN (:term_0,:term_1,:ts.id, s.term, s.counter, os.user_id, os.id, COUNT(oserm_2,:term_3,:term_4,:term_5,:term_6,:term_7,:term_8,:term_9,:term_10)");

$term_0="$term[0]";
    $term_1="$term[1]";
    $term_2="$term[2]";
    $term_3="$term[3]";
    $term_4="$term[4]";
//following code is not relevive to this question.

Other loop:

  $string = array();

    foreach ($tag_id as $term){
        $string[] = $term['term'];
     }

$term = $string;


来源:https://stackoverflow.com/questions/28869540/how-can-i-get-outside-foreach-loop-value-in-this-situation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!