PHP根据JSON Feed值更改变量中的值

时间:2015-10-29 05:28:10

标签: php json variables

我从名为$ response

的JSON Feed中获得折扣值

JSON Feed是{"折扣":" 15%"}

我想读取该值并根据原始值增加另一个变量。

我的脚本是

2>&1 | grep -Po 'since \K.*(?= \()'

当我输出值时,我在$ ed(现有折扣)和$ nd(新折扣)中都没有得到任何东西

2 个答案:

答案 0 :(得分:1)

$ response1是对象而不是$ response1->折扣。所以你的循环将是这样的:

foreach($response1 as $ed){

if($ed->discount == $d0){ $nd = $d1; }
elseif($ed->discount == $d1){ $nd = $d2; }
elseif($ed->discount == $d2){ $nd = $d3; }
elseif($ed->discount == $d3){ $nd = $d4; }
else {$nd = "10%";}
}

答案 1 :(得分:1)

试试这个:

$result['discount'] = '15%';
$response = json_encode($result);

$nd = "";

$d0 = "10%";
$d1 = "15%";
$d2 = "20%";
$d3 = "30%";
$d4 = "40%";


$response1 = json_decode($response,true);

foreach($response1 as $ed){

if($ed == $d0){ $nd = $d1; }
elseif($ed == $d1){ $nd = $d2; }
elseif($ed == $d2){ $nd = $d3; }
elseif($ed == $d3){ $nd = $d4; }
else {$nd = "10%";}
}

echo $ed.''.$nd;