0

I have an array of arrays set up like so. There are a total of 10 arrays but I will just display the first 2. The second column has a unique id of between 1-10 (each only used once).

Array
(
   [0] => Array
        (
            [0] => User1
            [1] => 5
        )

    [1] => Array
        (
            [0] => User2
            [1] => 3
        )
)

I have another array of arrays:

Array
(
    [0] => Array
        (
            [0] => 3
            [1] => 10.00
        )

    [1] => Array
        (
            [0] => 5
            [1] => 47.00
        )
)

where the first column is the id and the second column is the value I want to add to the first array.

Each id (1-10) is only used once. How would I go about adding the second column from Array#2 to Array#1 matching the ID#?

4 Answers 4

2

There are tons of ways to do this :) This is one of them, optimizing the second array for search and walking the first one:

Live example

<?
$first_array[0][] = 'User1';
$first_array[0][] = 5;
$first_array[1][] = 'User2';
$first_array[1][] = 3;

$secnd_array[0][] = 3;
$secnd_array[0][] = 10.00;
$secnd_array[1][] = 5;
$secnd_array[1][] = 47.00;

// Make the user_id the key of the array
foreach ($secnd_array as $sca) {
    $searchable_second_array[ $sca[0] ] = $sca[1];
}
// Modify the original array
array_walk($first_array, function(&$a) use ($searchable_second_array) {
    // Here we find the second element of the first array in the modified second array :p
    $a[] = $searchable_second_array[ $a[1] ];
});

// print_r($first_array);
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that 0 will always be the key of the array and 1 will always be the value you'd like to add, a simple foreach loop is all you need.

Where $initial is the first array you provided and $add is the second:

<?php

$initial = array(array("User1", 5),
    array("User2", 3));

$add = array(
    array(0, 10.00),
    array(1, 47.00));

foreach ($add as $item) {
    if (isset($initial[$item[0]])) {
        $initial[$item[0]][] = $item[1];
    }
}

printf("<pre>%s</pre>", print_r($arr1[$item[0]], true));

1 Comment

Shouldn't be: printf("<pre>%s</pre>", print_r($initial[$item[0]], true));? And this only return one element
1

I don't know if I got you right, but I've come up with a solution XD

<?php
$array_1 = array(
    0 => array(
      0 => 'ID1',
      1 => 5
    ),
    1 => array(
      0 => 'ID2',
      1 => 3
    )
);

$array_2 = array(
    0 => array(
      0 => 3,
      1 => 10.00
    ),
    1 => array(
      0 => 5,
      1 => 47.00
    )
);


foreach($array_1 as $key_1 => $arr_1){
  foreach($array_2 as $key_2 => $arr_2){
    if($arr_2[0] == $arr_1[1]){
      $array_1[$key_1][2] = $arr_2[1];
    }
  }
}

var_dump($array_1);

?>

Demo: https://eval.in/201648

The short version would look like this:

<?php
$array_1 = array(array('ID1',5),array('ID2',3));

$array_2 = array(array(3,10.00),array(5,47.00));


foreach($array_1 as $key => $arr_1){
  foreach($array_2 as$arr_2){
    if($arr_2[0] == $arr_1[1]){
      $array_1[$key][2] = $arr_2[1];
    }
  }
}

var_dump($array_1);

?>

Demo: https://eval.in/201649

Hope that helps :)

Comments

1

A quick and dirty way just to show you one of the more self-explaining ways to do it :)

$users = array(
    0 => array(
        0 => 'User1',
        1 => 123
    ),
    1 => array(
        0 => 'User2',
        1 => 456
    )
);

$items = array(
    0 => array(
        0 => 123,
        1 => 'Stuff 1'
    ),
    1 => array(
        0 => 456,
        1 => 'Stuff 2'
    )
);

foreach($items as $item){
    foreach($users as $key => $user){
        if($item[0] == $user[1])
            array_push($users[$key], $item[1]);
    }
}

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.