Shantanu
Shantanu

Reputation: 148

How to combine 3 or 4 arrays into one

I have three following arrays.

Array ( [0] => 395 [1] => 295 )
Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg ) 
Array ( [0] => Seller Test Product [1] => Offline Product for Test )

The first array is the quantity, the second array is for the images, the third array is for the name of the products. I want to combine all these three array into one and display it using foreach loop in PHP.

if I use array_merge(), I am getting the output:

Array ( [0] => 395 [1] => 295 ) Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg ) Array ( [0] => Seller Test Product [1] => Offline Product for Test ) Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg [2] => Seller Test Product [3] => Offline Product for Test [4] => 395 [5] => 295 )

Now, how can I display it using foreach loop in the view.

in the view the code is :

<?php foreach($c as $key => $strs)
    {  ?>

    <img style="width:150px;" src="<?php echo @getimagesize(base_url("assets/upload/product/".$key)) ? base_url("assets/upload/product/".$key):'http://placehold.it/350x200';?>" class="img-responsive">
    <input type="text" name="vala" value="<?php echo $strs; ?>">
    <input type="text" name="valas" value="<?php echo $strss; ?>">

    <?php } ?>

Any help is welcome.

Upvotes: 0

Views: 233

Answers (3)

Philipp Maurer
Philipp Maurer

Reputation: 2505

So what you really want is to group all fields of all arrays together. Values with the same index shall be merged into a single object. array_map() can be used for this.

$final = array_map(function($quantity, $image, $name) {
        return (object)['quantity' => $quantity, 'image' => $image, 'name' => $name];
    }, $quantityArray, $imageArray, $nameArray);

The result will be:

[
    {
        'qunatity' => 395,
        'image' => '5a3a13f237715637629.jpeg',
        'name' => 'Seller Test Product'
    },
    {
        'qunatity' => 295,
        'image' => '5a3b602654cfd527057.jpeg',
        'name' => 'Offline Product for Test'
    }
]

You can then address them in your foreach like this:

foreach($final as $product) {
    echo $product->name;
    echo $product->image;
    echo $product->quantity;
}

For those of you who are in a real hurry, the following call to array_map() will do the same trick, but without mapping the array fields to a specific key in the new multidimensional array:

$final = array_map(NULL, $quantityArray, $imageArray, $nameArray);

The result will be:

[
    [
        0 => 395,
        1 => '5a3a13f237715637629.jpeg',
        2 => 'Seller Test Product'
    ],
    [
        0 => 295,
        1 => '5a3b602654cfd527057.jpeg',
        2 => 'Offline Product for Test'
    ],
]

The inner arrays of the newly created mltidimensinal array will be filled in the order of which the arrays were provided to array_map().

Upvotes: 5

Andreas
Andreas

Reputation: 23958

You can loop one array and use key to get the corresponding value from the other arrays.

$a=array ( 0 => 395,1 => 295 );
$b=array ( 0 =>" 5a3a13f237715637629.jpeg" ,1 => "5a3b602654cfd527057.jpg" ) ;
$c=array ( 0 => "Seller Test Product",1 => "Offline Product for Test" );

Foreach($a as $key => $val){
    $res[$key]['qty'] = $val;
    $res[$key]['img'] = $b[$key];
    $res[$key]['desc'] = $c[$key];
}
Var_dump($res);

Output:

array(2) {
  [0]=>
  array(3) {
    ["qty"]=> int(395)
    ["img"]=> string(25) " 5a3a13f237715637629.jpeg"
    ["desc"]=> string(19) "Seller Test Product"
  }
  [1]=>
  array(3) {
    ["qty"]=> int(295)
    ["img"]=> string(23) "5a3b602654cfd527057.jpg"
    ["desc"]=> string(24) "Offline Product for Test"
  }
}

https://3v4l.org/h8B0u

Upvotes: 1

SELA
SELA

Reputation: 6823

Example Code

<?php
$array1=array ( 0 => 395,1 => 295 );
$array2=array ( 0 =>" 5a3a13f237715637629.jpeg" ,1 => "5a3b602654cfd527057.jpg" ) ;
$array3=array ( 0 => "Seller Test Product",1 => "Offline Product for Test" );
echo "<pre>";
print_r($array1);print_r($array2);print_r($array3);
$result=array_merge($array1,$array2,$array3);
print_r($result);
?>

Upvotes: -1

Related Questions