Read a specific value in Json with PHP

Asked

Viewed 621 times

2

I’ve already searched all the sites, but I couldn’t find a way to get the value of totalPage.

I’m reading Json with the PHP and I need to take this amount to make the pagination.

Below is the data.

object(stdClass) # 1(3) {
    ["requestInfo"] =>
    object(stdClass) # 2(3) {

        ["status"] =>
        string(2)"OK"
        ["message"] =>
        string(7)"SUCCESS"
        ["generatedDate"] =>
        NULL

    }
    ["pagination"] =>
    object(stdClass) # 3(4) {
        ["page"] =>
        int(1)
        ["size"] =>
        int(10)
        ["totalSize"] =>
        int(1400)
        ["totalPage"] =>
        int(140)
    }
    ["offers"] =>
    array(10) {
        [0] =>
        object(stdClass) # 4(10) {
            ["id"] =>
            string(8)"11248674"
            ["name"] =>
            string(156)"Smartphone Motorola Moto G5 Plus TV XT1683 Platinum com 32GB, Tela 5.2 ´ ´, Dual Chip, Android 7.0, 4G, Câmera 12MP, Processador Octa - Core e 2GB de RAM"
            ["product"] =>
            object(stdClass) # 5(11) {
                ["id"] =>
                int(630970)
                ["name"] =>
                string(40)"Smartphone Motorola Moto G 5 Plus XT1683"
                ["shortName"] =>
                string(22)"Motorola 5 Plus XT1683"
                ["priceMin"] =>
                float(891.65)
                ["priceMax"] =>
                float(1819)
                ["discount"] =>
                int(50)
                ["thumbnail"] =>
                object(stdClass) # 6(4) {
                    ["url"] =>
                    string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_600x600-PU9a0ba_1.jpg"
                    ["height"] =>
                    int(600)
                    ["width"] =>
                    int(600)
                    ["otherFormats"] =>
                    array(4) {
                        [0] =>
                        object(stdClass) # 7(3) {
                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_100x100-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(100)
                            ["width"] =>
                            int(100)
                        }
                        [1] =>
                        object(stdClass) # 8(3) {
                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_200x200-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(200)
                            ["width"] =>
                            int(200)
                        }
                        [2] =>
                        object(stdClass) # 9(3) {
                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_300x300-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(300)
                            ["width"] =>
                            int(300)
                        }
                        [3] =>
                        object(stdClass) # 10(3) {

                            ["url"] =>
                            string(113)"https://thumbs.buscape.com.br/celular-e-smartphone/smartphone-motorola-moto-g-5-plus-xt1683_600x600-PU9a0ba_1.jpg"
                            ["height"] =>
                            int(600)
                            ["width"] =>
                            int(600)
                        }
                    }
                }
                ["userRating"] =>
                object(stdClass) # 11(2) {
                    ["comments"] =>
                    int(226)
                    ["rating"] =>
                    int(9)
                }
                ["category"] =>
                object(stdClass) # 12(5) {
                    ["id"] =>
                    int(77)
                    ["name"] =>
                    string(20)"Celular e Smartphone"
                    ["thumbnail"] =>
                    object(stdClass) # 13(1) {
                        ["url"] =>
                        string(50)"http://imagem.buscape.com.br/bp5/categorias/77.jpg"
                    }
                    ["hasProduct"] =>
                    int(444)
                    ["hasOffer"] =>
                    int(8297)
                }
                ["hasOffer"] =>
                int(70)
                ["link"] =>
                string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
            }
            ["category"] =>
            object(stdClass) # 14(6) {
                ["id"] =>
                int(77)
                ["name"] =>
                string(20)"Celular e Smartphone"
                ["thumbnail"] =>
                object(stdClass) # 15(1) {

                    ["url"] =>
                    string(50)"http://imagem.buscape.com.br/bp5/categorias/77.jpg"
                }
                ["hasProduct"] =>
                int(444)
                ["hasOffer"] =>
                int(8297)
                ["link"] =>
                string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
            }
            ["link"] =>
            string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
            ["thumbnail"] =>
            string(61)"https://thumbs.buscape.com.br/T100x100/__2.903294-dbe7fcb.jpg"
            ["price"] =>
            float(891.65)
            ["discount"] =>
            int(50)
            ["installment"] =>
            object(stdClass) # 16(2) {
                ["quantity"] =>
                int(10)
                ["value"] =>
                float(104.9)
            }
            ["store"] =>
            object(stdClass) # 17(6) {
                ["id"] =>
                int(5756)
                ["name"] =>
                string(11)"Casas Bahia"
                ["thumbnail"] =>
                string(46)"https://www.lomadee.com/vitrine/logo903294.gif"
                ["ebit"] =>
                object(stdClass) # 18(3) {

                    ["id"] =>
                    int(30)
                    ["name"] =>
                    string(12)"e-bit Ótima"
                    ["rating"] =>
                    int(4)
                }
                ["link"] =>
                string(96)"https://developer.lomadee.com/redir/validation/?sourceId=xxxxxxxxxxxxxxxxxx&appToken=xxxxxxxxxxxxxxxxxx"
                ["ocb"] =>
                bool(false)
            }
        }
  • Insert the json complete the question

  • Good morning, I added the full code, take a look

2 answers

3

Return values to json and use json_decode(), which will return an object or matrix if the second value is true. Example:

$json = '{"paisId":"84","produtoId":"1","status":"0","opId":"134"}';
$json = json_decode($json, true);
echo $json['paisId'];
echo $json['produtoId'];
echo $json['status'];
echo $json['opId'];
  • I did it the way you did, but no results came back, so I made $json = json_decode($json_file, true); echo $json['totalPage'];

  • Do a var_dump($json); and tell me what you returned.

0

Note that You have returns from non-arrays objects (the true of json_decode converts it into an associative array, particularly I prefer to work with objects). Stay tuned in the hierarchy of the elements, otherwise it will be impossible to manipulate them.

Then to manipulate the values (as objects) you must use:

$objeto = json_decode($json);
echo $objeto->pagination->totalPage;

In the case of the associative array:

$associativo = json_decode($json, true);
echo $associativo["pagination"]["totalPage"];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.