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

foreach获取PHP中的数组索引结果

  •  0
  • user3844830  · 技术社区  · 11 年前

    我从SOAP客户端得到了以下参数作为响应。但我只希望很少有人作为结果。我得到了正确的结果,但它只适用于1辆车,我有超过1辆车。所以我不知道如何循环才能得到结果。

    1辆车的输出

    array (size=5)
      'SchwackeCode' => int 10130969
      'WE_Number' => int 19373134
      'HSN' => string '0005' (length=4)
      'TSN' => string 'AMP' (length=3)
      'VIN' => string '12345678901472583' (length=17)
    

    代码:

    $client = new SoapClient($wsdl, $options);
    $result = $client->getVehicleValuation($params);    
    $return = array(
                                        'SchwackeCode' => $result->vehicle->SchwackeCode,
                                        'WE_Number'    => $result->vehicle->WE_Number,
                                        'HSN'          => $result->vehicle->HSN, 
                                        'TSN'          => $result->vehicle->TSN,
                                        'VIN'          => $result->vehicle->Ident_Number,
                                        'WE_Number'    => $result->vehicle->WE_Number                                       
                                );
    return $return;
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   GTS Soft.    11 年前

    好的,那就试试这个简单的代码,

    $cnt=0; 
    $arr=Array('SchwackeCode','WE_Number','HSN','TSN' );
    
        foreach($result->Vehicle[$cnt]->Customer[0] as $key=>$val)
       {
             if(in_array($key,$arr)
            {
                        your_piece of code;
             }
              $cnt++;
    
       }
    

    Didnt测试了这段代码,但希望它能奏效。:)