62009030 62009030 - 6 months ago 9
JSON Question

Parse json in right way in array

I have this array:

array(2) {
[0]=>
array(2) {
["hour"]=>
string(2) "11"
["second_param"]=>
string(434) "[{"x":735.6979166666666,"y":96.0},{"x":1733.0,"y":1.0},{"x":1772.0,"y":3.0},{"x":1848.0,"y":1.0},{"x":1863.0,"y":1.0},{"x":1874.0,"y":2.0},{"x":1929.0,"y":1.0},{"x":1954.0,"y":2.0},{"x":1963.0,"y":1.0},{"x":1976.0,"y":1.0},{"x":1990.0,"y":1.0},{"x":2000.0,"y":15.0},{"x":2800.0,"y":1.0},{"x":2897.0,"y":1.0},{"x":2993.0,"y":1.0},{"x":3037.0,"y":1.0},{"x":3069.0,"y":1.0},{"x":3082.0,"y":1.0},{"x":4532.0,"y":1.0},{"x":4909.0,"y":1.0}]"
}
[1]=>
array(2) {
["hour"]=>
string(2) "16"
["second_param"]=>
string(494) "[{"x":735.5625000000001,"y":16.0},{"x":831.0,"y":1.0},{"x":1029.0,"y":11.0},{"x":1181.0,"y":2.0},{"x":1345.0,"y":1.0},{"x":1609.0,"y":15.0},{"x":1708.0,"y":1.0},{"x":1772.0,"y":1.0},{"x":1841.666666666667,"y":6.0},{"x":1888.6666666666665,"y":3.0},{"x":1941.4999999999998,"y":10.0},{"x":1997.909090909091,"y":33.0},{"x":2030.0,"y":1.0},{"x":2272.0,"y":4.0},{"x":2816.0,"y":2.0},{"x":2954.3333333333335,"y":3.0},{"x":3022.0,"y":2.0},{"x":3170.0,"y":1.0},{"x":4090.0,"y":2.0},{"x":4545.0,"y":5.0}]"
}


on "second_param" I have json. I want to parse that json
I tried json_decode

but than I need to reorganize my array for something like this:

array(68) {
[0]=>
array(3) {
["hour"]=>
string(7) "11"
["x"]=>
fload "2016-02-19"
["y"]=>
float(16.939582156973)
}
[1]=>
array(3) {
["hour"]=>
string(7) "11"
["x"]=>
fload "735.6979166666666"
["y"]=>
float(96.0)
}
[2]=>
array(3) {
["hour"]=>
string(7) "11"
["x"]=>
fload "1733.0"
["y"]=>
float(1.0)
}
[3]=>
array(3) {
["hour"]=>
string(7) "11"
["x"]=>
fload "1772.0"
["y"]=>
float(3.0)
}
[4]=>
array(3) {
["hour"]=>
string(7) "11"
["x"]=>
fload "1848.0"
["y"]=>
float(1.0)
}
......
[5]=>
array(3) {
["hour"]=>
string(7) "16"
["x"]=>
fload "735.5625000000001"
["y"]=>
float(16.0)
}
[6]=>
array(3) {
["hour"]=>
string(7) "16"
["x"]=>
fload "831.0"
["y"]=>
float(1.0)
}
[7]=>
array(3) {
["hour"]=>
string(7) "16"
["x"]=>
fload "1029.0"
["y"]=>
float(11.0)
}
[8]=>
array(3) {
["hour"]=>
string(7) "16"
["x"]=>
fload "1181.0"
["y"]=>
float(2.0)
}


and so on.

So "hour" shoud be repeated the number of times "x" appears

Some ideas?

Thanks

Answer

It is just a case of looping over the outer array, and then for each of the json strings, decode it ( its an array of objects ) and for each occurance or your coordinates adding to a new array that the process builds

$arr = array( array("hour" => "11",
                    "second_param" => '[{"x":735.6979166666666,"y":96.0},{"x":1733.0,"y":1.0},{"x":1772.0,"y":3.0},{"x":1848.0,"y":1.0},{"x":1863.0,"y":1.0},{"x":1874.0,"y":2.0},{"x":1929.0,"y":1.0},{"x":1954.0,"y":2.0},{"x":1963.0,"y":1.0},{"x":1976.0,"y":1.0},{"x":1990.0,"y":1.0},{"x":2000.0,"y":15.0},{"x":2800.0,"y":1.0},{"x":2897.0,"y":1.0},{"x":2993.0,"y":1.0},{"x":3037.0,"y":1.0},{"x":3069.0,"y":1.0},{"x":3082.0,"y":1.0},{"x":4532.0,"y":1.0},{"x":4909.0,"y":1.0}]'
            ),
             array( "hour" => "16",
                    "second_param" => '[{"x":735.5625000000001,"y":16.0},{"x":831.0,"y":1.0},{"x":1029.0,"y":11.0},{"x":1181.0,"y":2.0},{"x":1345.0,"y":1.0},{"x":1609.0,"y":15.0},{"x":1708.0,"y":1.0},{"x":1772.0,"y":1.0},{"x":1841.666666666667,"y":6.0},{"x":1888.6666666666665,"y":3.0},{"x":1941.4999999999998,"y":10.0},{"x":1997.909090909091,"y":33.0},{"x":2030.0,"y":1.0},{"x":2272.0,"y":4.0},{"x":2816.0,"y":2.0},{"x":2954.3333333333335,"y":3.0},{"x":3022.0,"y":2.0},{"x":3170.0,"y":1.0},{"x":4090.0,"y":2.0},{"x":4545.0,"y":5.0}]'
             )
      );

print_r($arr);

$newarr = array();

foreach ($arr as $a) {

    $j = json_decode($a['second_param']);
    foreach( $j as $coord ) {
        $t = array();
        $t['hour'] = $a['hour'];
        $t['x'] = $coord->x;
        $t['y'] = $coord->y;

        $newarr[] = $t;
    }

}

print_r($newarr);

The result is

Array
(
[0] => Array
    (
        [hour] => 11
        [x] => 735.69791666667
        [y] => 96
    )

[1] => Array
    (
        [hour] => 11
        [x] => 1733
        [y] => 1
    )

[2] => Array
    (
        [hour] => 11
        [x] => 1772
        [y] => 3
    )

[3] => Array
    (
        [hour] => 11
        [x] => 1848
        [y] => 1
    )

[4] => Array
    (
        [hour] => 11
        [x] => 1863
        [y] => 1
    )

[5] => Array
    (
        [hour] => 11
        [x] => 1874
        [y] => 2
    )

[6] => Array
    (
        [hour] => 11
        [x] => 1929
        [y] => 1
    )

[7] => Array
    (
        [hour] => 11
        [x] => 1954
        [y] => 2
    )

[8] => Array
    (
        [hour] => 11
        [x] => 1963
        [y] => 1
    )

[9] => Array
    (
        [hour] => 11
        [x] => 1976
        [y] => 1
    )

[10] => Array
    (
        [hour] => 11
        [x] => 1990
        [y] => 1
    )

[11] => Array
    (
        [hour] => 11
        [x] => 2000
        [y] => 15
    )

[12] => Array
    (
        [hour] => 11
        [x] => 2800
        [y] => 1
    )

[13] => Array
    (
        [hour] => 11
        [x] => 2897
        [y] => 1
    )

[14] => Array
    (
        [hour] => 11
        [x] => 2993
        [y] => 1
    )

[15] => Array
    (
        [hour] => 11
        [x] => 3037
        [y] => 1
    )

[16] => Array
    (
        [hour] => 11
        [x] => 3069
        [y] => 1
    )

[17] => Array
    (
        [hour] => 11
        [x] => 3082
        [y] => 1
    )

[18] => Array
    (
        [hour] => 11
        [x] => 4532
        [y] => 1
    )

[19] => Array
    (
        [hour] => 11
        [x] => 4909
        [y] => 1
    )

[20] => Array
    (
        [hour] => 16
        [x] => 735.5625
        [y] => 16
    )

[21] => Array
    (
        [hour] => 16
        [x] => 831
        [y] => 1
    )

[22] => Array
    (
        [hour] => 16
        [x] => 1029
        [y] => 11
    )

[23] => Array
    (
        [hour] => 16
        [x] => 1181
        [y] => 2
    )

[24] => Array
    (
        [hour] => 16
        [x] => 1345
        [y] => 1
    )

[25] => Array
    (
        [hour] => 16
        [x] => 1609
        [y] => 15
    )

[26] => Array
    (
        [hour] => 16
        [x] => 1708
        [y] => 1
    )

[27] => Array
    (
        [hour] => 16
        [x] => 1772
        [y] => 1
    )

[28] => Array
    (
        [hour] => 16
        [x] => 1841.6666666667
        [y] => 6
    )

[29] => Array
    (
        [hour] => 16
        [x] => 1888.6666666667
        [y] => 3
    )

[30] => Array
    (
        [hour] => 16
        [x] => 1941.5
        [y] => 10
    )

[31] => Array
    (
        [hour] => 16
        [x] => 1997.9090909091
        [y] => 33
    )

[32] => Array
    (
        [hour] => 16
        [x] => 2030
        [y] => 1
    )

[33] => Array
    (
        [hour] => 16
        [x] => 2272
        [y] => 4
    )

[34] => Array
    (
        [hour] => 16
        [x] => 2816
        [y] => 2
    )

[35] => Array
    (
        [hour] => 16
        [x] => 2954.3333333333
        [y] => 3
    )

[36] => Array
    (
        [hour] => 16
        [x] => 3022
        [y] => 2
    )

[37] => Array
    (
        [hour] => 16
        [x] => 3170
        [y] => 1
    )

[38] => Array
    (
        [hour] => 16
        [x] => 4090
        [y] => 2
    )

[39] => Array
    (
        [hour] => 16
        [x] => 4545
        [y] => 5
    )