1

Hello i am new with Json in php. I have a web service that gives me data in json format. I take this data making decode put when i try to use this data i cant

Here is my code:

 $url = "http://www.webinsurer.gr/....;

    $json = json_decode(@file_get_contents($url), true);

and if i make debug i see the data i take :

[file] => C:\xampp\htdocs\development.insurancemarket.gr\mvc\protected\models\Ratingsmail.php
[line] => 18
[data] => Array
    (
        [0] => Array
            (
                [POL_EXPIREDATE] => 2014-05-19 12:00:00
                [INCO_IWCODE] => 41
                [INCO_DESC] => MAPFRE ASISTENCIA
                [PACK_IWCODE] => 0
                [PACK_DESC] => 
                [OFFERCODE] => 
                [PAYMENTCODE] => 
            )

        [1] => Array
            (.....

But i dont now how to use that data. when i try this :

$b= $json->{1}->{'INCO_IWCODE'};

    Debug::debuger($b);

the result is nothing

what is wrong? sorry for long post.

7
  • show us console.log($json), it will output to the firebug/developer tools console (f12 in any browser) Commented May 15, 2014 at 8:28
  • "if I make debug" with what code? Is this print_r($json)? If so you need to use $json['data'][0]['INCO_IWCODE']. Commented May 15, 2014 at 8:28
  • See my answer here Commented May 15, 2014 at 8:29
  • @BartłomiejWach What? You cannot debug PHP variables on a javascript console. Commented May 15, 2014 at 8:29
  • remove ", true" from your json decode line. Commented May 15, 2014 at 8:32

3 Answers 3

1

When setting the second argument on json_decode to true, you are actively asking for the data to be returned in an associative array and not objects. Thats why your code didn't work.

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

3 Comments

i removed the true and again in the debug the data was empty
@theodosis $json->data[1]->INCO_IWCODE - you are using {...} which is wrong and has a different meaning in PHP
Also what Dan said :-) To elaborate: {...} is for dynamic referencing, eg. if the variable you want depends on a computed result, or when the variable has a naming which is incompatible with PHP syntax: $obj->{$myvar} or $obj->{'invalid-variable-name'} are examples of valid uses. It does not work with arrays of any kind, only objects.
0

Demo

You are converting json to associative array. You need to use;

$b = $json["data"][1]["INCO_IWCODE"];

3 Comments

its not working the result is : Array ( [file] => C:\xampp\htdocs\development.insurancemarket.gr\mvc\protected\models\Ratingsmail.php [line] => 30 [data] => )
@user3438570 it is not working with what? Your formatting a bit hard to read. Please see my demo in answer
I took your code but again in the debug the data was empty. i dont now why.
0

$a = $json[0]->INCO_IWCODE;

That worked for my guys. thanks you all!!!

Comments

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.