简体   繁体   中英

How to access dynamically value of an array from API Response in PHP?

I'm not really good with array, and need some help,

I need to specifically retrieve only "value" from this given array, for example of this given array below, I only need the "40000" value.

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" => ""
      ]
    ]
  ]
]

I've been trying to use array_search array_key_exists ,and many more array functions, but still no luck..

I've achieved this result before by this code

 $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

the problem is this is too static, because sometimes it doesnt always have "OKE" or "YES"

I don't know how to specifically check with certain "key" of an array

like,

if(0['service'] == 'OKE') //I already have input field for service
{
 $value = ??? //should be 40000 here and this is what I need
}

how to achieve this with most simple method?

thanks before

Laravel has a built in helper Arr::get() for pulling nested values from arrays using 'dot notation'. 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

Or from the array you gave:

$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

The result will be null if the value does not exist.

For Laravel <= 5.6, you can use array_get instead.

If I understood your question correctly, you can try do it this way:

function getValueFromArray($arr, $key)
{
    foreach ($arr as $item) {
        if ( $key === $item['service'] ) {
            return $item['cost'][0]['value'];
        }
    }
}

$value = getValueFromArray($costObj['rajaongkir']['results'][0]['costs'], 'OKE');

But note that this function will be applicable just for your array structure. If structure changes, it might not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM