-2

I need to form the final array, its not displaying how i am expecting, here is the code i used.

I need to print the final array in the expected format, I have attached the output how i am looking for in the below section,

<?php

 $order_id = 12345;
$array1 = array(
   "firstName" => "Test Fname",
   "lastName" => "Test Lname",
   "address1" => "Test Address",
   "city" => "Test City",
   "country" => "IND",
   "email" => "[email protected]"
 );

$array2 = array(
   "firstName" => "Test Fname",
   "lastName" => "Test Lname",
   "address1" => "Test Address",
   "city" => "Test City",
   "country" => "IND",
   "email" => "[email protected]"
);

$result = array("user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100");


$itemDetails = array();
 foreach($result as  $res){

   $itemDetails[] = 
       array(
        "User" => array(
        "uservalue" => $res['user_id'],
        "imageId" => $res['item_id']
      ),
        "price" => $res['price'],
        "qty" => $res['qty']
    ); 
 }



$final_array = array(
   "id" => $order_id,
   "billing" => $array1,
   "shipping" => $array2,
   "item"  => $itemDetails
);

 echo '<pre>';
 print_r($final_array);die;

?>

Output of current Code

Array
(
   [id] => 12345
   [billing] => Array
    (
        [firstName] => Test Fname
        [lastName] => Test Lname
        [address1] => Test Address
        [city] => Test City
        [country] => IND
        [email] => [email protected]
    )

    [shipping] => Array
    (
        [firstName] => Test Fname
        [lastName] => Test Lname
        [address1] => Test Address
        [city] => Test City
        [country] => IND
        [email] => [email protected]
    )

   [item] => Array
      (
        [0] => Array
            (
                [User] => Array
                    (
                        [uservalue] => 9999
                        [imageId] => cloth
                    )

                [price] => 500
                [qty] => 100
            )

        [1] => Array
            (
                [User] => Array
                    (
                        [uservalue] => 9999
                        [imageId] => cloth
                    )

                [price] => 500
                [qty] => 100
            )

        [2] => Array
            (
                [User] => Array
                    (
                        [uservalue] => 9999
                        [imageId] => cloth
                    )

                [price] => 500
                [qty] => 100
             )

        )

  )

Expected Result should be like below

array
(
  "id" => 12345
   "billing" => array
      (
        [firstName] => Test Fname,
        [lastName] => Test Lname,
        [address1] => Test Address,
        [city] => Test City,
        [country] => IND,
        [email] => [email protected]
      )

   "shipping" => array
       (
        [firstName] => Test Fname,
        [lastName] => Test Lname,
        [address1] => Test Address,
        [city] => Test City,
        [country] => IND,
        [email] => [email protected]
      )

  "item" => array
    (
        array
            (
                "User" => Array
                    (
                        "uservalue" => 9999,
                        "imageId" => cloth
                    )

                [price] => 500,
                [qty] => 100
            )

      )
)

I am expecting output like above, can we display the final array in that format, can anyone look into it and update me your thoughts. Thanks!!

5
  • How looks your output now? Commented Oct 1, 2019 at 6:52
  • you can get above result if result array is multidimentional or assign values to itemDetails without foreach loop Commented Oct 1, 2019 at 6:52
  • @PragneshChauhan, please post me as an answer, If result array has multliple values, that is displaying with keys Commented Oct 1, 2019 at 6:55
  • Might be worth reading up on object orientated next, a lot of this could be handled with objects, collections and getters/setters. Commented Oct 1, 2019 at 6:56
  • how to you say its a duplicated question, there the requirement is different.@mickmackusa Commented Oct 1, 2019 at 7:21

2 Answers 2

0

if you allow only single item in itemdetails

$itemDetails[] =
    [
    "User"  => [
        "uservalue" => $result['user_id'],
        "imageId"   => $result['item_id'],
    ],
    "price" => $result['price'],
    "qty"   => $result['qty'],
];

Single item demo

if itemdetails allow multiple items

$result = [
    ["user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100"],
    ["user_id" =>"9999","item_id" =>"cloth","price" =>"400","qty" =>"200"]
];

$itemDetails = array();
 foreach($result as  $res){

   $itemDetails[] = 
       array(
        "User" => array(
        "uservalue" => $res['user_id'],
        "imageId" => $res['item_id']
      ),
        "price" => $res['price'],
        "qty" => $res['qty']
    ); 
 }

Multiple items demo

Sign up to request clarification or add additional context in comments.

3 Comments

hi @Pragnesh, can we remove keys like , 0 , 1 for item array?
@Manjunath, Yes if you want an associative array, but make sure it is unique otherwise it will be overwritten
yes @Pragnesh, that will be unique, only item_id may repeat in each array, but how can we remove 0 , 1 keys from the item array, can we print exactly like in the expected output?
0

If $result has multiple values, this should work.

     $result = array(array("user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100"),array("user_id" =>"500","item_id" =>"cloth123","price" =>"511","qty" =>"80"));

        $itemDetails = array();
         foreach($result as  $res){

           $itemDetails[] = 
               array(
                "User" => array(
                "uservalue" => $res['user_id'],
                "imageId" => $res['item_id']
              ),
                "price" => $res['price'],
                "qty" => $res['qty']
            ); 
         }

        $final_array = array(
//    "id" => $order_id,
//    "billing" => $array1,
//    "shipping" => $array2,
   "item"  => $itemDetails
);

 echo '<pre>';
 print_r($final_array);

Output

Array
(
    [item] => Array
        (
            [0] => Array
                (
                    [User] => Array
                        (
                            [uservalue] => 9999
                            [imageId] => cloth
                        )

                    [price] => 500
                    [qty] => 100
                )

            [1] => Array
                (
                    [User] => Array
                        (
                            [uservalue] => 500
                            [imageId] => cloth123
                        )

                    [price] => 511
                    [qty] => 80
                )

        )

)

6 Comments

hello @Vaibhavi, can we remove keys i,e 0 , 1 for item array?
@Manjunath An array data structure or simply an array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula.
okay, but I need to send this array to some api, they requested one sample request, we need to send in that format, so in the sample request, keys are not available for item array, so i am worried, how can we remove keys
@Manjunath You can return response in api by using json_encode.
Sorry, didn't get you @Vaibhavi, you mean to say send request in json?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.