如何仅从具有特定值的 curl(php) 中获取键名
How to get only the key names from a curl(php) that have certain value
所以我想做的是只获取具有所需标签的键名。
我正在做一个 curl 得到这个 url https://api.mercadolibre.com/categories/MLB1002/attributes
使用此代码:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.mercadolibre.com/categories/MLB1002/attributes',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer APP_USR-868446844464533-040421-c41459517982417c3732856e7e872541-224564420'
),
));
$response = curl_exec($curl);
curl_close($curl);
$array = json_decode(trim($response), TRUE);
foreach($array as $item) {
echo nl2br($item['name'] . "\n");
}
我仅在 PostMan 上解析得到的值是:
{
"id": "BRAND",
"name": "Marca",
"tags": {
"catalog_required": true,
"required": true
},
"hierarchy": "PARENT_PK",
"relevance": 1,
"value_type": "string",
"value_max_length": 255,
"values": [
{
但是是一个有 2000 多行的大列表
我想做的是在我的 foreach 中放置一个 if 以仅获取具有标签“required=true”的“名称”...我可以用任何方式做到这一点吗?
你可以签入foreach
foreach($array as $item) {
if($item['tags']['required']){
echo nl2br($item['name'] . "\n");
}
}
所以我想做的是只获取具有所需标签的键名。 我正在做一个 curl 得到这个 url https://api.mercadolibre.com/categories/MLB1002/attributes 使用此代码:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.mercadolibre.com/categories/MLB1002/attributes',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer APP_USR-868446844464533-040421-c41459517982417c3732856e7e872541-224564420'
),
));
$response = curl_exec($curl);
curl_close($curl);
$array = json_decode(trim($response), TRUE);
foreach($array as $item) {
echo nl2br($item['name'] . "\n");
}
我仅在 PostMan 上解析得到的值是:
{
"id": "BRAND",
"name": "Marca",
"tags": {
"catalog_required": true,
"required": true
},
"hierarchy": "PARENT_PK",
"relevance": 1,
"value_type": "string",
"value_max_length": 255,
"values": [
{
但是是一个有 2000 多行的大列表
我想做的是在我的 foreach 中放置一个 if 以仅获取具有标签“required=true”的“名称”...我可以用任何方式做到这一点吗?
你可以签入foreach
foreach($array as $item) {
if($item['tags']['required']){
echo nl2br($item['name'] . "\n");
}
}