在这种情况下,如何获取foreach循环值以外的值? [英] How can I get outside foreach loop value in this situation?

查看:128
本文介绍了在这种情况下,如何获取foreach循环值以外的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从mysql数据的foreach循环中调用所有值,并使用那里的值输入到另一个mysql select语句中.看下面的代码.它只能收集一个值.我当时正在考虑使用第一个foreach循环包括整个部分,但是,因为foreach的值是独立的,因此我需要使用爆炸数组进行PDO准备.因此,最后,我想知道一种从外部foreach循环获取所有值的方法.感激.

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.

推荐答案

如果最后一个目的是要根据yourvalues创建数组,为什么要将数据附加到字符串中,只需将数据添加到数组中,然后直接使用它即可.

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);

然后您可以使用

   $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]";

这篇关于在这种情况下,如何获取foreach循环值以外的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆