Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a string which is in valid Json format and it looks like this:

{
    "message": "success",
    "result": {
        "46620": {
            "course_id": "29",
            "en_title": "Google Analytics (Basic)",
            "fa_title": "مبانی گوگل آنالیتیکز",
            "badge_link": "http://www.darsnameh.com/badge/index.php?user=46620&badge="
        },
        "49449": {
            "course_id": "16",
            "en_title": "Multimedia Reporting 1- Reporting in the Internet Age",
            "fa_title": "گزارش‌گری چندرسانه‌ای ۱- گزارشگری در زمانه‌ی اينترنت",
            "badge_link": "http://www.darsnameh.com/badge/index.php?user=49449&badge="
        },
        "55480": {
            "course_id": "33",
            "en_title": "HTML for Journalists and Bloggers",
            "fa_title": "آشنایی با اچ‌تی‌ام‌ال Ùˆ Ùناوری‌های اینترنت برای روزنامه‌نگاران Ùˆ وبلاگ‌نویس‌ها",
            "badge_link": "http://www.darsnameh.com/badge/index.php?user=55480&badge="
        },
        "59250": {
            "course_id": "31",
            "en_title": "Twitter",
            "fa_title": "توییتر",
            "badge_link": "http://www.darsnameh.com/badge/index.php?user=59250&badge="
        },
        "103716": {
            "course_id": "42",
            "en_title": "How to write a CV?",
            "fa_title": "چگونه رزومه بنویسیم؟",
            "badge_link": "http://www.darsnameh.com/badge/index.php?user=103716&badge="
        }
    }
}

I want to read different fields, for example all fa_titles, I use $obj = json_decode($str, true); to convert this to Json, then echo sizeof($obj['result']); shows me that it has 5 result object, but how can I access to fa_title or en_title fields now?

share|improve this question
1  
Use a foreach loop. –  Palladium Jul 31 '12 at 16:02

2 Answers 2

up vote 2 down vote accepted

In JSON, arrays are designated by brackets ([ and ]) while dictionaries only have braces ({ and }).

You could access each result object individually by doing $obj['result']['46620']['en_title']. The result object is an associative array (also known as a dictionary) of associative arrays.

You can also iterate through each object using a foreach loop, as demonstrated in this question

share|improve this answer

To loop through all the results, something like this should do it:

$obj = json_decode($str);
foreach ($obj->result as $result)
{
    $fa_title = $result->fa_title;
    $en_title = $result->en_title;
}
share|improve this answer
    
With little change it works: I used $obj['result'] –  Mazdak Jul 31 '12 at 16:21
1  
Yes, you can access as an array like that if you use the second parameter of json_decode as true –  peacemaker Jul 31 '12 at 16:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.