3

I have two arrays

  1. print_r($val);

  2. print_r($results);

I need to join this two array by PERSONAL ID, example array1. 123456 to array2. 123456, also array1 654321 to array2 654321

Array 1:

Array (
[1] => Array
    (
        [0] => test1
        [1] => 123456
    )

[2] => Array
    (
        [0] => test2
        [1] => 654321
    )
)

Array 2:

Array (
[5] => Array
    (
        [login] => 123456
        [firstname] => George
        [lastname] => George
    )

[8] => Array
    (
        [personal_id] => 654321
        [firstname] => John
        [lastname] => John
    )

How can I join this two array?

Thank you

George B.
  • 172
  • 2
  • 2
  • 14

5 Answers5

1

Try This.

$new_Array = array();
foreach ($val as $key => $v) {        
    foreach ($results as $r) {
        if($v[1] == $r['login']){
            $new_Array[$key]['login'] = $r['login'];
            $new_Array[$key]['firstname'] = $r['firstname'];
            $new_Array[$key]['lastname'] = $r['lastname'];
            $new_Array[$key]['test'] = $v[0];
        }
    }
}
print_r($new_Array);
Ruby Nanthagopal
  • 596
  • 1
  • 5
  • 17
1
    $firstArray = [
        1 => [
                0 => 'test1',
                1 => '123456',
            ],
        2 => [
                0 => 'test2',
                1 => '654321',
            ]    
        ];

// move index to keys to avoid second-dimension foreach in future
    $idxCachedArray = [];
    foreach ($firstArray as $item) {
        $idxCachedArray[$item[1]] = $item;
        unset ($idxCachedArray[$item[1]][1]);
    }

    $secondArray = [
        5 => [
                'login' => 123456,
                'firstname' => 'George',
                'lastname' => 'George',
            ],
        8 => [
                'login' => 654321,
                'firstname' => 'John',
                'lastname' => 'John',
            ],
        ];

    $resultArray = [];
    foreach ($secondArray as $key => $item) {
        $resultArray[$key] = $item;
        if (isset($idxCachedArray[$item['login']])) {
            $resultArray[$key] = array_merge($resultArray[$key], $idxCachedArray[$item['login']]); 
        }
    }

    var_dump ($resultArray);
A.Mikhailov
  • 466
  • 4
  • 9
1

you can do some thing of this sort

$arr1 = [
  ['test', '123'],
  ['test2', '345']
];

$arr2 = [
  [
    'login' => '123',
    'first' => 'asddf'
  ], [
    'login' => '345',
    'first' => 'def'
  ]
];

$mergedArray = [];
foreach ($arr1 as $elem) {
  $login = $elem[1];
  $match = null;
  foreach ($arr2 as $elem2) {
    if ($elem2['login'] == $login) {
      $match = $elem2;
    }
  }

  $mergedArray[] = array_merge($elem, $match);
}

print_r($mergedArray);
abhi
  • 106
  • 5
1

try to something like this...

$val = array (
        '1' => array(
                '0' => 'test1',
                '1' => '123456',
         ),

        '2' => array(
                '0' => 'test2',
                '1' => '654321',
            )
    );
    $results = array (
        '5' => array
            (
                'login' => '123456',
                'firstname' => 'George',
                'lastname' => 'George',
            ),

        '8' => array(
                'personal_id' => '654321',
                'firstname' => 'John',
                'lastname' => 'John',
            )
    );

    foreach ($val as $key => $value) {
        $val = $value['1'];
        foreach ($results as $key1 => $value1) {
            if ($value1['login'] == $val) {
                $output = array_merge($value,$value1);
            } elseif ($value1['personal_id'] == $val) {
                $output2 = array_merge($value,$value1);
            }
        }
    }
    echo "<pre>";
    print_r(array_unique($output));
    echo "<pre>";
    print_r(array_unique($output2));
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
0

You have not make your output clearly, suppose you expected output is like this:

array(2) {
  [123456]=>
  array(4) {
    ["firstname"]=>
    string(6) "George"
    ["lastname"]=>
    string(6) "George"
    ["personal_id"]=>
    int(123456)
    [0]=>
    string(5) "test1"
  }
  [654321]=>
  array(4) {
    ["firstname"]=>
    string(4) "John"
    ["lastname"]=>
    string(4) "John"
    ["personal_id"]=>
    int(654321)
    [0]=>
    string(5) "test2"
  }
}

here is the code, and the demo, hope it helps.

  <?php
    $val = array (
            '1' => array(
                    '0' => 'test1',
                    '1' => '123456',
             ),

            '2' => array(
                    '0' => 'test2',
                    '1' => '654321',
                )
        );
        $results = array (
            '5' => array
                (
                    'login' => '123456',
                    'firstname' => 'George',
                    'lastname' => 'George',
                ),

            '8' => array(
                    'personal_id' => '654321',
                    'firstname' => 'John',
                    'lastname' => 'John',
                )
        );



    /*
      $indexOfPrimarykey is the nth of index as the specify id, to make it more universely, I used index.
      You array is just the first and the last index, here also can use
      reset($array); $first_key = key($array); to get the first key, 
      and
      end($array); $last_key = key($array); to get the last key.
    */
    function mergeArray($array, $indexOfPrimarykey)
    {
      $o = [];
      array_walk($array, function($v) use($indexOfPrimarykey, &$o)
      {
        $keys = array_keys($v);
        $k = $keys[$indexOfPrimarykey];
        $value = $v[$k];
        unset($v[$k]);
        $o[$value] = $v;
      });
      return $o;
    }
    $val = mergeArray($val,1);
    $results = mergeArray($results,0);
    array_walk($val, function($v, $k) use(&$results)
    {
      $results[$k] += ['personal_id' => $k]  + $v; 
    });
    var_dump($results);
LF00
  • 27,015
  • 29
  • 156
  • 295