Read Json/Array string in Php

By | July 12, 2018
Questions:

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?

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *