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:

4
  • That's not a JSON array you're passing in, so I don't know why you'd think RecursiveArrayIterator would work. Commented Dec 12, 2011 at 23:38
  • i have tried this code on codepad.org/coNivEDv and it works, so i dont know where should i look for errors Commented Dec 13, 2011 at 0: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. Commented Dec 13, 2011 at 1: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. Commented Dec 13, 2011 at 1:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

14 Comments

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
Add a print_r($array); before the loop to see if you actually got data.
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?
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.
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)
|

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.