get value within object in an array











up vote
0
down vote

favorite












I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.



My array $params (from print_r) looks like this:



Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...


Cut off here, there are two more objects in this array.



Now if I do: echo $params[0]->name I get an empty result.



Also tried print_r($params[0], true);, empty result.



Also tried, empty result:



$status = $params[0];
echo $status->name;


What am I doing wrong here?



Thanks in advance










share|improve this question




















  • 3




    There is no 0 index in $params. The only index I can see is newOrderStatus.
    – Sergiu Paraschiv
    Nov 8 at 10:29






  • 1




    Try $params['newOrderStatus']->name
    – The fourth bird
    Nov 8 at 10:29










  • Also duplicate of: stackoverflow.com/questions/1921421/…
    – k0pernikus
    Nov 8 at 10:32















up vote
0
down vote

favorite












I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.



My array $params (from print_r) looks like this:



Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...


Cut off here, there are two more objects in this array.



Now if I do: echo $params[0]->name I get an empty result.



Also tried print_r($params[0], true);, empty result.



Also tried, empty result:



$status = $params[0];
echo $status->name;


What am I doing wrong here?



Thanks in advance










share|improve this question




















  • 3




    There is no 0 index in $params. The only index I can see is newOrderStatus.
    – Sergiu Paraschiv
    Nov 8 at 10:29






  • 1




    Try $params['newOrderStatus']->name
    – The fourth bird
    Nov 8 at 10:29










  • Also duplicate of: stackoverflow.com/questions/1921421/…
    – k0pernikus
    Nov 8 at 10:32













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.



My array $params (from print_r) looks like this:



Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...


Cut off here, there are two more objects in this array.



Now if I do: echo $params[0]->name I get an empty result.



Also tried print_r($params[0], true);, empty result.



Also tried, empty result:



$status = $params[0];
echo $status->name;


What am I doing wrong here?



Thanks in advance










share|improve this question















I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.



My array $params (from print_r) looks like this:



Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...


Cut off here, there are two more objects in this array.



Now if I do: echo $params[0]->name I get an empty result.



Also tried print_r($params[0], true);, empty result.



Also tried, empty result:



$status = $params[0];
echo $status->name;


What am I doing wrong here?



Thanks in advance







php arrays object prestashop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 10:30









Mickael Leger

2,3881726




2,3881726










asked Nov 8 at 10:28









IJsbrand Schipperus

33




33








  • 3




    There is no 0 index in $params. The only index I can see is newOrderStatus.
    – Sergiu Paraschiv
    Nov 8 at 10:29






  • 1




    Try $params['newOrderStatus']->name
    – The fourth bird
    Nov 8 at 10:29










  • Also duplicate of: stackoverflow.com/questions/1921421/…
    – k0pernikus
    Nov 8 at 10:32














  • 3




    There is no 0 index in $params. The only index I can see is newOrderStatus.
    – Sergiu Paraschiv
    Nov 8 at 10:29






  • 1




    Try $params['newOrderStatus']->name
    – The fourth bird
    Nov 8 at 10:29










  • Also duplicate of: stackoverflow.com/questions/1921421/…
    – k0pernikus
    Nov 8 at 10:32








3




3




There is no 0 index in $params. The only index I can see is newOrderStatus.
– Sergiu Paraschiv
Nov 8 at 10:29




There is no 0 index in $params. The only index I can see is newOrderStatus.
– Sergiu Paraschiv
Nov 8 at 10:29




1




1




Try $params['newOrderStatus']->name
– The fourth bird
Nov 8 at 10:29




Try $params['newOrderStatus']->name
– The fourth bird
Nov 8 at 10:29












Also duplicate of: stackoverflow.com/questions/1921421/…
– k0pernikus
Nov 8 at 10:32




Also duplicate of: stackoverflow.com/questions/1921421/…
– k0pernikus
Nov 8 at 10:32












4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










You need to access object as following



$params['newOrderStatus'];


In above object you will have all child objects so you can access them by following



$params['newOrderStatus']->name;
$params['newOrderStatus']->template;





share|improve this answer





















  • Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
    – IJsbrand Schipperus
    Nov 8 at 10:34


















up vote
2
down vote













Well, as you said your array looks like this :



Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...


So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name






share|improve this answer




























    up vote
    1
    down vote













    Your array $params has a key called newOrderStatus which has the object as a value you are looking for.



    Looking at your example, there is value for index 0.



    To get the value of the name property, you could use:



    $params['newOrderStatus']->name






    share|improve this answer






























      up vote
      0
      down vote













      You can type cast it to an array like this:



      $array =  (array) $yourObject;





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205835%2fget-value-within-object-in-an-array%23new-answer', 'question_page');
        }
        );

        Post as a guest
































        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        You need to access object as following



        $params['newOrderStatus'];


        In above object you will have all child objects so you can access them by following



        $params['newOrderStatus']->name;
        $params['newOrderStatus']->template;





        share|improve this answer





















        • Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
          – IJsbrand Schipperus
          Nov 8 at 10:34















        up vote
        1
        down vote



        accepted










        You need to access object as following



        $params['newOrderStatus'];


        In above object you will have all child objects so you can access them by following



        $params['newOrderStatus']->name;
        $params['newOrderStatus']->template;





        share|improve this answer





















        • Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
          – IJsbrand Schipperus
          Nov 8 at 10:34













        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You need to access object as following



        $params['newOrderStatus'];


        In above object you will have all child objects so you can access them by following



        $params['newOrderStatus']->name;
        $params['newOrderStatus']->template;





        share|improve this answer












        You need to access object as following



        $params['newOrderStatus'];


        In above object you will have all child objects so you can access them by following



        $params['newOrderStatus']->name;
        $params['newOrderStatus']->template;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 10:32









        Ayaz Shah

        1,97511645




        1,97511645












        • Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
          – IJsbrand Schipperus
          Nov 8 at 10:34


















        • Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
          – IJsbrand Schipperus
          Nov 8 at 10:34
















        Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
        – IJsbrand Schipperus
        Nov 8 at 10:34




        Thanks! Somehow I thought I had tried this also, but I was probably lost in trying to much.
        – IJsbrand Schipperus
        Nov 8 at 10:34












        up vote
        2
        down vote













        Well, as you said your array looks like this :



        Array
        (
        [newOrderStatus] => OrderState Object
        (
        [name] => Canceled
        [template] => order_canceled
        [send_email] => 1
        ...


        So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name






        share|improve this answer

























          up vote
          2
          down vote













          Well, as you said your array looks like this :



          Array
          (
          [newOrderStatus] => OrderState Object
          (
          [name] => Canceled
          [template] => order_canceled
          [send_email] => 1
          ...


          So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name






          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote









            Well, as you said your array looks like this :



            Array
            (
            [newOrderStatus] => OrderState Object
            (
            [name] => Canceled
            [template] => order_canceled
            [send_email] => 1
            ...


            So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name






            share|improve this answer












            Well, as you said your array looks like this :



            Array
            (
            [newOrderStatus] => OrderState Object
            (
            [name] => Canceled
            [template] => order_canceled
            [send_email] => 1
            ...


            So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 10:32









            Mickael Leger

            2,3881726




            2,3881726






















                up vote
                1
                down vote













                Your array $params has a key called newOrderStatus which has the object as a value you are looking for.



                Looking at your example, there is value for index 0.



                To get the value of the name property, you could use:



                $params['newOrderStatus']->name






                share|improve this answer



























                  up vote
                  1
                  down vote













                  Your array $params has a key called newOrderStatus which has the object as a value you are looking for.



                  Looking at your example, there is value for index 0.



                  To get the value of the name property, you could use:



                  $params['newOrderStatus']->name






                  share|improve this answer

























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Your array $params has a key called newOrderStatus which has the object as a value you are looking for.



                    Looking at your example, there is value for index 0.



                    To get the value of the name property, you could use:



                    $params['newOrderStatus']->name






                    share|improve this answer














                    Your array $params has a key called newOrderStatus which has the object as a value you are looking for.



                    Looking at your example, there is value for index 0.



                    To get the value of the name property, you could use:



                    $params['newOrderStatus']->name







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 8 at 10:49

























                    answered Nov 8 at 10:31









                    The fourth bird

                    18k71323




                    18k71323






















                        up vote
                        0
                        down vote













                        You can type cast it to an array like this:



                        $array =  (array) $yourObject;





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          You can type cast it to an array like this:



                          $array =  (array) $yourObject;





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can type cast it to an array like this:



                            $array =  (array) $yourObject;





                            share|improve this answer












                            You can type cast it to an array like this:



                            $array =  (array) $yourObject;






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 8 at 10:31









                            Aravind Bhat K

                            13112




                            13112






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205835%2fget-value-within-object-in-an-array%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest




















































































                                Popular posts from this blog

                                Schultheiß

                                Verwaltungsgliederung Dänemarks

                                Liste der Kulturdenkmale in Wilsdruff