How to transform array to this json pattern?












-2















I have array something like this



Array
(
[0] => Array
(
[product_id] => 13
[unit] => 1
[price] => 20.0000
[total] => 20.0000
)

[1] => Array
(
[product_id] => 15
[unit] => 2
[price] => 30.0000
[total] => 10.0000
)

)


I was stuck to transform above array to the "ProductList" in the following pattern



array(
"UserID" => 2,
"ProductList" => [
{
"ProductID"=> 13,
"Price"=> 20.0000
},
{
"ProductID"=> 15,
"Price"=> 30.0000
}
]
)


I tried like this:



$products = ;
foreach($items as $item) {
if(!empty($item['product_id'])) {
$product = '{"ProductID"=>' . $item['product_id'] . '}';
$products = $product;
}
}


But not the expected result.










share|improve this question

























  • use the php provided json_encode function

    – Hasta Dhana
    Dec 31 '18 at 3:26











  • What you want to transform to isn't really JSON with that outer array(). What is it that you want, exactly?

    – Brad
    Dec 31 '18 at 3:30











  • @Brad I have array from the first block of code. Just wondered how to pass the values into "ProductList" of second block of code (Need exactly same pattern like this) Failed with json_encode, maybe I did something wrong.

    – Kuang
    Dec 31 '18 at 3:40











  • @Kuang First, get the structure you want in a regular object. Then, use json_encode() on that object. It's a two-step process since you want to transform that object first.

    – Brad
    Dec 31 '18 at 3:41
















-2















I have array something like this



Array
(
[0] => Array
(
[product_id] => 13
[unit] => 1
[price] => 20.0000
[total] => 20.0000
)

[1] => Array
(
[product_id] => 15
[unit] => 2
[price] => 30.0000
[total] => 10.0000
)

)


I was stuck to transform above array to the "ProductList" in the following pattern



array(
"UserID" => 2,
"ProductList" => [
{
"ProductID"=> 13,
"Price"=> 20.0000
},
{
"ProductID"=> 15,
"Price"=> 30.0000
}
]
)


I tried like this:



$products = ;
foreach($items as $item) {
if(!empty($item['product_id'])) {
$product = '{"ProductID"=>' . $item['product_id'] . '}';
$products = $product;
}
}


But not the expected result.










share|improve this question

























  • use the php provided json_encode function

    – Hasta Dhana
    Dec 31 '18 at 3:26











  • What you want to transform to isn't really JSON with that outer array(). What is it that you want, exactly?

    – Brad
    Dec 31 '18 at 3:30











  • @Brad I have array from the first block of code. Just wondered how to pass the values into "ProductList" of second block of code (Need exactly same pattern like this) Failed with json_encode, maybe I did something wrong.

    – Kuang
    Dec 31 '18 at 3:40











  • @Kuang First, get the structure you want in a regular object. Then, use json_encode() on that object. It's a two-step process since you want to transform that object first.

    – Brad
    Dec 31 '18 at 3:41














-2












-2








-2








I have array something like this



Array
(
[0] => Array
(
[product_id] => 13
[unit] => 1
[price] => 20.0000
[total] => 20.0000
)

[1] => Array
(
[product_id] => 15
[unit] => 2
[price] => 30.0000
[total] => 10.0000
)

)


I was stuck to transform above array to the "ProductList" in the following pattern



array(
"UserID" => 2,
"ProductList" => [
{
"ProductID"=> 13,
"Price"=> 20.0000
},
{
"ProductID"=> 15,
"Price"=> 30.0000
}
]
)


I tried like this:



$products = ;
foreach($items as $item) {
if(!empty($item['product_id'])) {
$product = '{"ProductID"=>' . $item['product_id'] . '}';
$products = $product;
}
}


But not the expected result.










share|improve this question
















I have array something like this



Array
(
[0] => Array
(
[product_id] => 13
[unit] => 1
[price] => 20.0000
[total] => 20.0000
)

[1] => Array
(
[product_id] => 15
[unit] => 2
[price] => 30.0000
[total] => 10.0000
)

)


I was stuck to transform above array to the "ProductList" in the following pattern



array(
"UserID" => 2,
"ProductList" => [
{
"ProductID"=> 13,
"Price"=> 20.0000
},
{
"ProductID"=> 15,
"Price"=> 30.0000
}
]
)


I tried like this:



$products = ;
foreach($items as $item) {
if(!empty($item['product_id'])) {
$product = '{"ProductID"=>' . $item['product_id'] . '}';
$products = $product;
}
}


But not the expected result.







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 7:48









marc_s

575k12811101257




575k12811101257










asked Dec 31 '18 at 3:25









KuangKuang

203




203













  • use the php provided json_encode function

    – Hasta Dhana
    Dec 31 '18 at 3:26











  • What you want to transform to isn't really JSON with that outer array(). What is it that you want, exactly?

    – Brad
    Dec 31 '18 at 3:30











  • @Brad I have array from the first block of code. Just wondered how to pass the values into "ProductList" of second block of code (Need exactly same pattern like this) Failed with json_encode, maybe I did something wrong.

    – Kuang
    Dec 31 '18 at 3:40











  • @Kuang First, get the structure you want in a regular object. Then, use json_encode() on that object. It's a two-step process since you want to transform that object first.

    – Brad
    Dec 31 '18 at 3:41



















  • use the php provided json_encode function

    – Hasta Dhana
    Dec 31 '18 at 3:26











  • What you want to transform to isn't really JSON with that outer array(). What is it that you want, exactly?

    – Brad
    Dec 31 '18 at 3:30











  • @Brad I have array from the first block of code. Just wondered how to pass the values into "ProductList" of second block of code (Need exactly same pattern like this) Failed with json_encode, maybe I did something wrong.

    – Kuang
    Dec 31 '18 at 3:40











  • @Kuang First, get the structure you want in a regular object. Then, use json_encode() on that object. It's a two-step process since you want to transform that object first.

    – Brad
    Dec 31 '18 at 3:41

















use the php provided json_encode function

– Hasta Dhana
Dec 31 '18 at 3:26





use the php provided json_encode function

– Hasta Dhana
Dec 31 '18 at 3:26













What you want to transform to isn't really JSON with that outer array(). What is it that you want, exactly?

– Brad
Dec 31 '18 at 3:30





What you want to transform to isn't really JSON with that outer array(). What is it that you want, exactly?

– Brad
Dec 31 '18 at 3:30













@Brad I have array from the first block of code. Just wondered how to pass the values into "ProductList" of second block of code (Need exactly same pattern like this) Failed with json_encode, maybe I did something wrong.

– Kuang
Dec 31 '18 at 3:40





@Brad I have array from the first block of code. Just wondered how to pass the values into "ProductList" of second block of code (Need exactly same pattern like this) Failed with json_encode, maybe I did something wrong.

– Kuang
Dec 31 '18 at 3:40













@Kuang First, get the structure you want in a regular object. Then, use json_encode() on that object. It's a two-step process since you want to transform that object first.

– Brad
Dec 31 '18 at 3:41





@Kuang First, get the structure you want in a regular object. Then, use json_encode() on that object. It's a two-step process since you want to transform that object first.

– Brad
Dec 31 '18 at 3:41












2 Answers
2






active

oldest

votes


















0














You're looking for json_encode(): http://php.net/manual/en/function.json-encode.php



Assign your array to a variable, for example $products, and then you can simply write json_encode($products) to achieve your desired result.






share|improve this answer


























  • Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

    – Brad
    Dec 31 '18 at 3:31



















0














Try this,



$prodlist = array();
$x = 0;
foreach($arrs as $arr) {
//if ($arr['product_id'] == "" && $arr['price'] == "") continue;
$prodlist[$x]['Price'] = $arr['price'];
$prodlist[$x]['ProductID'] = $arr['product_id'];
$x += 1;
}
$arr2return['user_id'] = $user_id;
$arr2return['ProductList'] = $prodlist;
json_encode($arr2return);





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%2f53983296%2fhow-to-transform-array-to-this-json-pattern%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're looking for json_encode(): http://php.net/manual/en/function.json-encode.php



    Assign your array to a variable, for example $products, and then you can simply write json_encode($products) to achieve your desired result.






    share|improve this answer


























    • Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

      – Brad
      Dec 31 '18 at 3:31
















    0














    You're looking for json_encode(): http://php.net/manual/en/function.json-encode.php



    Assign your array to a variable, for example $products, and then you can simply write json_encode($products) to achieve your desired result.






    share|improve this answer


























    • Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

      – Brad
      Dec 31 '18 at 3:31














    0












    0








    0







    You're looking for json_encode(): http://php.net/manual/en/function.json-encode.php



    Assign your array to a variable, for example $products, and then you can simply write json_encode($products) to achieve your desired result.






    share|improve this answer















    You're looking for json_encode(): http://php.net/manual/en/function.json-encode.php



    Assign your array to a variable, for example $products, and then you can simply write json_encode($products) to achieve your desired result.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 31 '18 at 3:34

























    answered Dec 31 '18 at 3:28









    James GrimshawJames Grimshaw

    10618




    10618













    • Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

      – Brad
      Dec 31 '18 at 3:31



















    • Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

      – Brad
      Dec 31 '18 at 3:31

















    Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

    – Brad
    Dec 31 '18 at 3:31





    Link-only answers aren't usually all that useful. While you're right, assume the person asking the question actually did look in the manual and probably doesn't know what they're looking for. You don't have to write a book on the topic or anything, but some in-context code would be helpful.

    – Brad
    Dec 31 '18 at 3:31













    0














    Try this,



    $prodlist = array();
    $x = 0;
    foreach($arrs as $arr) {
    //if ($arr['product_id'] == "" && $arr['price'] == "") continue;
    $prodlist[$x]['Price'] = $arr['price'];
    $prodlist[$x]['ProductID'] = $arr['product_id'];
    $x += 1;
    }
    $arr2return['user_id'] = $user_id;
    $arr2return['ProductList'] = $prodlist;
    json_encode($arr2return);





    share|improve this answer




























      0














      Try this,



      $prodlist = array();
      $x = 0;
      foreach($arrs as $arr) {
      //if ($arr['product_id'] == "" && $arr['price'] == "") continue;
      $prodlist[$x]['Price'] = $arr['price'];
      $prodlist[$x]['ProductID'] = $arr['product_id'];
      $x += 1;
      }
      $arr2return['user_id'] = $user_id;
      $arr2return['ProductList'] = $prodlist;
      json_encode($arr2return);





      share|improve this answer


























        0












        0








        0







        Try this,



        $prodlist = array();
        $x = 0;
        foreach($arrs as $arr) {
        //if ($arr['product_id'] == "" && $arr['price'] == "") continue;
        $prodlist[$x]['Price'] = $arr['price'];
        $prodlist[$x]['ProductID'] = $arr['product_id'];
        $x += 1;
        }
        $arr2return['user_id'] = $user_id;
        $arr2return['ProductList'] = $prodlist;
        json_encode($arr2return);





        share|improve this answer













        Try this,



        $prodlist = array();
        $x = 0;
        foreach($arrs as $arr) {
        //if ($arr['product_id'] == "" && $arr['price'] == "") continue;
        $prodlist[$x]['Price'] = $arr['price'];
        $prodlist[$x]['ProductID'] = $arr['product_id'];
        $x += 1;
        }
        $arr2return['user_id'] = $user_id;
        $arr2return['ProductList'] = $prodlist;
        json_encode($arr2return);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 4:10









        Apetu GideonApetu Gideon

        564




        564






























            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%2f53983296%2fhow-to-transform-array-to-this-json-pattern%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'