0

i have this data cennik.json file:

{
    "waluta": "PLN",
    "vat": 1.23,
    "00101": {
        "cena": 340,
        "powiazanyZ": "00139"
    },
    "00102": {
        "cena": 325.33,
        "powiazanyZ": "00140"
    },
    "00103": {
        "cena": 306.67,
        "powiazanyZ": "00141"
    },
    "00104": {
        "cena": 289.33,
        "powiazanyZ": "00142"
    },
    "00105": {
        "cena": 276,
        "powiazanyZ": "00143"
    },
    "00106": {
        "cena": 258.67,
        "powiazanyZ": "00144"
    },
    "00107": {
        "cena": 240,
        "powiazanyZ": "00145"
    },
    "00108": {
        "cena": 222.67,
        "powiazanyZ": "00146"
    },
    "00109": {
        "cena": 205.33,
        "powiazanyZ": "00147"
    },
    "00110": {
        "cena": 189.33,
        "powiazanyZ": "00148"
    },
    "00120": {
        "cena": 413.33,
        "powiazanyZ": "00150"
    },
    "00121": {
        "cena": 73.33,
        "powiazanyZ": "00000"
    },
    "00122": {
        "cena": 153.33,
        "powiazanyZ": "00000"
    },
    "00123": {
        "cena": 153.33,
        "powiazanyZ": "00000"
    },
    "00138": {
        "cena": 1937,
        "powiazanyZ": "00152"
    },
    "00139": {
        "cena": 366.82,
        "powiazanyZ": "00101"
    },
    "00140": {
        "cena": 325.33,
        "powiazanyZ": "00102"
    },
    "00141": {
        "cena": 306.67,
        "powiazanyZ": "00103"
    },
    "00142": {
        "cena": 289.33,
        "powiazanyZ": "001041"
    },
    "00143": {
        "cena": 276,
        "powiazanyZ": "00105"
    },
    "00144": {
        "cena": 258.67,
        "powiazanyZ": "00106"
    },
    "00145": {
        "cena": 240,
        "powiazanyZ": "00107"
    },
    "00146": {
        "cena": 222.67,
        "powiazanyZ": "00108"
    },
    "00147": {
        "cena": 205.33,
        "powiazanyZ": "00109"
    },
    "00148": {
        "cena": 189.33,
        "powiazanyZ": "00110"
    },
    "00150": {
        "cena": 413.33,
        "powiazanyZ": "00120"
    },
    "00152": {
        "cena": 1997.33,
        "powiazanyZ": "00138"
    }
}

and this from my php file:

$vat = 0.00;
$waluta = '';
$kod00101 = 0.00;
$kod00102 = 0.00;
...
$kod00152 = 0.00;

$cennik_plik = file_get_contents("cennik.json");
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($cennik_plik, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        $v = ${'kod'.$key};
        if('$key' == $v){
            $v = number_format((float)$val[cena],2,'.','');
        }
    } else {
        if('$key' == 'waluta') {$waluta = $val;};
        if('$key' == 'vat') {$vat = number_format((float)$val,2,'.','');};
    }
}

i am trying this and i get following error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php:50
Stack trace:
 #0 C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php(50): ArrayIterator->__construct('???{?? "walu...')
 #1 {main} thrown in C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php on line 50

Any Suggestions?

I have tried to solve it myself using these resources, but nothing:

Community
  • 1
  • 1
gerpaick
  • 801
  • 2
  • 13
  • 36
  • That's not a JSON array you're passing in, so I don't know why you'd think `RecursiveArrayIterator` would work. – CanSpice Dec 12 '11 at 23:38
  • i have tried this code on http://codepad.org/coNivEDv and it works, so i dont know where should i look for errors – gerpaick Dec 13 '11 at 00:25
  • 1
    I realize setting up xdebug is hard, but you need to employ some debugging before you ask a question here. So e.g., in this case you need to step through the values. And if that is `echo` or `var_dump`, so be it. But e.g. you didn't even bother to check what `json_decode()` gave you. – Till Dec 13 '11 at 01:00
  • you are right. to be honest, i dont know how and where set up this stuff and where should i start to look for errors. i was sure that i was some easy to fix error, and as i am php really newbie, i tried to ask quick question. now, with mario's help i am starting to understand something. – gerpaick Dec 13 '11 at 01:20

1 Answers1

4

I'm not quite sure what you are attempting with that data. But you certainly don't need that RecursiveArrayIterator. A boring foreach is enough to traverse that array:

$json = file_get_contents("cennik.json");
# the culprit was the UTF-8 BOM, which OPs specific verison of json_decode tripped over (newer versions would return NULL)
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json);
$array = json_decode($json, TRUE);

var_dump($array);    # <-- WHAT DOES THAT OUTPUT ??


foreach ($array as $key => $value) {

    if ($key == "waluta") {
        $waluta = $value;
    }
    elseif ($key == "vat") {
        $vat = $value;
    }

    // all the data entries
    elseif (is_numeric($key)) {
        $kod[$key] = $value["cena"];
        // ${"kod$key"} = ...;
    }
}

Note that you can use variable variables. But this example screams array. I'm not even sure why you would want to use a separate list of variables. The source array should be good enough to work with.

mario
  • 144,265
  • 20
  • 237
  • 291
  • i have tried this way, but i cannot get values assigned to variables. i tried to put echo "$key => $value\n"; into foreach loop, but no success. i get a warning: Warning: Invalid argument supplied for foreach() in C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php on line 50 – gerpaick Dec 13 '11 at 00:25
  • Add a `print_r($array);` before the loop to see if you actually got data. – mario Dec 13 '11 at 00:33
  • yes, i get data: { "waluta": "PLN", "vat": 1.23, "00101": { "cena": 340, "powiazanyZ": "00139" }, "00102": { "cena": 325.33, "powiazanyZ": "00140" }, "00103": { "cena": 306.67, "powiazanyZ": ......"cena": 413.33, "powiazanyZ": "00120" }, "00152": { "cena": 1997.33, "powiazanyZ": "00138" } }, so where to search a problem? – gerpaick Dec 13 '11 at 00:38
  • Add a print_r($array); before the loop **and after the json_decode**. Please copy the example exactly. `$array` cannot contain a JSON string after the decode. – mario Dec 13 '11 at 00:46
  • i get result as i listed before only AFTER json_decode. before json_decode i get null. – gerpaick Dec 13 '11 at 00:53
  • There's still something wrong with your code. I get `array(29) { ["waluta"]=> string(3) "PLN" ["vat"]=> float(1.2299999999999999822364316059975) ["00101"]=> array(2) { ["cena"]=>` a real array after the decoding. You just pasted the raw file contents. -- Please post your current code on codepad. – mario Dec 13 '11 at 00:56
  • var_dump($array); gives me: string(2190) and then json file content.. http://codepad.org/M4PNOQlE and it works, maybe something with reading from file...? – gerpaick Dec 13 '11 at 01:02
  • Well, then I can only guess your JSON file is double-encoded. That can't be good. But a simple (though awful) check might cover that... – mario Dec 13 '11 at 01:04
  • Valid does not mean it's not double encoded. Upload it somewhere and send a link. Also please try the updated example. – mario Dec 13 '11 at 01:09
  • i have uploaded my json file here: http://ge.tt/94ni27B . trying updated example i get time exceed of php server – gerpaick Dec 13 '11 at 01:15
  • Well, that's a valid and raw JSON file then. Your browser hangs in an eternal loop, meaning your `json_decode` is nun-functional. Update your PHP installation, or use another `json_decode` implementation (either the Services_JSON from ZendFramework, or the test version [up_json_decode()](http://ge.tt/9IVY37B/v/0) from upgradephp) – mario Dec 13 '11 at 01:20
  • i have changed file encoding of my json file from utf-8 to ascii and i think it is working now. do i have to use ascii encoding? – gerpaick Dec 13 '11 at 01:25
  • 1
    Ah, okay. Did not look closely enough. You have the stupid UTF-8 BOM in your json file. That's what your json_decode version tripped over (you should still update your PHP version). As workaround you can use `preg_replace('/^\xEF\xBB\xBF/', '', $str)` – mario Dec 13 '11 at 01:31
  • THANKS, now it is working. i get array(29) from var_dump($array); and i assign values to variables. thanks again – gerpaick Dec 13 '11 at 01:36