Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i would like to merge this array such that its one array

<?php

i have decalered the array before the for loop

$sample=array();

I am Fetching database data which is in form of eg 5,6,2 i had imploded the before and stored them

$myseats= $db->displaySeats($showtime_id);
foreach ($myseats as $key) {
    $bookedseats= $key['seats_booked'];

Exploding the values to $test variable

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    $test=explode(",", $bookedseats);
    array_push($sample,$test);
}
print_r($sample) ;
?>

the problem is I am getting this output

Array ( [0] => Array ( [0] => 22 
                        [1] => 26 
                        [2] => 27 
                        [3] => 37 
                        [4] => 38 
                        [5] => 41 
                    ) 
        [1] => Array ( [0] => 30 
                        [1] => 31 
                        [2] => 32 
                    ) 
    )

that is two arrays inside an array..the database has this two entries which am using a test

>Solution :

Instead of pushing arrays into an array, use array_merge() to place all these items into a single array as part of your loop

$sample = [];
foreach ( $rows as $bookedseats ){
    $test=explode(",", $bookedseats);
    $sample = array_merge($sample, $test);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading