Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON File to array in PHP

Tags:

json

arrays

php

I am currently trying to read co-ordinates from a JSON file and save them to an array for further use. However I am not managing to read the file successfully. my JSON file has the following format(I am only concerned with cX and cY):

{
    "cX": [
        246,
        1253,
        1464,
        1183
    ],
    "cY": [
        223,
        138,
        383,
        114
    ],
    "scroll": [
        0,
        90,
        0,
        0
    ],
    "sessionID": [
        "f06807c10fb31d5530ad8c4236b94ee2",
        "f06807c10fb31d5530ad8c4236b94ee2",
        "f06807c10fb31d5530ad8c4236b94ee2",
        "f06807c10fb31d5530ad8c4236b94ee2"
    ],
    "Time": [
        "11:30:01",
        "11:30:02",
        "11:30:03",
        "11:30:03"
    ],
    "elem": [
        "H1",
        "H1",
        "H1",
        "BODY"
    ]
}

I am attempting to use the following php code:

<?php
  $string = file_get_contents("./PHP/JSON/clicks.json");
  $json_a = json_decode($string, true);
  for($idx = 0; $idx < count($json_a); $idx++){
    $obj = (Array)$json_a[$idx];
    echo $obj["cX"];
?>
like image 905
KrisF Avatar asked Jul 19 '26 04:07

KrisF


1 Answers

<?php
  $string = file_get_contents("./PHP/JSON/clicks.json");
  $json_a = json_decode($string);
  $cx = $json_a->cX;
  $cy = $json_a->cY;
?>

I hope this help you.

like image 53
Mohamed chiheb Ben jemaa Avatar answered Jul 21 '26 23:07

Mohamed chiheb Ben jemaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!