How to combine two arrays in one php?

2

I'm a little bit starting with php this and I was wondering if it's possible to combine two arrays , more or less my idea is this:

 $fruits = array("id"=>"1", "name"=>"banana");
   $vegetables = array("id"=>"1", "name"=>"cabbage");
   foreach($fruits as $fruit)
   {               
      $results = array("id"=>$fruit->id, "description"=>$fruit->name);
   }
  // $results = array("id"=>" ", "description"=>" ");
   return $results;

I want to know if I can return, those two array within a single one, with those two keys " id " and " description ", is there any method to do it? I tried with array_fill_keys but it did not work. thanks.

    
asked by danielvkp 28.01.2016 в 14:23
source

3 answers

5

Create an array and add the values of the other arrays at the end of the first array:

$results = array();
$results[] = $fruits;
$results[] = $vegetables;
return $results;

For your comment, what you need is not only to merge the arrays into a multidimensional array, but also to add a mapping function for all the elements of this new multidimensional array. You can use array_map to map the elements of the new multidimensional array into a new one with the data that you require.

$results = array();
$results[] = $fruits;
$results[] = $vegetables;
$mapeo = function($elemento) {
    return array(
        'id' => $elemento['id'],
        'description' => $elemento['name']
        );
}
return array_map($mapeo, $results);

The array_map function will do the following:

  • Go through the elements of $results provided in the second argument.
  • For each element, apply the $mapeo function provided in the first argument.
  • Return as a result a new array that has the result of applying $mapeo to each element of $results .
  • Considering that this is what you are looking for, the code can be reduced to:

    $mapeo = function($elemento) {
        return array(
            'id' => $elemento['id'],
            'description' => $elemento['name']
            );
    }
    return array_map($mapeo, array($fruits, $vegetables));
    

    Note: I am using lambdas, enabled in PHP since 5.3.0

        
    answered by 28.01.2016 / 15:33
    source
    2

    Look at the array_mege () function of PHP itself

    The example they put:

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

    The result of the example would be:

    Array
    (
        [color] => green
        [0] => 2
        [1] => 4
        [2] => a
        [3] => b
        [shape] => trapezoid
        [4] => 4
    )
    
        
    answered by 28.01.2016 в 14:52
    0

    Using the array_merge ()

    method
    <?php
    $fruits =  array("id"=>"1", "name"=>"banana");
    $vegetables= array("id"=>"1", "name"=>"cabbage");
    $result = array_merge($fruits , $vegetables);
    print_r($result);
    ?>
    

    Example:

    Example array_merge () link

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

    The example would have an exit:

    Array
    (
        [color] => green
        [0] => 2
        [1] => 4
        [2] => a
        [3] => b
        [shape] => trapezoid
        [4] => 4
    )
    
        
    answered by 28.01.2016 в 16:04