【问题标题】:Forming final array with multiple array in PHP [duplicate]在PHP中形成具有多个数组的最终数组[重复]
【发布时间】:2019-10-01 06:43:59
【问题描述】:

我需要形成最终的数组,它没有显示我的期望,这是我使用的代码。

我需要以预期的格式打印最终的数组,我已经在下面的部分中附加了我正在寻找的输出,

<?php

 $order_id = 12345;
$array1 = array(
   "firstName" => "Test Fname",
   "lastName" => "Test Lname",
   "address1" => "Test Address",
   "city" => "Test City",
   "country" => "IND",
   "email" => "test@blank.in"
 );

$array2 = array(
   "firstName" => "Test Fname",
   "lastName" => "Test Lname",
   "address1" => "Test Address",
   "city" => "Test City",
   "country" => "IND",
   "email" => "test@blank.in"
);

$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;

?>

当前代码的输出

Array
(
   [id] => 12345
   [billing] => Array
    (
        [firstName] => Test Fname
        [lastName] => Test Lname
        [address1] => Test Address
        [city] => Test City
        [country] => IND
        [email] => test@blank.in
    )

    [shipping] => Array
    (
        [firstName] => Test Fname
        [lastName] => Test Lname
        [address1] => Test Address
        [city] => Test City
        [country] => IND
        [email] => test@blank.in
    )

   [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
             )

        )

  )

预期结果应如下所示

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

   "shipping" => array
       (
        [firstName] => Test Fname,
        [lastName] => Test Lname,
        [address1] => Test Address,
        [city] => Test City,
        [country] => IND,
        [email] => test@blank.in
      )

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

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

      )
)

我期待像上面这样的输出,我们能否以该格式显示最终数组,任何人都可以查看它并更新您的想法。谢谢!!

【问题讨论】:

  • 您的输出现在看起来如何?
  • 如果result 数组是多维数组或在没有foreach 循环的情况下将值分配给itemDetails,您可以获得上述结果
  • @PragneshChauhan,请发帖作为答案,如果结果数组有多个值,则用键显示
  • 接下来可能值得一读面向对象的内容,其中很多内容可以通过对象、集合和 getter/setter 来处理。
  • 怎么说它是一个重复的问题,要求不同。@mickmackusa

标签: php arrays associative-array


【解决方案1】:

如果您只允许 itemdetails 中的单个项目

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

Single item demo

如果itemdetails 允许多个项目

$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

【讨论】:

  • 嗨@Pragnesh,我们可以为项目数组删除像 , 0 , 1 这样的键吗?
  • @Manjunath,是的,如果你想要一个关联数组,但要确保它是唯一的,否则它将被覆盖
  • 是的@Pragnesh,这将是唯一的,只有 item_id 可以在每个数组中重复,但是我们如何从 item 数组中删除 0 、 1 键,我们可以像预期的输出一样打印吗?
【解决方案2】:

如果$result 有多个值,这应该可以工作。

     $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);

输出

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
                )

        )

)

【讨论】:

  • 你好@Vaibhavi,我们可以删除项目数组的键 i,e 0 , 1 吗?
  • @Manjunath 数组数据结构或简称数组是由一组元素(值或变量)组成的数据结构,每个元素由至少一个数组索引或键标识。存储一个数组,以便可以通过数学公式从其索引元组计算每个元素的位置。
  • 好的,但是我需要将这个数组发送到某个 api,他们请求了一个示例请求,我们需要以该格式发送,所以在示例请求中,项目数组的键不可用,所以我很担心,我们怎样才能删除密钥
  • @Manjunath 您可以使用json_encode在api中返回响应。
  • 抱歉,@Vaibhavi 没听清你的意思,你的意思是用 json 格式发送请求?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 2020-02-18
  • 1970-01-01
  • 2010-11-17
  • 1970-01-01
  • 2018-11-20
相关资源
最近更新 更多