数组在php中由数组键组合?

时间:2011-03-03 11:03:18

标签: php

是否有一个很好的功能,可以按键组合数组? (在此示例中为pid)

array
  0 => 
    array
      'product' => string 'a product pid 3' (length=9)
      'name' => string 'adamramadhan' (length=12)
      'pid' => string '3' (length=1)
      'timecreate' => string '2011-02-26 13:30:07' (length=19)
  1 => 
    array
      'product' => string 'a product pid 4' (length=8)
      'name' => string 'adamramadhan' (length=12)
      'pid' => string '4' (length=1)
      'timecreate' => string '2011-02-26 13:30:54' (length=19)

array
  0 => 
    array
      'pid' => string '3' (length=1)
      'comment' => string 'a comment on pid 3' (length=8)
  1 => 
    array
      'pid' => string '4' (length=1)
      'comment' => string 'a comment on pid 4' (length=8)
  2 => 
    array
      'pid' => string '3' (length=1)
      'comment' => string 'a comment on pid 3' (length=5)
  3 => 
    array
      'pid' => string '4' (length=1)
      'comment' => string 'a comment on pid 4' (length=5)
  4 => 
    array
      'pid' => string '3' (length=1)
      'comment' => string 'a comment on pid 3' (length=7)
  5 => 
    array
      'pid' => string '4' (length=1)
      'comment' => string 'a comment on pid 4' (length=7)
  6 => 
    array
      'pid' => string '3' (length=1)
      'comment' => string 'a comment on pid 3' (length=18)
  7 => 
    array
      'pid' => string '4' (length=1)
      'comment' => string 'a comment on pid 4' (length=18)

类似

array
  0 => 
    array
      'product' => string 'a product pid 3' (length=9)
      'name' => string 'adamramadhan' (length=12)
      'pid' => string '3' (length=1)
      'timecreate' => string '2011-02-26 13:30:07' (length=19)
      'comments' => array 
        0 => 
          array
            'pid' => string '3' (length=1)
            'comment' => string 'a comment on pid 3' (length=8)
        2 => 
          array
            'pid' => string '3' (length=1)
            'comment' => string 'a comment on pid 3' (length=5)
        4 => 
          array
            'pid' => string '3' (length=1)
            'comment' => string 'a comment on pid 3' (length=7)
        6 => 
          array
            'pid' => string '3' (length=1)
            'comment' => string 'a comment on pid 3' (length=18)
  1 => 
    array
      'product' => string 'a product pid 4' (length=8)
      'name' => string 'adamramadhan' (length=12)
      'pid' => string '4' (length=1)
      'timecreate' => string '2011-02-26 13:30:54' (length=19)
      'comments' => array 
        1 => 
          array
            'pid' => string '4' (length=1)
            'comment' => string 'a comment on pid 4' (length=8)
        3 => 
          array
            'pid' => string '4' (length=1)
            'comment' => string 'a comment on pid 4' (length=5)
        5 => 
          array
            'pid' => string '4' (length=1)
            'comment' => string 'a comment on pid 4' (length=7)
        7 => 
          array
            'pid' => string '4' (length=1)
            'comment' => string 'a comment on pid 4' (length=18)

或每个产品评论数组都没有pid(嵌套在产品数组上)?

感谢您的光临。

Adam Ramadhan

1 个答案:

答案 0 :(得分:2)

// Use pid as key
foreach ($arr1 as $key => $value) {
    $arr1_new[$value['pid']] = $value;
}

// Move comments into $arr1_new
foreach($arr2 as $key => $value) {
    $arr2_new[$value['pid']]['comments'][] = $value['comment'];
    // $arr2_new[$value['pid']]['comments'][] = $value; // if you really need all information here...
}

var_dump($arr2_new);