如何在foreach值中使用循环

时间:2017-01-02 12:22:10

标签: php

我有一个数组,我需要从中返回一个这样的字符串: propertyType = Apartment& propertyType = Villament 。我正在尝试这样但是当我在循环外部时,我无法获得所有值,只得到第一个值我该怎么做?这些值我想在循环中使用。

<?php
$response = [
    "0" => "Apartment",
    "1"=> "Villament" 
];

foreach ($response as $key => $item) {
  $ppp = "propertyType=".$item.'&';//propertyType=Apartment&propertyType=Villament&
}
echo $ppp;
?>

5 个答案:

答案 0 :(得分:1)

SELECT COUNT (*)  
    INTO num_dups
FROM ADDR_UNIQ_RANGE_CHECK aurc 
WHERE aurc.range_id NOT IN 
             (SELECT base_system_feature_xla_id 
                   FROM DNRANGE_SYS_FEATURE_XLA) 
AND p_UNI_VALUE_NUM >= aurc.from_value 
and p_UNI_VALUE_NUM <= aurc.to_value;

答案 1 :(得分:1)

试试这个

<?php
$respose =  
Array (
    "0" => "Apartment",
    "1"=> "Villament" 
);
$count = count ($respose); 

$ppp = [];
// $str_ppp = '';
foreach ($respose as $key => $item) {
  $ppp[] = "propertyType=".$item;
 // or in your way
 // $str_ppp .= "propertyType=".$item.'&';
}
echo implode('&',$ppp) ;
?>

只需复制代码即可。我希望这会奏效。 感谢

答案 2 :(得分:0)

试试这个

git ls-files --others --exclude-standard > D:/temp.txt

if [ -f D:/temp.txt ] ; then
    for line in `cat D:/temp.txt`;
        do
            if [ -f $line ] ; then 
                cp_path=`dirname $line`
                echo `date +%d/%m/%Y:%H:%M`-$1-$line >> ${cp_path}/version1.txt
            fi
        done
fi

您需要连接$ ppp值。为此使用<?php $respose = Array ( "0" => "Apartment", "1"=> "Villament" ); $count = count ($respose); $ppp =""; foreach ($respose as $key => $item) { $ppp.="propertyType=".$item.'&';//propertyType=Apartment&propertyType=Villament& } echo $ppp ; ?> 运算符。

答案 3 :(得分:0)

试试这个:

$response = array_map(function($v) {
  return 'propertyType='.$v; 
}, $response);

因此,新格式化的数组变为:

Array
(
  [0] => propertyType=Apartment
  [1] => propertyType=Villament
)

现在,您只需将其转换为以&作为粘合剂

的字符串
echo implode('&', $response); // propertyType=Apartment&propertyType=Villament      

答案 4 :(得分:0)

试试这个简单。

<?php
$respose =  
Array (
   "0" => "Apartment",
   "1"=> "Villament" 
);
$ppp ='propertyType='.$respose[0].'&propertyType='.$respose[1];
echo $ppp;