Read a specific value in Json with PHP

2

I already researched all the sites, but I could not find a way to get the totalPage value.

I'm reading Json with PHP and I need to get this value to do the pagination.

The data below follows.

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)
            }
        }
    
asked by anonymous 29.10.2017 / 22:11

2 answers

3

Return the values as json and use json_decode() , which will return an object or array 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'];
    
29.10.2017 / 22:53
0

Note that you have return from non-arrayed objects ( true of json_decode converts you into an associative array, particularly preferring to work with objects). Keep an eye on the hierarchy of elements, otherwise it will be impossible to manipulate them.

Then to manipulate the values (like objects) you should 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"];
    
06.12.2017 / 12:47