Unclear behaviour of arrays in php

  Kiến thức lập trình

I can’t rewrite values in array

<?php

$req_url = 'https://open.er-api.com/v6/latest/USD';
$response_json = file_get_contents($req_url); // get json as string

$response = json_decode($response_json, true); // json string to array

$key = array_keys($response["rates"]); // get all keys from array

    if ('success' === $response["result"]) { //check if success

        for ($i = 0; $i < sizeof($response["rates"]); $i++) {
            $response["rates"][$key[$i]] = round($response["rates"][$key[$i]], 2); //do nothing to array
        }

        for ($i = 0; $i < sizeof($response["rates"]); $i++) {
            $response["rates"][$key[$i]] = (string)round($response["rates"][$key[$i]], 2); // change array
        }

    }

I try all kinds of solutions what i foud online and chat gpt can’t handle it

New contributor

Voha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT