我对数组不是很满意,需要一些帮助,
我只需要从这个给定的数组中专门检索“值”, 例如下面这个给定数组的例子,我只需要“ 40000”值。
array:3 [▼
0 => array:3 [▼
"service" => "OKE"
"description" => "Ongkos Kirim Ekonomis"
"cost" => array:1 [▼
0 => array:3 [▼
"value" => 40000
"etd" => "2-3"
"note" => ""
]
]
]
1 => array:3 [▼
"service" => "REG"
"description" => "Layanan Reguler"
"cost" => array:1 [▼
0 => array:3 [▼
"value" => 48000
"etd" => "1-2"
"note" => ""
]
]
]
2 => array:3 [▼
"service" => "YES"
"description" => "Yakin Esok Sampai"
"cost" => array:1 [▼
0 => array:3 [▼
"value" => 72000
"etd" => "1-1"
"note" => ""
]
]
]
]
我一直在尝试使用array_search
array_key_exists
和更多的数组函数,但还是没有运气。
我以前通过这段代码已经达到了这个结果
$costsOKE = $costObj['rajaongkir']['results'][0]['costs'][0]['cost'][0]['value']; //OKE
$costsREG = $costObj['rajaongkir']['results'][0]['costs'][1]['cost'][0]['value']; //REG
$costsYES = $costObj['rajaongkir']['results'][0]['costs'][2]['cost'][0]['value']; //YES
问题是这太静态了,因为有时它并不总是带有“ OKE”或“ YES”
我不知道如何具体检查数组的某些“键”
喜欢
if(0['service'] == 'OKE') //I already have input field for service
{
$value = ??? //should be 40000 here and this is what I need
}
如何用最简单的方法实现这一目标?
先感谢
答案 0 :(得分:1)
Laravel具有内置的辅助程序Arr::get()
,用于使用“点表示法”从数组中提取嵌套值。
Laravel 6.x Arr::get() docs
use Illuminate\Support\Arr;
$costsOKE = Arr::get($costObj, 'rajaongkir.results.0.costs.0.cost.0.value');//OKE
$costsREG = Arr::get($costObj, 'rajaongkir.results.0.costs.1.cost.0.value');//REG
$costsYES = Arr::get($costObj, 'rajaongkir.results.0.costs.2.cost.0.value');//YES
或者从您提供的数组中获取:
$costsOKE = Arr::get($costObj, '0.cost.0.value');//OKE
$costsREG = Arr::get($costObj, '1.cost.0.value');//REG
$costsYES = Arr::get($costObj, '2.cost.0.value');//YES
如果该值不存在,则结果为null
。
对于Laravel <= 5.6,您可以改用array_get
。
答案 1 :(得分:0)
如果我正确理解了您的问题,则可以尝试通过以下方式进行操作:
function leerreservas($pnr) {
error_reporting(0);
//ini_set('display_errors', true);
//ini_set('display_startup_errors', true);
$oDate = new DateTime();
$timestamp = $oDate->format('Y-m-d\Th:i:sP');
$timetolive = $oDate->add(new DateInterval('PT1H'))->format('Y-m-d\Th:i:sP');
$Locator=$pnr;
$BinarySecurityToken=selectokken(); $endpoint=selecendpoint();
$sabre=sabredatosusuario();
$namespaces="http://webservices.sabre.com/pnrbuilder/v1_15";
$test = '<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:eb="http://www.ebxml.org/namespaces/messageHeader"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Header>
<eb:MessageHeader SOAP-ENV:mustUnderstand="1" eb:version="1.0">
<eb:From>
<eb:PartyId>Client</eb:PartyId>
</eb:From>
<eb:To>
<eb:PartyId>SWS</eb:PartyId>
</eb:To>
<eb:CPAId>'.$sabre['ipcc'].'</eb:CPAId>
<eb:ConversationId>MyConversationID</eb:ConversationId>
<eb:Service>Service</eb:Service>
<eb:Action>getReservationRQ</eb:Action>
<eb:MessageData>
<eb:MessageId>'.$timestamp.'</eb:MessageId>
<eb:Timestamp>'.$timetolive.'</eb:Timestamp>
</eb:MessageData>
</eb:MessageHeader>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext"
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/12/utility">
<wsse:BinarySecurityToken>'.$BinarySecurityToken.'</wsse:BinarySecurityToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns7:GetReservationRQ xmlns:ns7="'.$namespaces.'" Version="1.15.0">
<ns7:Locator>'.$Locator.'</ns7:Locator>
<ns7:RequestType>Stateful</ns7:RequestType>
<ns7:ReturnOptions UnmaskCreditCard="true">
<ns7:SubjectAreas>
<ns7:SubjectArea>PRICING_INFORMATION</ns7:SubjectArea>
</ns7:SubjectAreas>
</ns7:ReturnOptions>
</ns7:GetReservationRQ>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$location_URL = $endpoint;
$action_URL = $namespaces;
$client = new SoapClient(null, array( 'location' => $location_URL, 'uri' => "",
'trace' => 1, ));
try {
$order_return = $client->__doRequest($test,$location_URL,$action_URL,1);
//Get response from here
if (empty($order_return)) {
echo "vacio";
} else {
$xml2 = reemplazo($order_return);
$xml = simplexml_load_string($xml2, 'SimpleXMLElement', LIBXML_NOCDATA);
$datos = json_decode(json_encode($xml), TRUE);
echo print_r($datos);
}
} catch (SoapFault $exception) {
var_dump(get_class($exception));
var_dump($exception);
}
}
但是请注意,此功能仅适用于您的数组结构。如果结构发生变化,则可能无法正常工作。