Home > OS >  How to echo two arrays without removing duplicates?
How to echo two arrays without removing duplicates?

Time:01-12

I'm trying to display data with two different arrays but without removing their duplicate at least.

These are the data in my database that I am working with, I did explode to separate the values and store it into an array.

kilos warehouse_quantity store_quantity
25.20.15 10.5.10 0.3.10
<?php
$store = explode('.', $data->store_quantity);
$kilos = explode('.', $data->kilos);

$arr_of_store = array();
$arr_of_kilos = array();

foreach($store as $store){
    $arr_of_store[] = $store;
}

foreach($kilos as $kilos){
    $arr_of_kilos[] = $kilos;
}

echo "Stacks in store: ";

$array_together = array_combine($arr_of_store,$arr_of_kilos);
foreach($array_together as $arr_of_store => $arr_of_kilos){
    echo $arr_of_store.' sacks of '.$arr_of_kilos.'kg'.'<br>';
}

echo "<br>";
echo "Stacks in warehouse: ";

$warehouse = explode('.', $data->warehouse_quantity);
$kilos2 = explode('.', $data->kilos);

$arr_of_ware = array();
$arr_of_kilos2 = array();

foreach($warehouse as $warehouse){
    $arr_of_ware[] = $warehouse;
}

foreach($kilos2 as $kilos2){
    $arr_of_kilos2[] = $kilos2;
}

$array_together2 = array_combine($arr_of_ware,$arr_of_kilos2);

foreach($array_together2 as $arr_of_ware => $arr_of_kilos2){
    echo $arr_of_ware.' sacks of '.$arr_of_kilos2.'kg'.'<br>';
}

?>

My desired output is

Stacks in store: 
0 sacks of 25kg
3 sacks of 20kg
10 sacks of 15kg

Stacks in warehouse: 
10 sacks of 25kg
5 sacks of 20kg
10 sacks of 15kg

But what I get is this

Stacks in store: 
0 sacks of 25kg
3 sacks of 20kg
10 sacks of 15kg

Stacks in warehouse: 
10 sacks of 15kg
5 sacks of 20kg

I tried concat() based on what I found in the internet stating that It can be used to display two arrays without removing duplicate but I receive an error instead where Call to undefined function concat(). What should I do to display the desired output?

CodePudding user response:

<?php
//Simulate your data
$data = new stdClass();
$data->kilos = '25.20.15';
$data->warehouse_quantity = '10.5.10';
$data->store_quantity = '0.3.10';
//var_dump($data);

function StacksInStore($datax) {
    echo 'Stacks in store: ';
    $kilos = explode('.',$datax->kilos);
    $store_quantity = explode('.',$datax->store_quantity);
    for($i=0;$i<count($store_quantity);$i  ) {
    echo '<br/>'.$store_quantity[$i].' sacks of '.$kilos[$i].'kg';
    }
}
function StacksInWarehouse($datax) {
    echo 'Stacks in warehouse:  ';
    $kilos = explode('.',$datax->kilos);
    $warehouse_quantity = explode('.',$datax->warehouse_quantity);
    for($i=0;$i<count($warehouse_quantity);$i  ) {
    echo '<br/>'.$warehouse_quantity[$i].' sacks of '.$kilos[$i].'kg';
    }
}

/*
 * Stacks in store: 
0 sacks of 25kg
3 sacks of 20kg
10 sacks of 15kg

Stacks in warehouse: 
10 sacks of 25kg
5 sacks of 20kg
10 sacks of 15kg
 * 
 */
StacksInStore($data);
echo '<br/><hr/><br/>';
StacksInWarehouse($data);

how to video

  • Related