Browse JSON array from PHP

1
JSON:

    {

    "_labels": {
        "a": "DOLARTODAY",
        "a1": "DOLAR CUCUTA",
        "b": "IMPLICITO",
        "c": "DICOM",
        "d": "DOLAR BITCOIN",
        "e": "DIPRO"
    },
    "_timestamp": {
        "epoch": "1520611865",
        "fecha": "Marzo 9, 2018 12:11 PM",
        "fecha_corta": "Mar 9, 2018",
        "fecha_corta2": "Mar 2018",
        "fecha_nice": "Marzo 9, 2018",
        "dia": "Viernes",
        "dia_corta": "Vie"
    },
    "USD": {
        "transferencia": 215778.05,
        "transfer_cucuta": 215778.05,
        "efectivo": 22327.24,
        "efectivo_real": 232758.62,
        "efectivo_cucuta": 232758.62,
        "promedio": 215778.05,
        "promedio_real": 39807.50,
        "cencoex": 10.00,
        "sicad1": 236044.85,
        "sicad2": 39807.50,
        "bitcoin_ref": 236044.85,
        "localbitcoin_ref": 240165.62,
        "dolartoday": 215778.05
    },
    "EUR": {
        "transferencia": 265389.74,
        "transfer_cucuta": 265389.74,
        "efectivo": 27504.93,
        "efectivo_real": 286730.69,
        "efectivo_cucuta": 286730.69,
        "promedio": 265389.74,
        "promedio_real": 49038.86,
        "cencoex": 12.32,
        "sicad1": 290783.65,
        "sicad2": 49038.86,
        "dolartoday": 265389.74
    },
    "COL": {
        "efectivo": 0.0116,
        "transfer": 0.0116,
        "compra": 0.0116,
        "venta": 0.01
    },
    "GOLD": {
        "rate": 1322.51
    },
    "USDVEF": {
        "rate": 10.2087
    },
    "USDCOL": {
        "setfxsell": 2700.00,
        "setfxbuy": 2650.00,
        "rate": 3447.00,
        "ratecash": 2700.00,
        "ratetrm": 2700.00,
        "trmfactor": 0.2,
        "trmfactorcash": 0.06
    },
    "EURUSD": {
        "rate": 1.23188
    },
    "BCV": {
        "fecha": "1520308800",
        "fecha_nice": "Marzo 6, 2018",
        "liquidez": "210.858.494.946",
        "reservas": "9.444.000"
    },
    "MISC": {
        "petroleo": "59,28",
        "reservas": "9,4"
        }
    }

I want to go through the JSON array through PHP. This is my Code

<?
$url = "http://s3.amazonaws.com/dolartoday/data.json";
//$xml = file_get_contents($url);
$json = file_get_contents($url);
$obj = json_decode($xml);

var_dump($obj);

$obj = array();            
foreach ($obj as $respuesta){
    $obj[$respuesta->USD]["promedio"] = $respuesta['resultado'] -> array(); 
}

$datos['USD-Promedio'] = $respuesta;
echo json_encode($datos);
?>

When I execute it, it gives me NULL

    
asked by Andrea Valentina 09.03.2018 в 18:38
source

2 answers

1

Based on this comment , you I'd say you do not need to iterate the object, you just need to access the value you're looking for.

Example:

$url = "http://s3.amazonaws.com/dolartoday/data.json";
$json = file_get_contents($url);
$obj = json_decode($json);

$datos['USD-Promedio'] = $obj->USD->promedio;
echo json_encode($datos);
    
answered by 09.03.2018 / 19:25
source
0

Change this line

$obj = json_decode($xml);

Because of this

$obj = json_decode($json);

It gives you null when you dumpeas pk you are passing json_decode a variable that does not exist.

A tip: In the line before your foreach do not initialize the variable $ obj because it will never enter your cycle, try to create another variable and the changes reflect it in that new one.

    
answered by 09.03.2018 в 18:53