How to push duplicate entry in PHP?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Following is an object:



$obj = (object) ;
$obj->{'key1'} = 'val1';


Now, how do I push a key with same name but different value?



$obj = (object) ;
$obj->{'key1'} = 'val1';
$obj->{'key1'} = 'val2';


It just edits the key1. I know this is an expected behavior but do we have other workarounds?



I want the schema to be like:



"collection": {
"obj1": "val1",
"obj1": "val2"
}









share|improve this question




















  • 2





    Instead of you should add array object inside the same key

    – Girish
    Jan 3 at 5:53











  • I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59











  • it will be better to use array and then typecast it to object, rather then creating array and typecasting it to object.

    – Ronak Bokaria
    Jan 3 at 5:59











  • But it will be something like this: [{}, {}]

    – Sanjay
    Jan 3 at 6:01






  • 2





    well keys can not be same! i suggest that you can use objects/array in collection to do this. "collection": [ {"obj1": "val1"}, {"obj1": "val1"}, ]

    – Ronak Bokaria
    Jan 3 at 6:03


















0















Following is an object:



$obj = (object) ;
$obj->{'key1'} = 'val1';


Now, how do I push a key with same name but different value?



$obj = (object) ;
$obj->{'key1'} = 'val1';
$obj->{'key1'} = 'val2';


It just edits the key1. I know this is an expected behavior but do we have other workarounds?



I want the schema to be like:



"collection": {
"obj1": "val1",
"obj1": "val2"
}









share|improve this question




















  • 2





    Instead of you should add array object inside the same key

    – Girish
    Jan 3 at 5:53











  • I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59











  • it will be better to use array and then typecast it to object, rather then creating array and typecasting it to object.

    – Ronak Bokaria
    Jan 3 at 5:59











  • But it will be something like this: [{}, {}]

    – Sanjay
    Jan 3 at 6:01






  • 2





    well keys can not be same! i suggest that you can use objects/array in collection to do this. "collection": [ {"obj1": "val1"}, {"obj1": "val1"}, ]

    – Ronak Bokaria
    Jan 3 at 6:03














0












0








0








Following is an object:



$obj = (object) ;
$obj->{'key1'} = 'val1';


Now, how do I push a key with same name but different value?



$obj = (object) ;
$obj->{'key1'} = 'val1';
$obj->{'key1'} = 'val2';


It just edits the key1. I know this is an expected behavior but do we have other workarounds?



I want the schema to be like:



"collection": {
"obj1": "val1",
"obj1": "val2"
}









share|improve this question
















Following is an object:



$obj = (object) ;
$obj->{'key1'} = 'val1';


Now, how do I push a key with same name but different value?



$obj = (object) ;
$obj->{'key1'} = 'val1';
$obj->{'key1'} = 'val2';


It just edits the key1. I know this is an expected behavior but do we have other workarounds?



I want the schema to be like:



"collection": {
"obj1": "val1",
"obj1": "val2"
}






php arrays object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:58







Sanjay

















asked Jan 3 at 5:50









SanjaySanjay

759120




759120








  • 2





    Instead of you should add array object inside the same key

    – Girish
    Jan 3 at 5:53











  • I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59











  • it will be better to use array and then typecast it to object, rather then creating array and typecasting it to object.

    – Ronak Bokaria
    Jan 3 at 5:59











  • But it will be something like this: [{}, {}]

    – Sanjay
    Jan 3 at 6:01






  • 2





    well keys can not be same! i suggest that you can use objects/array in collection to do this. "collection": [ {"obj1": "val1"}, {"obj1": "val1"}, ]

    – Ronak Bokaria
    Jan 3 at 6:03














  • 2





    Instead of you should add array object inside the same key

    – Girish
    Jan 3 at 5:53











  • I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59











  • it will be better to use array and then typecast it to object, rather then creating array and typecasting it to object.

    – Ronak Bokaria
    Jan 3 at 5:59











  • But it will be something like this: [{}, {}]

    – Sanjay
    Jan 3 at 6:01






  • 2





    well keys can not be same! i suggest that you can use objects/array in collection to do this. "collection": [ {"obj1": "val1"}, {"obj1": "val1"}, ]

    – Ronak Bokaria
    Jan 3 at 6:03








2




2





Instead of you should add array object inside the same key

– Girish
Jan 3 at 5:53





Instead of you should add array object inside the same key

– Girish
Jan 3 at 5:53













I inserted the schema I want. Please look at the edited schema!

– Sanjay
Jan 3 at 5:59





I inserted the schema I want. Please look at the edited schema!

– Sanjay
Jan 3 at 5:59













it will be better to use array and then typecast it to object, rather then creating array and typecasting it to object.

– Ronak Bokaria
Jan 3 at 5:59





it will be better to use array and then typecast it to object, rather then creating array and typecasting it to object.

– Ronak Bokaria
Jan 3 at 5:59













But it will be something like this: [{}, {}]

– Sanjay
Jan 3 at 6:01





But it will be something like this: [{}, {}]

– Sanjay
Jan 3 at 6:01




2




2





well keys can not be same! i suggest that you can use objects/array in collection to do this. "collection": [ {"obj1": "val1"}, {"obj1": "val1"}, ]

– Ronak Bokaria
Jan 3 at 6:03





well keys can not be same! i suggest that you can use objects/array in collection to do this. "collection": [ {"obj1": "val1"}, {"obj1": "val1"}, ]

– Ronak Bokaria
Jan 3 at 6:03












2 Answers
2






active

oldest

votes


















0














You can map the values in an array with "key1" as key:



$obj = (object) ;
$obj->key1 = ['val1', 'val2'];


Output:



{#183 ▼
+"key1": array:2 [▼
0 => "val1"
1 => "val2"
]
}


Now, you can easily traverse for "key1" and get all the values.



Thanks!






share|improve this answer
























  • I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59






  • 1





    Hi Sanjay, You can't have more than one values for a single key.

    – Ayush Jain
    Jan 3 at 6:00











  • Even if you have a way, think about how you will be accessing the values for "key1"

    – Ayush Jain
    Jan 3 at 6:01











  • For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

    – Ayush Jain
    Jan 3 at 6:04





















0














The default PHP object behavior, like many other languages, is that key must be unique. If it exists it will be updated.



There is not other workaround, unless you want to just make that value of the key an array and add to it if you set the value for an existing key.



E.g.



$obj = (object) ;
$obj->{'key1'} = 'val1';


Object is:



object(stdClass)#1 (1) { ["key1"]=> string(4) "val2" }


$obj = (object) ;
$obj->{'key1'} = array();
$obj->{'key1'} = 'val1';
$obj->{'key1'} = 'val2';


And now object is:



object(stdClass)#1 (1) { ["key1"]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } }


Based on that you could create a custom object that does that when you assign a value to an existing key:



class MyObj {
private $data = array();
public function __set($key, $val) {
if (array_key_exists($key, $this->data)) {
if (!is_array($this->data[$key])) {
$this->data[$key] = array($this->data[$key], $val);
} else {
$this->data[$key] = $val;
}
} else {
$this->data[$key] = $val;
}
}
public function __get($key) {
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
}
}


So now you set them like this:



$obj = new MyObj();
$obj->key1 = 'val1';
$obj->key2 = 'val2';
$obj->key1 = 'val3';


And the result is:



echo '<pre>';
print_r($obj->key1);
echo '</pre>';

Array
(
[0] => val1
[1] => val3
)





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',
    autoActivateHeartbeat: false,
    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%2f54016973%2fhow-to-push-duplicate-entry-in-php%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can map the values in an array with "key1" as key:



    $obj = (object) ;
    $obj->key1 = ['val1', 'val2'];


    Output:



    {#183 ▼
    +"key1": array:2 [▼
    0 => "val1"
    1 => "val2"
    ]
    }


    Now, you can easily traverse for "key1" and get all the values.



    Thanks!






    share|improve this answer
























    • I inserted the schema I want. Please look at the edited schema!

      – Sanjay
      Jan 3 at 5:59






    • 1





      Hi Sanjay, You can't have more than one values for a single key.

      – Ayush Jain
      Jan 3 at 6:00











    • Even if you have a way, think about how you will be accessing the values for "key1"

      – Ayush Jain
      Jan 3 at 6:01











    • For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

      – Ayush Jain
      Jan 3 at 6:04


















    0














    You can map the values in an array with "key1" as key:



    $obj = (object) ;
    $obj->key1 = ['val1', 'val2'];


    Output:



    {#183 ▼
    +"key1": array:2 [▼
    0 => "val1"
    1 => "val2"
    ]
    }


    Now, you can easily traverse for "key1" and get all the values.



    Thanks!






    share|improve this answer
























    • I inserted the schema I want. Please look at the edited schema!

      – Sanjay
      Jan 3 at 5:59






    • 1





      Hi Sanjay, You can't have more than one values for a single key.

      – Ayush Jain
      Jan 3 at 6:00











    • Even if you have a way, think about how you will be accessing the values for "key1"

      – Ayush Jain
      Jan 3 at 6:01











    • For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

      – Ayush Jain
      Jan 3 at 6:04
















    0












    0








    0







    You can map the values in an array with "key1" as key:



    $obj = (object) ;
    $obj->key1 = ['val1', 'val2'];


    Output:



    {#183 ▼
    +"key1": array:2 [▼
    0 => "val1"
    1 => "val2"
    ]
    }


    Now, you can easily traverse for "key1" and get all the values.



    Thanks!






    share|improve this answer













    You can map the values in an array with "key1" as key:



    $obj = (object) ;
    $obj->key1 = ['val1', 'val2'];


    Output:



    {#183 ▼
    +"key1": array:2 [▼
    0 => "val1"
    1 => "val2"
    ]
    }


    Now, you can easily traverse for "key1" and get all the values.



    Thanks!







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 5:58









    Ayush JainAyush Jain

    1437




    1437













    • I inserted the schema I want. Please look at the edited schema!

      – Sanjay
      Jan 3 at 5:59






    • 1





      Hi Sanjay, You can't have more than one values for a single key.

      – Ayush Jain
      Jan 3 at 6:00











    • Even if you have a way, think about how you will be accessing the values for "key1"

      – Ayush Jain
      Jan 3 at 6:01











    • For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

      – Ayush Jain
      Jan 3 at 6:04





















    • I inserted the schema I want. Please look at the edited schema!

      – Sanjay
      Jan 3 at 5:59






    • 1





      Hi Sanjay, You can't have more than one values for a single key.

      – Ayush Jain
      Jan 3 at 6:00











    • Even if you have a way, think about how you will be accessing the values for "key1"

      – Ayush Jain
      Jan 3 at 6:01











    • For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

      – Ayush Jain
      Jan 3 at 6:04



















    I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59





    I inserted the schema I want. Please look at the edited schema!

    – Sanjay
    Jan 3 at 5:59




    1




    1





    Hi Sanjay, You can't have more than one values for a single key.

    – Ayush Jain
    Jan 3 at 6:00





    Hi Sanjay, You can't have more than one values for a single key.

    – Ayush Jain
    Jan 3 at 6:00













    Even if you have a way, think about how you will be accessing the values for "key1"

    – Ayush Jain
    Jan 3 at 6:01





    Even if you have a way, think about how you will be accessing the values for "key1"

    – Ayush Jain
    Jan 3 at 6:01













    For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

    – Ayush Jain
    Jan 3 at 6:04







    For further clarification, you can checkout: stackoverflow.com/questions/21786351/…

    – Ayush Jain
    Jan 3 at 6:04















    0














    The default PHP object behavior, like many other languages, is that key must be unique. If it exists it will be updated.



    There is not other workaround, unless you want to just make that value of the key an array and add to it if you set the value for an existing key.



    E.g.



    $obj = (object) ;
    $obj->{'key1'} = 'val1';


    Object is:



    object(stdClass)#1 (1) { ["key1"]=> string(4) "val2" }


    $obj = (object) ;
    $obj->{'key1'} = array();
    $obj->{'key1'} = 'val1';
    $obj->{'key1'} = 'val2';


    And now object is:



    object(stdClass)#1 (1) { ["key1"]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } }


    Based on that you could create a custom object that does that when you assign a value to an existing key:



    class MyObj {
    private $data = array();
    public function __set($key, $val) {
    if (array_key_exists($key, $this->data)) {
    if (!is_array($this->data[$key])) {
    $this->data[$key] = array($this->data[$key], $val);
    } else {
    $this->data[$key] = $val;
    }
    } else {
    $this->data[$key] = $val;
    }
    }
    public function __get($key) {
    if (array_key_exists($key, $this->data)) {
    return $this->data[$key];
    }
    }
    }


    So now you set them like this:



    $obj = new MyObj();
    $obj->key1 = 'val1';
    $obj->key2 = 'val2';
    $obj->key1 = 'val3';


    And the result is:



    echo '<pre>';
    print_r($obj->key1);
    echo '</pre>';

    Array
    (
    [0] => val1
    [1] => val3
    )





    share|improve this answer




























      0














      The default PHP object behavior, like many other languages, is that key must be unique. If it exists it will be updated.



      There is not other workaround, unless you want to just make that value of the key an array and add to it if you set the value for an existing key.



      E.g.



      $obj = (object) ;
      $obj->{'key1'} = 'val1';


      Object is:



      object(stdClass)#1 (1) { ["key1"]=> string(4) "val2" }


      $obj = (object) ;
      $obj->{'key1'} = array();
      $obj->{'key1'} = 'val1';
      $obj->{'key1'} = 'val2';


      And now object is:



      object(stdClass)#1 (1) { ["key1"]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } }


      Based on that you could create a custom object that does that when you assign a value to an existing key:



      class MyObj {
      private $data = array();
      public function __set($key, $val) {
      if (array_key_exists($key, $this->data)) {
      if (!is_array($this->data[$key])) {
      $this->data[$key] = array($this->data[$key], $val);
      } else {
      $this->data[$key] = $val;
      }
      } else {
      $this->data[$key] = $val;
      }
      }
      public function __get($key) {
      if (array_key_exists($key, $this->data)) {
      return $this->data[$key];
      }
      }
      }


      So now you set them like this:



      $obj = new MyObj();
      $obj->key1 = 'val1';
      $obj->key2 = 'val2';
      $obj->key1 = 'val3';


      And the result is:



      echo '<pre>';
      print_r($obj->key1);
      echo '</pre>';

      Array
      (
      [0] => val1
      [1] => val3
      )





      share|improve this answer


























        0












        0








        0







        The default PHP object behavior, like many other languages, is that key must be unique. If it exists it will be updated.



        There is not other workaround, unless you want to just make that value of the key an array and add to it if you set the value for an existing key.



        E.g.



        $obj = (object) ;
        $obj->{'key1'} = 'val1';


        Object is:



        object(stdClass)#1 (1) { ["key1"]=> string(4) "val2" }


        $obj = (object) ;
        $obj->{'key1'} = array();
        $obj->{'key1'} = 'val1';
        $obj->{'key1'} = 'val2';


        And now object is:



        object(stdClass)#1 (1) { ["key1"]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } }


        Based on that you could create a custom object that does that when you assign a value to an existing key:



        class MyObj {
        private $data = array();
        public function __set($key, $val) {
        if (array_key_exists($key, $this->data)) {
        if (!is_array($this->data[$key])) {
        $this->data[$key] = array($this->data[$key], $val);
        } else {
        $this->data[$key] = $val;
        }
        } else {
        $this->data[$key] = $val;
        }
        }
        public function __get($key) {
        if (array_key_exists($key, $this->data)) {
        return $this->data[$key];
        }
        }
        }


        So now you set them like this:



        $obj = new MyObj();
        $obj->key1 = 'val1';
        $obj->key2 = 'val2';
        $obj->key1 = 'val3';


        And the result is:



        echo '<pre>';
        print_r($obj->key1);
        echo '</pre>';

        Array
        (
        [0] => val1
        [1] => val3
        )





        share|improve this answer













        The default PHP object behavior, like many other languages, is that key must be unique. If it exists it will be updated.



        There is not other workaround, unless you want to just make that value of the key an array and add to it if you set the value for an existing key.



        E.g.



        $obj = (object) ;
        $obj->{'key1'} = 'val1';


        Object is:



        object(stdClass)#1 (1) { ["key1"]=> string(4) "val2" }


        $obj = (object) ;
        $obj->{'key1'} = array();
        $obj->{'key1'} = 'val1';
        $obj->{'key1'} = 'val2';


        And now object is:



        object(stdClass)#1 (1) { ["key1"]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } }


        Based on that you could create a custom object that does that when you assign a value to an existing key:



        class MyObj {
        private $data = array();
        public function __set($key, $val) {
        if (array_key_exists($key, $this->data)) {
        if (!is_array($this->data[$key])) {
        $this->data[$key] = array($this->data[$key], $val);
        } else {
        $this->data[$key] = $val;
        }
        } else {
        $this->data[$key] = $val;
        }
        }
        public function __get($key) {
        if (array_key_exists($key, $this->data)) {
        return $this->data[$key];
        }
        }
        }


        So now you set them like this:



        $obj = new MyObj();
        $obj->key1 = 'val1';
        $obj->key2 = 'val2';
        $obj->key1 = 'val3';


        And the result is:



        echo '<pre>';
        print_r($obj->key1);
        echo '</pre>';

        Array
        (
        [0] => val1
        [1] => val3
        )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 6:49









        Sam BattatSam Battat

        5,30511528




        5,30511528






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016973%2fhow-to-push-duplicate-entry-in-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]