Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

Returing An Array From 2 Functions

The purpose of the code below is to show you how you can take rows within a while loop and call a function.

Then, you call another function within a function. In the second function, you will return an array that get stacked into a larger array.

Finally, the array is parsed outside of the loop. PHP programmers can face this type of logic when they work with other people’s scripts.

Note: You can get the same results by returning a single function as described on this page.

<?php
include('connect.inc');
$db=public_db_connect();

function second_func($var2,$date2){
global $db; 

$my_values2[] = $var2.",".$date2;
return $my_values2; //return an array
}


function get_data($var1,$date){
global $db; 

$my_values = second_func($var1,$date);
return $my_values; //return an array
}
 
$command = "SELECT DISTINCT id, email FROM table_sort WHERE id IN (1,2,3,4,5,6,7) ORDER BY id ASC ";
$result = mysqli_query($db, $command);
 
while($row = mysqli_fetch_assoc($result)) {
$var1 = $row['id'];
 
$command2= "SELECT min(date) as date FROM table_sort WHERE id ='$var1' ORDER BY id ASC";
$result2 = mysqli_query($db, $command2);
 
while($row2 = mysqli_fetch_assoc($result2)){
 
$date = $row2['date'];
echo $var1."-".$date."<br/>";
$get_data = get_data($var1,$date);
$my_array[] = $get_data;
}
}

print_r($my_array);

foreach ($my_array as $key => $value) {
$value = implode(",",$value);
echo "<br/>".$key."-".$value;
$value2 = str_replace(",","-",$value);
echo "<br/>".$value2;

}
?>