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

7
  • Possible duplicate of Combine two arrays Commented Nov 29, 2016 at 8:57
  • Execute a loop and make it manually... Commented Nov 29, 2016 at 8:58
  • @MasivuyeCokile, This question does not match with what you through as duplicate. Commented Nov 29, 2016 at 8:59
  • @FrayneKonok "possible" understand the word? meaning he can go through that question and get pointers from it, there are many SO answers about joining/combining arrays. Commented Nov 29, 2016 at 9:01
  • @Frayne Konok Thank you for response. But I can't understand what I do. Can you give me some example? Thank you. Commented Nov 29, 2016 at 9:02

5 Answers 5

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

Comments

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);

Comments

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);

Comments

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));

Comments

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);

Comments

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.