Read array with json post

0

I receive a array of size (1) sent by POST of a JSON :

Array
(
    [{
        "promotion": {
            "name": "PRUEBAS_|_NO_BORRAR",
            "subdomain": "pruebas"
        },
        "playNumber": 9,
        "playData": {
            "code": "3MBY9P",
            "email": "cruiz@po_es",
            "NOMBRE": "Carlos_ruizs"
        },
        "prize": "NO_PREMIO",
        "device": "Macintosh",
        "platform": "OS_X",
        "browser": "Firefox",
        "ip": "62_117_146_31",
        "playDateTime": "2017-03-21_12:52:36"
    }] => 
)

I've tried to go through it in every way. I have also made a decode of array parameter received, but still have the same format:

$array = json_decode(json_encode($json), true, 512);

To go through it I tried:

foreach ($array as $indice => $valor) {
      echo $indice;
      var_dump($valor);
}

But without result. Any ideas on how to solve it?


I also tried:

 $array=json_decode($json, true);

but Apache gives me this answer

  

: Got error 'PHP message: PHP Warning: json_decode () expects parameter 1 to be string, given array

And I've tried like that (Apache does not complain, but I do not get anything)

$array= json_decode(json_encode($json),true));
    
asked by Miguel Tabernero 23.03.2017 в 07:27
source

1 answer

0

If it arrives to you as a string, play the decode with the second parameter in true to convert it to an array, otherwise you will make them objects (stdClass)

Try here

<?php

$json = '[{
    "promotion": {
        "name": "PRUEBAS_|_NO_BORRAR",
        "subdomain": "pruebas"
    },
    "playNumber": 9,
    "playData": {
        "code": "3MBY9P",
        "email": "cruiz@po_es",
        "NOMBRE": "Carlos_ruizs"
    },
    "prize": "NO_PREMIO",
    "device": "Macintosh",
    "platform": "OS_X",
    "browser": "Firefox",
    "ip": "62_117_146_31",
    "playDateTime": "2017-03-21_12:52:36"
}]';

$resultado = '';

$arr = json_decode($json, true);

foreach ( $arr as $k ) {

    foreach ( $k as $indice => $valor ) {

        if ( is_array($valor) ) {
            foreach ( $valor as $j => $ivalor ) {
                $resultado .= "$indice [$j] = $ivalor <br>";
            }
        } else {
            $resultado .= "$indice: $valor <br>";
        }
    }    
}

echo $resultado;
    
answered by 23.03.2017 в 07:53