0

is there a good function that can combine array by key ? ( in this example is 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)

and

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)

to something like

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)

or with no pid on each product comments array ( nested on products array ) ?

thanks for looking in.

Adam Ramadhan

3
  • You do not care about the integer first key? Commented Mar 3, 2011 at 11:07
  • sorry i dont get which one is the int first key , the first key on products array ? Commented Mar 3, 2011 at 11:11
  • Does not mater, the 0-1 in the first array and the 0-7 in the second array. But check my solution anyway :) Commented Mar 3, 2011 at 11:13

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

What does the result look like? How "mixed"?
My fault, changed $arr1_new into $arr2_new in the second foreach, It should work now.
thank you powtac, you save me alot of my time, its 6 hours already figuring this thing out. testing one by one php array function. may god repay you in someway someday :D
No prob Adam, once everybody has to learn how to deal with multidimensional arrays ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.