0

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

5
  • 1
    Show us a bit of your code..... just to get us started from a sensible place for you to follow Commented May 19, 2016 at 14:28
  • @RiggsFolly I was tried to move values to new array but I removed cause It was wrong Commented May 19, 2016 at 14:33
  • 1
    In your case, there are 20 entries for x and y on both cases. If x and y always appears, you can just use count(second_param). Or do you need to insert the attribute hour inside the json object? Commented May 19, 2016 at 14:34
  • I need to add the attribute hour in all Commented May 19, 2016 at 14:36
  • yes, x and y always appear Commented May 19, 2016 at 14:36

2 Answers 2

1

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
    )
Sign up to request clarification or add additional context in comments.

Comments

0

Loop into array, decode the json and add hour inside the elements:

$data = array(
    0 => 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}]'
    ),
    1 => 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}]'
    )
);

foreach ($data as $k => $item) {
    $arr = json_decode($item['second_param'], true);
    foreach ($arr as $key => $value) {
        $arr[$key]['hour'] = 11;
    }
    $data[$k] = json_encode($arr);
}

var_dump($data);

And encode it back at the end, if needed.

2 Comments

the hour is not always 11
then insert whatever you need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.