代码之家  ›  专栏  ›  技术社区  ›  ForgivenIT

将web结果转换为PHP

  •  -2
  • ForgivenIT  · 技术社区  · 7 年前

    我相信这可能很简单,以前也有人问过,但我找不到。。我尝试过搜索键或值,但无法找到它

    $priceComp_xml":[{"CurrencyCode":"USD","Amount":"92.35"}] 这是网站的回报吗

    我试过了 $array1=json_decode($priceComp_xml,TRUE);没有将其放入数组中 我也尝试过,但我无法将其放入数组中,以便获得值

    array_values() 
    array_key_exists() 
    array_search() 
    

    我想把92.35放入php变量$cost中

    4 回复  |  直到 7 年前
        1
  •  1
  •   Philipp    7 年前

    如果这真的是你的结果,而且你一直都有 $priceComp_xml: 在前面,您只需跳过前16个字符,然后使用 json_decode

    $res = '$priceComp_xml":[{"CurrencyCode":"USD","Amount":"92.35"}]';
    $json = substr($res, 16);
    $data = json_decode($json);
    

    如果前缀有时有其他名称,您仍然可以跳过所有内容,直到第一次出现 :

    $res = '$priceComp_xml":[{"CurrencyCode":"USD","Amount":"92.35"}]';
    $json = substr($res, strpos($res, ':') + 1);
    $data = json_decode($json);
    
        2
  •  0
  •   Andreas    7 年前

    可以使用“分解”删除前导变量名。
    使用explode意味着即使变量名更改,它也可以工作。

    我使用“爆炸”按钮 ": 并将其限制为两项。

    $str = '$priceComp_xml":[{"CurrencyCode":"USD","Amount":"92.35"}]';
    
    $arr = json_decode(explode('":', $str,2)[1], true);
    var_dump($arr);
    

    输出:

    array(1) {
      [0]=>
      array(2) {
        ["CurrencyCode"]=>
        string(3) "USD"
        ["Amount"]=>
        string(5) "92.35"
      }
    }
    

    为了得到你可以使用的数量

    echo $arr[0]['Amount']; //92.35
    

    https://3v4l.org/Vcpov

        3
  •  0
  •   mrk    7 年前

    只需删除不需要的 string str_replace json_decode 后果

    $arr = json_decode(str_replace('$priceComp_xml":' , "" , $response ) , true );
    
        4
  •  -2
  •   ForgivenIT    7 年前

    这就是我最终得到它的原因(在我进行var_转储之前,我没有注意到simplexmlement)

    $count = $compPricing[0];
    $result=array();
            foreach($count as $number){
                $Amount=$count->Amount->__toString();
                $CurrencyCode=$count->CurrencyCode->__toString();
    
            $result = array('Amount' => $Amount, 'CurrencyCode' => $CurrencyCode);
    }
    echo json_encode($Amount);