drvvvuyia15070493 2012-07-07 12:26
浏览 40

按键PHP CodeIgniter的数组

I have the following response from my PHP code.

Array
(
[customer] => 402
[carat] => Array
    (
        [0] => 6
        [1] => 5
    )

[units] => Array
    (
        [0] => grams
        [1] => dwt
        [2] => dwt
    )

[weight] => Array
    (
        [0] => 5
        [1] => 3
        [2] => 6
    )

[our_payout] => Array
    (
        [0] => 15
        [1] => 9
        [2] => 60
    )

[sale_payout] => Array
    (
        [0] => 18
        [1] => 12
        [2] => 180
    )

[hidden_carat] => Array
    (
        [0] => 6
        [1] => 5
        [2] =>
    )

[hidden_unit] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 2
    )

[carat_scale_price] => Array
    (
        [0] =>
        [1] =>
        [2] =>
    )

[sales_id] => Array
    (
        [0] =>
        [1] =>
        [2] =>
    )

[currency_rate_id] => Array
    (
        [0] =>
        [1] =>
        [2] =>
    )

[gold_price_id] => Array
    (
        [0] =>
        [1] =>
        [2] =>
    )

[taget_percentage] =>
[reference] => 0
[notes] =>
[unit] => 0
[customer_id] => 402
[total_items] => 2
[submit] =>
)

And I want to join for example [0] => grams with [0] => 5 [0] => 16 and so on. How can I do that?

  • 写回答

1条回答 默认 最新

  • doutao1282 2012-07-07 14:04
    关注

    I'm with Steve, I'm not really sure what you're asking for. It looks like you want to transpose that matrix but only for some of the keys? Here's something that might do what you're looking for, assuming that your original array above is in the variable $arr:

    $lineItems = array();
    $i = 0;
    $allLineItemsHaveI = true;
    
    while($allLineItemsHaveI) {
       $lineItems[$i] = array();
       foreach($arr as $key => $subArr) {
          if(array_key_exists($i, $subArr)) {
             $lineItems[$i][$key] = $subArr[$i];
          } else {
             $allLineItemsHaveI = false;
          }
       } 
    
       $i++;
    }    
    
    array_pop($lineItems);
    

    This is the output:

    Array
    (
        [0] => Array
            (
                [carat] => 6
                [units] => grams
                [weight] => 5
                [our_payout] => 15
                [sale_payout] => 18
                [hidden_carat] => 6
            )
    
        [1] => Array
            (
                [carat] => 5
                [units] => dwt
                [weight] => 3
                [our_payout] => 9
                [sale_payout] => 12
                [hidden_carat] => 5
            )
    
    )
    

    Also, check this out: Transposing multidimensional arrays in PHP

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?