0

I have two arrays

array1:

  [0]=>array(
     ['user']=>'name1',
     ['age']=>'28'     
  ),
  [1]=>array(
     ['user']=>'name2',
     ['age']=>'58'     
  ),
  [2]=>array(
     ['user']=>'name3',
     ['age']=>'15'     
  )

array2

  [0]=>array(
     ['user']=>'name3',
     ['sex']=>'male'     
  ),
  [1]=>array(
     ['user']=>'name1',
     ['sex']=>'male'     
  ),
  [2]=>array(
     ['user']=>'name2',
     ['sex']=>'female'
  )

Expecting result

  [0]=>array(
     ['user']=>'name1',
     ['age']=>'28',
     ['sex']=>'male'     
  ),
  [1]=>array(
     ['user']=>'name2',
     ['age']=>'58',
     ['sex']=>'male'     
  ),
  [2]=>array(
     ['user']=>'name3',
     ['age']=>'15',
     ['sex']=>'female'
  )

I have tried lot of ways array_merge, array_combine . But i dont know where i did mistakes. please help me

4
  • I think this only php question not mysql/ Commented Mar 31, 2014 at 14:32
  • @MahdiParsa.. I am getting this result from mysql query only...Sorry.. Can u help me.. Commented Mar 31, 2014 at 14:35
  • Possible duplicate: stackoverflow.com/questions/13469803/… Commented Mar 31, 2014 at 14:37
  • this can be done with php, but it's probably better to join the 2 queries, can you share them please? Commented Mar 31, 2014 at 14:39

2 Answers 2

2

Well, if you cannot find built-in function to match your particular case (and there will be plenty of such cases), then maybe you have to try by yourself?

Map the arrays by the name key and fill a new one:

<?php
$arr1 =array ( 0=>array(
     'user'=>'name1',
     'age'=>'28'     
  ),
  1=>array(
     'user'=>'name2',
     'age'=>'58'     
  ),
  2=>array(
     'user'=>'name3',
     'age'=>'15'     
  )
);

$arr2 =array (0=>array(
     'user'=>'name3',
     'sex'=>'male'     
  ),
  1=>array(
     'user'=>'name1',
     'sex'=>'male'     
  ),
  2=>array(
     'user'=>'name2',
     'sex'=>'female'
  )
);

$result = array();
foreach ($arr1 as $key => $value) {
    foreach ($arr2 as $k => $v) {
        if($value['user'] == $v['user']) {
            $result[$k]['user'] = $arr1[$k]['user'];
            $result[$k]['age'] = $arr1[$k]['age'];
            $result[$k]['sex'] = $arr2[$k]['sex'];
        }
    }
}

var_dump($result);

array (size=3)
  1 => 
    array (size=3)
      'user' => string 'name2' (length=5)
      'age' => string '58' (length=2)
      'sex' => string 'male' (length=4)
  2 => 
    array (size=3)
      'user' => string 'name3' (length=5)
      'age' => string '15' (length=2)
      'sex' => string 'female' (length=6)
  0 => 
    array (size=3)
      'user' => string 'name1' (length=5)
      'age' => string '28' (length=2)
      'sex' => string 'male' (length=4)

You can use sort to sort by keys:

sort($result);
var_dump($result);

array (size=3)
  0 => 
    array (size=3)
      'user' => string 'name1' (length=5)
      'age' => string '28' (length=2)
      'sex' => string 'male' (length=4)
  1 => 
    array (size=3)
      'user' => string 'name2' (length=5)
      'age' => string '58' (length=2)
      'sex' => string 'male' (length=4)
  2 => 
    array (size=3)
      'user' => string 'name3' (length=5)
      'age' => string '15' (length=2)
      'sex' => string 'female' (length=6)
Sign up to request clarification or add additional context in comments.

1 Comment

The only answer that actually does what he asked for. +1.
-1

I would use array_merge

Like below..

$result = array_merge($array1, $array2);
print_r($result);

Then use array_unique to get the unique values.

1 Comment

thanks u for ur response. I tried this but not working

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.