3v4l.org

run code in 300+ PHP versions simultaneously
<?php $st = '{"id":"PAY-000000000000000000000000","intent":"sale","state":"approved","cart":"11111111111111111", "payer":{"payment_method":"paypal","status":"VERIFIED","payer_info":{"email":"user@host.com","first_name":"NAME","middle_name":"XXX","last_name":"LNAME", "payer_id":"ZZZZZZZZZZZZZ","country_code":"MD"}},"transactions":[{"amount":{"total":"13.49","currency":"USD","details":[]}, "payee":{"merchant_id":"ZZZZZZZZZZZZZ","email":"PAYER@HOST.com"},"item_list":{"items":[{"name":"Package", "description":"Package","price":"8.49","currency":"USD","quantity":1},{"name":"donate","description":"Donate","price":"5.00", "currency":"USD","quantity":1}]},"related_resources":[{"sale":{"id":"0NRFFFFFFFFFFFFFF","state":"completed","amount":{"total":"13.49", "currency":"USD","details":{"subtotal":"13.49"}},"payment_mode":"INSTANT_TRANSFER","protection_eligibility":"INELIGIBLE","transaction_fee":{"value":"0.89", "currency":"USD"},"parent_payment":"PAY-000000000000000000000000","create_time":"2018-01-01T20:35:19Z","update_time":"2018-01-01T20:35:19Z", "links":[{"href":"https:\/\/api.paypal.com\/v1\/payments\/sale\/00000000000000000","rel":"self","method":"GET"}, {"href":"https:\/\/api.paypal.com\/v1\/payments\/sale\/00000000000000000\/refund","rel":"refund","method":"POST"}, {"href":"https:\/\/api.paypal.com\/v1\/payments\/payment\/PAY-000000000000000000000000","rel":"parent_payment","method":"GET"}]}}]}], "create_time":"2018-01-00T20:35:20Z","links":[{"href":"https:\/\/api.paypal.com\/v1\/payments\/payment\/PAY-000000000000000000000000", "rel":"self","method":"GET"}]}'; $reply = json_decode(trim(stripcslashes($st), '"'), true); echo "\nLets go!\n"; $fee = getKeyFromArray('transaction_fee', $reply); echo "\n++++++++++++++++++++++++++++\n"; print_r($fee); $fee = getFee($st); echo "\n++++++++++++++++++++++++++++\n"; print_r($fee); $fee = getTheFuckingKeyFromTheFuckingArrayWithoutFuckingTroubles('transaction_fee', $reply); echo PHP_EOL, "Why is PHP such a bad language?!", PHP_EOL; echo PHP_EOL, "++++++++++++++++++++++++++++", PHP_EOL; echo PHP_EOL, print_r($fee, 1); function getFee($s) { // we need to locate something like this // "transaction_fee":{"value":"0.89","currency":"USD"} $p = strpos($s, 'transaction_fee'); $max = strlen($s); echo "\nGot p = $p\n"; if ($p > 0) { // scan for '{' to '}' $p1 = $p; while ($s[$p1] != '{' && $p1 < $max) { $p1++; } echo "\nGot p1 = $p1\n"; $p2 = $p1; while ($s[$p2] != '}' && $p2 < $max) { $p2++; } echo "\nGot p2 = $p2\n"; $r = substr($s, $p1, $p2 - $p1).'}'; return @json_decode($r, true); //return $r; } else { echo "WTF2????"; return null; } } function getKeyFromArray($dataItemName, $array) { foreach ($array as $key => $value) { echo "\nChecking ".(string)$key."\n"; if ((string)$key == $dataItemName) { echo "\nGot needle!\n"; return $value; } if (is_array($value)) { echo "\n ".(string)$key." is an array, going deeper.\n"; echo "\n============================\n"; print_r($value); echo "\n============================\n"; return getKeyFromArray($dataItemName, $value); } } echo "\nWTF???\n"; return null; } /** * @param string $theFuckingKey * @param array $theFuckingArray * * @return mixed|null */ function getTheFuckingKeyFromTheFuckingArrayWithoutFuckingTroubles/* please */($theFuckingKey, array $theFuckingArray) { foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($theFuckingArray), RecursiveIteratorIterator::SELF_FIRST) as $key => $value) { if ($key === $theFuckingKey) { return $value; } } return null; }

preferences:
105.7 ms | 1726 KiB | 5 Q