1

I have to add some elements from an array into another array for a project.

Here's the deal: I have 2 arrays, from 2 tables of a database, which are named $stand and $signal.

$stand is made up of arrays:

$stand = [[id, name, width, length,...], [id, name, width, length,...], ...]

$signal is made up of arrays as well:

$signal = [[id, num, name, adress, ...], [id, num, name, adress, ...], ...]

Each entry of $stand matches with an entry of $signal: the id of an entry of $stand is equal to the number of elements in $signal.

For these entries, I'd like to add the content of the entry of $signal at the end of the entry of $stand.

Here's the code I used, but unfortunately it doesn't work:

foreach ($stand as $st) {
   foreach ($signal as $sig) {
      if ($st[0] == $sig[1]) {
         $st[]=$sig;
      }
   }
}
5
  • Have a look at array_push. Commented Dec 31, 2013 at 10:51
  • 1
    May I suggest to make it via SQL? Commented Dec 31, 2013 at 10:52
  • @Rikesh : I did (array_push($st,$sig); instead of $st[]=$sig;) but it didn't work either. Commented Dec 31, 2013 at 10:53
  • regarding your code, you would want to add to array $stand not to its elements. Better approach is array_merge Commented Dec 31, 2013 at 10:57
  • The OP asked to add array B to existing array A. NOT create a new array C = array_merge(A, B). Commented Jun 29 at 2:33

5 Answers 5

8

array_merge is the elegant way:

$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b); 
// $merge is now equals to array('a','b','c','d');

Doing something like:

$merge = $a + $b;
// $merge now equals array('a','b')

Will not work, because the + operator does not actually merge them. If they $a has the same keys as $b, it won't do anything.

Sign up to request clarification or add additional context in comments.

Comments

2

Use array_merge function.

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

For more detail read here

Comments

1
foreach ($stand as $key => $st) {
   foreach ($signal as $sig) {
      if ($st[0] == $sig[1]) {
         $stand[$key][]=$sig;
      }
   }
}

3 Comments

It worked with this solution! Sorry about that question btw, i'm a beginner in PHP.
The solution is correct but you should try to avoid copying the code without understanding (unless you do understand what he did their). And Paul, it would be great if you mentioned why you did what you did :).
When I saw this answer, I've searched about that '$key =>' code, because I didn't learned it. And I realized I made a huge mistake using $st in my condition instead of $stand.
0

$st seems to be a copy of an element array, but not an element reference. So modifications you make to $st are lost. Thus you should precede $st with an ampersand to use it as a reference:

foreach($stand as &$st)

Comments

0

As I understand, you want something like this

$array1 = array(array('id'=>1,'name'=>'manish'),array('id'=>2,'name'=>'bhuvnesh'));

$array2 = array(array('id'=>1,'color'=>'red'),array('id'=>2,'color'=>'green'));

$newArray = array();

foreach($array1 as $key => $vals) {
    $id = $vals['id'];
    $color = $array2[$key]['color'];
    $newArray[] = array('id'=>$id, 'name'=>$vals['name'], 'color'=>$color);
}

echo '<pre>';
print_r($newArray);

This will return

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => manish
            [color] => red
        )

    [1] => Array
        (
            [id] => 2
            [name] => bhuvnesh
            [color] => green
        )

)

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.