Skip to content
Advertisement

how to create associative array from NOAA data

I’m trying to convert this to an associative array with json_decode but I’m messing up somewhere.

$noaaRaw looks like:

Array ( [0] => { "predictions" : [ [1] => {"t":"2021-01-01 00:29", "v":"8.870", "type":"H"},{"t":"2021-01-01 06:22", "v":"0.807", "type":"L"},{"t":"2021-01-01 12:31", "v":"10.383", "type":"H"},{"t":"2021-01-01 18:58", "v":"-0.402", "type":"L"},{"t":"2021-01-02 01:11", "v":"8.988", "type":"H"},{"t":"2021-01-02 07:05", "v":"0.720", "type":"L"},{"t":"2021-01-02 13:15", "v":"10.374", "type":"H"},{"t":"2021-01-02 19:41", "v":"-0.428", "type":"L"},{"t":"2021-01-03 01:53", "v":"9.131", "type":"H"},{"t":"2021-01-03 07:52", "v":"0.654", "type":"L"},{"t":"2021-01-03 14:00", "v":"10.271", "type":"H"},{"t":"2021-01-03 20:26", "v":"-0.378", "type":"L"},{"t":"2021-01-04 02:39", "v":"9.297", "type":"H"},{"t":"2021-01-04 08:42", "v":"0.604", "type":"L"},{"t":"2021-01-04 14:50", "v":"10.066", "type":"H"},{"t":"2021-01-04 21:14", "v":"-0.261", "type":"L"},{"t":"2021-01-05 03:28", "v":"9.485", "type":"H"},{"t":"2021-01-05 09:37", "v":"0.546", "type":"L"},{"t":"2021-01-05 15:44", "v":"9.788", "type":"H"},{"t":"2021-01-05 22:06", "v":"-0.103", "type":"L"},{"t":"2021-01-06 04:20", "v":"9.699", "type":"H"},{"t":"2021-01-06 10:35", "v":"0.440", "type":"L"},{"t":"2021-01-06 16:42", "v":"9.499", "type":"H"},{"t":"2021-01-06 23:00", "v":"0.061", "type":"L"},{"t":"2021-01-07 05:16", "v":"9.938", "type":"H"},{"t":"2021-01-07 11:36", "v":"0.265", "type":"L"},{"t":"2021-01-07 17:44", "v":"9.259", "type":"H"},{"t":"2021-01-07 23:57", "v":"0.202", "type":"L"},{"t":"2021-01-08 06:13", "v":"10.193", "type":"H"},{"t":"2021-01-08 12:38", "v":"0.022", "type":"L"},{"t":"2021-01-08 18:47", "v":"9.121", "type":"H"},{"t":"2021-01-09 00:55", "v":"0.284", "type":"L"},{"t":"2021-01-09 07:12", "v":"10.464", "type":"H"},{"t":"2021-01-09 13:40", "v":"-0.291", "type":"L"},{"t":"2021-01-09 19:52", "v":"9.121", "type":"H"},{"t":"2021-01-10 01:54", "v":"0.272", "type":"L"},{"t":"2021-01-10 08:11", "v":"10.745", "type":"H"},{"t":"2021-01-10 14:41", "v":"-0.645", "type":"L"},{"t":"2021-01-10 20:54", "v":"9.238", "type":"H"},{"t":"2021-01-11 02:52", "v":"0.174", "type":"L"},{"t":"2021-01-11 09:08", "v":"10.995", "type":"H"},{"t":"2021-01-11 15:38", "v":"-0.959", "type":"L"},{"t":"2021-01-11 21:52", "v":"9.387", "type":"H"},{"t":"2021-01-12 03:47", "v":"0.055", "type":"L"},{"t":"2021-01-12 10:02", "v":"11.146", "type":"H"},{"t":"2021-01-12 16:31", "v":"-1.151", "type":"L"},{"t":"2021-01-12 22:46", "v":"9.494", "type":"H"},{"t":"2021-01-13 04:40", "v":"-0.012", "type":"L"},{"t":"2021-01-13 10:54", "v":"11.155", "type":"H"},{"t":"2021-01-13 17:22", "v":"-1.183", "type":"L"},{"t":"2021-01-13 23:38", "v":"9.532", "type":"H"},{"t":"2021-01-14 05:31", "v":"0.008", "type":"L"},{"t":"2021-01-14 11:45", "v":"11.018", "type":"H"},{"t":"2021-01-14 18:11", "v":"-1.056", "type":"L"},{"t":"2021-01-15 00:27", "v":"9.501", "type":"H"},{"t":"2021-01-15 06:20", "v":"0.123", "type":"L"},{"t":"2021-01-15 12:35", "v":"10.746", "type":"H"},{"t":"2021-01-15 18:58", "v":"-0.785", "type":"L"},{"t":"2021-01-16 01:14", "v":"9.406", "type":"H"},{"t":"2021-01-16 07:09", "v":"0.327", "type":"L"},{"t":"2021-01-16 13:23", "v":"10.356", "type":"H"},{"t":"2021-01-16 19:43", "v":"-0.400", "type":"L"},{"t":"2021-01-17 02:00", "v":"9.261", "type":"H"},{"t":"2021-01-17 07:57", "v":"0.598", "type":"L"},{"t":"2021-01-17 14:10", "v":"9.872", "type":"H"},{"t":"2021-01-17 20:28", "v":"0.058", "type":"L"},{"t":"2021-01-18 02:45", "v":"9.089", "type":"H"},{"t":"2021-01-18 08:46", "v":"0.898", "type":"L"},{"t":"2021-01-18 14:58", "v":"9.339", "type":"H"},{"t":"2021-01-18 21:14", "v":"0.539", "type":"L"},{"t":"2021-01-19 03:32", "v":"8.921", "type":"H"},{"t":"2021-01-19 09:38", "v":"1.178", "type":"L"},{"t":"2021-01-19 15:49", "v":"8.813", "type":"H"},{"t":"2021-01-19 22:01", "v":"0.991", "type":"L"},{"t":"2021-01-20 04:20", "v":"8.789", "type":"H"},{"t":"2021-01-20 10:31", "v":"1.387", "type":"L"},{"t":"2021-01-20 16:42", "v":"8.355", "type":"H"},{"t":"2021-01-20 22:50", "v":"1.371", "type":"L"},{"t":"2021-01-21 05:09", "v":"8.712", "type":"H"},{"t":"2021-01-21 11:26", "v":"1.497", "type":"L"},{"t":"2021-01-21 17:37", "v":"8.008", "type":"H"},{"t":"2021-01-21 23:40", "v":"1.652", "type":"L"},{"t":"2021-01-22 06:00", "v":"8.706", "type":"H"},{"t":"2021-01-22 12:22", "v":"1.495", "type":"L"},{"t":"2021-01-22 18:33", "v":"7.805", "type":"H"},{"t":"2021-01-23 00:32", "v":"1.810", "type":"L"},{"t":"2021-01-23 06:52", "v":"8.793", "type":"H"},{"t":"2021-01-23 13:18", "v":"1.364", "type":"L"},{"t":"2021-01-23 19:31", "v":"7.768", "type":"H"},{"t":"2021-01-24 01:24", "v":"1.822", "type":"L"},{"t":"2021-01-24 07:45", "v":"8.988", "type":"H"},{"t":"2021-01-24 14:12", "v":"1.100", "type":"L"},{"t":"2021-01-24 20:25", "v":"7.887", "type":"H"},{"t":"2021-01-25 02:15", "v":"1.681", "type":"L"},{"t":"2021-01-25 08:34", "v":"9.283", "type":"H"},{"t":"2021-01-25 15:01", "v":"0.738", "type":"L"},{"t":"2021-01-25 21:14", "v":"8.112", "type":"H"},{"t":"2021-01-26 03:03", "v":"1.419", "type":"L"},{"t":"2021-01-26 09:20", "v":"9.635", "type":"H"},{"t":"2021-01-26 15:46", "v":"0.332", "type":"L"},{"t":"2021-01-26 21:59", "v":"8.387", "type":"H"},{"t":"2021-01-27 03:49", "v":"1.088", "type":"L"},{"t":"2021-01-27 10:03", "v":"9.997", "type":"H"},{"t":"2021-01-27 16:29", "v":"-0.070", "type":"L"},{"t":"2021-01-27 22:41", "v":"8.685", "type":"H"},{"t":"2021-01-28 04:32", "v":"0.735", "type":"L"},{"t":"2021-01-28 10:45", "v":"10.324", "type":"H"},{"t":"2021-01-28 17:10", "v":"-0.434", "type":"L"},{"t":"2021-01-28 23:22", "v":"8.995", "type":"H"},{"t":"2021-01-29 05:16", "v":"0.393", "type":"L"},{"t":"2021-01-29 11:27", "v":"10.582", "type":"H"},{"t":"2021-01-29 17:52", "v":"-0.727", "type":"L"},{"t":"2021-01-30 00:03", "v":"9.311", "type":"H"},{"t":"2021-01-30 06:00", "v":"0.088", "type":"L"},{"t":"2021-01-30 12:11", "v":"10.734", "type":"H"},{"t":"2021-01-30 18:34", "v":"-0.916", "type":"L"},{"t":"2021-01-31 00:45", "v":"9.619", "type":"H"},{"t":"2021-01-31 06:45", "v":"-0.150", "type":"L"},{"t":"2021-01-31 12:55", "v":"10.748", "type":"H"},{"t":"2021-01-31 19:17", "v":"-0.971", "type":"L"},{"t":"2021-02-01 01:28", "v":"9.887", "type":"H"},{"t":"2021-02-01 07:32", "v":"-0.290", "type":"L"},{"t":"2021-02-01 13:42", "v":"10.597", "type":"H"},{"t":"2021-02-01 20:01", "v":"-0.872", "type":"L"},{"t":"2021-02-02 02:13", "v":"10.081", "type":"H"},{"t":"2021-02-02 08:22", "v":"-0.314", "type":"L"},{"t":"2021-02-02 14:31", "v":"10.279", "type":"H"},{"t":"2021-02-02 20:49", "v":"-0.624", "type":"L"},{"t":"2021-02-03 03:01", "v":"10.178", "type":"H"},{"t":"2021-02-03 09:16", "v":"-0.235", "type":"L"},{"t":"2021-02-03 15:25", "v":"9.837", "type":"H"},{"t":"2021-02-03 21:40", "v":"-0.270", "type":"L"},{"t":"2021-02-04 03:53", "v":"10.185", "type":"H"},{"t":"2021-02-04 10:14", "v":"-0.107", "type":"L"},{"t":"2021-02-04 16:23", "v":"9.357", "type":"H"},{"t":"2021-02-04 22:35", "v":"0.117", "type":"L"},{"t":"2021-02-05 04:50", "v":"10.138", "type":"H"},{"t":"2021-02-05 11:16", "v":"0.007", "type":"L"},{"t":"2021-02-05 17:26", "v":"8.939", "type":"H"},{"t":"2021-02-05 23:34", "v":"0.461", "type":"L"},{"t":"2021-02-06 05:50", "v":"10.088", "type":"H"},{"t":"2021-02-06 12:20", "v":"0.055", "type":"L"},{"t":"2021-02-06 18:33", "v":"8.673", "type":"H"},{"t":"2021-02-07 00:35", "v":"0.690", "type":"L"},{"t":"2021-02-07 06:54", "v":"10.093", "type":"H"},{"t":"2021-02-07 13:25", "v":"-0.014", "type":"L"},{"t":"2021-02-07 19:41", "v":"8.624", "type":"H"},{"t":"2021-02-08 01:38", "v":"0.744", "type":"L"},{"t":"2021-02-08 07:58", "v":"10.201", "type":"H"},{"t":"2021-02-08 14:29", "v":"-0.213", "type":"L"},{"t":"2021-02-08 20:47", "v":"8.777", "type":"H"},{"t":"2021-02-09 02:40", "v":"0.619", "type":"L"},{"t":"2021-02-09 08:59", "v":"10.386", "type":"H"},{"t":"2021-02-09 15:28", "v":"-0.465", "type":"L"},{"t":"2021-02-09 21:44", "v":"9.021", "type":"H"},{"t":"2021-02-10 03:36", "v":"0.403", "type":"L"},{"t":"2021-02-10 09:55", "v":"10.552", "type":"H"},{"t":"2021-02-10 16:20", "v":"-0.660", "type":"L"},{"t":"2021-02-10 22:36", "v":"9.249", "type":"H"},{"t":"2021-02-11 04:28", "v":"0.202", "type":"L"},{"t":"2021-02-11 10:45", "v":"10.623", "type":"H"},{"t":"2021-02-11 17:08", "v":"-0.734", "type":"L"},{"t":"2021-02-11 23:23", "v":"9.411", "type":"H"},{"t":"2021-02-12 05:17", "v":"0.080", "type":"L"},{"t":"2021-02-12 11:33", "v":"10.568", "type":"H"},{"t":"2021-02-12 17:52", "v":"-0.671", "type":"L"},{"t":"2021-02-13 00:07", "v":"9.496", "type":"H"},{"t":"2021-02-13 06:03", "v":"0.057", "type":"L"},{"t":"2021-02-13 12:17", "v":"10.387", "type":"H"},{"t":"2021-02-13 18:34", "v":"-0.478", "type":"L"},{"t":"2021-02-14 00:48", "v":"9.504", "type":"H"},{"t":"2021-02-14 06:47", "v":"0.137", "type":"L"},{"t":"2021-02-14 13:00", "v":"10.091", "type":"H"},{"t":"2021-02-14 19:14", "v":"-0.175", "type":"L"},{"t":"2021-02-15 01:28", "v":"9.442", "type":"H"},{"t":"2021-02-15 07:30", "v":"0.309", "type":"L"},{"t":"2021-02-15 13:42", "v":"9.699", "type":"H"},{"t":"2021-02-15 19:54", "v":"0.211", "type":"L"},{"t":"2021-02-16 02:07", "v":"9.318", "type":"H"},{"t":"2021-02-16 08:14", "v":"0.557", "type":"L"},{"t":"2021-02-16 14:25", "v":"9.239", "type":"H"},{"t":"2021-02-16 20:34", "v":"0.649", "type":"L"},{"t":"2021-02-17 02:47", "v":"9.146", "type":"H"},{"t":"2021-02-17 09:00", "v":"0.851", "type":"L"},{"t":"2021-02-17 15:10", "v":"8.748", "type":"H"},{"t":"2021-02-17 21:17", "v":"1.097", "type":"L"},{"t":"2021-02-18 03:31", "v":"8.947", "type":"H"},{"t":"2021-02-18 09:48", "v":"1.145", "type":"L"},{"t":"2021-02-18 15:59", "v":"8.279", "type":"H"},{"t":"2021-02-18 22:04", "v":"1.510", "type":"L"},{"t":"2021-02-19 04:18", "v":"8.760", "type":"H"},{"t":"2021-02-19 10:41", "v":"1.385", "type":"L"},{"t":"2021-02-19 16:52", "v":"7.889", "type":"H"},{"t":"2021-02-19 22:54", "v":"1.836", "type":"L"},{"t":"2021-02-20 05:09", "v":"8.633", "type":"H"},{"t":"2021-02-20 11:36", "v":"1.520", "type":"L"},{"t":"2021-02-20 17:48", "v":"7.633", "type":"H"},{"t":"2021-02-20 23:46", "v":"2.029", "type":"L"},{"t":"2021-02-21 06:03", "v":"8.618", "type":"H"},{"t":"2021-02-21 12:32", "v":"1.505", "type":"L"},{"t":"2021-02-21 18:47", "v":"7.560", "type":"H"},{"t":"2021-02-22 00:41", "v":"2.049", "type":"L"},{"t":"2021-02-22 07:00", "v":"8.762", "type":"H"},{"t":"2021-02-22 13:30", "v":"1.302", "type":"L"},{"t":"2021-02-22 19:45", "v":"7.699", "type":"H"},{"t":"2021-02-23 01:37", "v":"1.862", "type":"L"},{"t":"2021-02-23 07:55", "v":"9.082", "type":"H"},{"t":"2021-02-23 14:24", "v":"0.917", "type":"L"},{"t":"2021-02-23 20:38", "v":"8.029", "type":"H"},{"t":"2021-02-24 02:30", "v":"1.477", "type":"L"},{"t":"2021-02-24 08:47", "v":"9.536", "type":"H"},{"t":"2021-02-24 15:13", "v":"0.413", "type":"L"},{"t":"2021-02-24 21:26", "v":"8.486", "type":"H"},{"t":"2021-02-25 03:19", "v":"0.954", "type":"L"},{"t":"2021-02-25 09:34", "v":"10.039", "type":"H"},{"t":"2021-02-25 15:58", "v":"-0.120", "type":"L"},{"t":"2021-02-25 22:09", "v":"9.007", "type":"H"},{"t":"2021-02-26 04:06", "v":"0.376", "type":"L"},{"t":"2021-12-26 10:05", "v":"1.338", "type":"L"},{"t":"2021-12-26 16:11", "v":"9.196", "type":"H"},{"t":"2021-12-26 22:33", "v":"0.542", "type":"L"},{"t":"2021-12-27 04:48", "v":"9.151", "type":"H"},{"t":"2021-12-27 11:00", "v":"1.075", "type":"L"},{"t":"2021-12-27 17:06", "v":"9.105", "type":"H"},{"t":"2021-12-27 23:25", "v":"0.520", "type":"L"},{"t":"2021-12-28 05:39", "v":"9.557", "type":"H"},{"t":"2021-12-28 11:58", "v":"0.690", "type":"L"},{"t":"2021-12-28 18:04", "v":"9.090", "type":"H"},{"t":"2021-12-29 00:18", "v":"0.442", "type":"L"},{"t":"2021-12-29 06:33", "v":"10.037", "type":"H"},{"t":"2021-12-29 12:57", "v":"0.202", "type":"L"},{"t":"2021-12-29 19:04", "v":"9.172", "type":"H"},{"t":"2021-12-30 01:13", "v":"0.294", "type":"L"},{"t":"2021-12-30 07:28", "v":"10.562", "type":"H"},{"t":"2021-12-30 13:56", "v":"-0.361", "type":"L"},{"t":"2021-12-30 20:05", "v":"9.358", "type":"H"},{"t":"2021-12-31 02:10", "v":"0.069", "type":"L"},{"t":"2021-12-31 08:23", "v":"11.089", "type":"H"},{"t":"2021-12-31 14:53", "v":"-0.936", "type":"L"},{"t":"2021-12-31 21:02", "v":"9.406", "type":"H"} [2] => ]} )

How do I convert that to an associative array?

<?php
  $noaaRaw = file('https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?begin_date=20210101&end_date=20211231&station=8443970&product=predictions&datum=MLLW&time_zone=lst_ldt&interval=hilo&units=english&application=Bob_S&format=json');


  print_r($noaaRaw);
  echo gettype($noaaRaw[0]['predictions'][1]);
  $noaa = json_decode($noaaRaw[0]['predictions'][1],true);
  print_r($noaa);
?>

Advertisement

Answer

file returns an array of lines. You want to use file_get_contents to get a string to decode:

$noaaRaw = file_get_contents('https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?begin_date=20210101&end_date=20211231&station=8443970&product=predictions&datum=MLLW&time_zone=lst_ldt&interval=hilo&units=english&application=Bob_S&format=json');

$result = json_decode($noaaRaw, true);

It is also possible after that, that you may need to enable allow_url_fopen.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement