PHP:从foreach循环中的函数调用创建数组并合并它们
此代码的目的是识别调用代码所在文件夹中的所有图像文件,以便创建图像库。图像以字母数字顺序列出,但我需要特定的顺序,因此使用标准PHP数组排序函数重新排序无法满足我的需要 我使用if语句将图像集合放置到不同的数组中,然后将数组合并到所需的顺序中 当我将代码作为foreach循环的一部分运行时,它运行良好。我想将if条件放入函数中以重用代码,但当我将代码复制并粘贴到函数中时,只会得到一个空白页:PHP:从foreach循环中的函数调用创建数组并合并它们,php,arrays,if-statement,foreach,Php,Arrays,If Statement,Foreach,此代码的目的是识别调用代码所在文件夹中的所有图像文件,以便创建图像库。图像以字母数字顺序列出,但我需要特定的顺序,因此使用标准PHP数组排序函数重新排序无法满足我的需要 我使用if语句将图像集合放置到不同的数组中,然后将数组合并到所需的顺序中 当我将代码作为foreach循环的一部分运行时,它运行良好。我想将if条件放入函数中以重用代码,但当我将代码复制并粘贴到函数中时,只会得到一个空白页: // echo statements are just for testing. foreach(gl
// echo statements are just for testing.
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var03){
$img_src03 = basename($var03);
$img_label03 = pathinfo($var03, PATHINFO_FILENAME);
// Assign specific values to the arrays as you cycle through $img_src03 values:
if (substr($img_src03, 0, 5) == 'ext_f'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 5) == 'ext_r'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 6) == 'ext_po'){
if (!isset($array34)) {
$array34 = array();
}
$array34[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bed'){
if (!isset($array35)) {
$array35 = array();
}
$array35[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bth'){
if (!isset($array36)) {
$array36 = array();
}
$array36[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
}
}
$arrayFinal = array_merge($array33, $array34, $array35, $array36);
echo 'This is $arrayFinal:<br><pre>'; print_r($arrayFinal); echo '</pre><br>';
//echo语句仅用于测试。
foreach(glob(IMAGEPATH.*.{jpg,png,gif,jpg,png,gif}),glob_括号作为$var03){
$img_src03=basename($var03);
$img_label03=pathinfo($var03,pathinfo_文件名);
//在循环使用$img_src03值时,为数组指定特定值:
如果(substr($img_src03,0,5)=‘ext_f’){
如果(!isset($array33)){
$array33=数组();
}
$array33[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img\u src03,0,5)='ext\u r'){
如果(!isset($array33)){
$array33=数组();
}
$array33[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img_src03,0,6)=‘ext_po’){
如果(!isset($array34)){
$array34=数组();
}
$array34[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img_src03,0,3)=‘bed’){
如果(!isset($array35)){
$array35=数组();
}
$array35[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img_src03,0,3)='bth'){
如果(!isset($array36)){
$array36=数组();
}
$array36[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}
}
$arrayFinal=array\u merge($array33、$array34、$array35、$array36);
echo“这是$arrayFinal:
”;打印($arrayFinal);回声“
”;
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var03){
$img_src03 = basename($var03);
$img_label03 = pathinfo($var03, PATHINFO_FILENAME);
// Trying to use a function call to run the if conditional. Function is outside the foreach loop. Nothing returned.
findImage03($img_src03, $img_label03);
}
function findImage03($img_src03, $img_label03){
// Assign specific values to the arrays as you cycle through $img_src03 values:
if (substr($img_src03, 0, 5) == 'ext_f'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 5) == 'ext_r'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 6) == 'ext_po'){
if (!isset($array34)) {
$array34 = array();
}
$array34[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bed'){
if (!isset($array35)) {
$array35 = array();
}
$array35[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bth'){
if (!isset($array36)) {
$array36 = array();
}
$array36[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
}
}
$arrayFinal = array_merge($array33, $array34, $array35, $array36);
echo 'This is $arrayFinal:<br><pre>'; print_r($arrayFinal); echo '</pre><br>';
// Set $iwd Image Working Directory:
// Required before gallery_ctrl_body.php inlcude as $iwd is needed before include.
// class IWD changes outside the class.
class IWD {
// Properties
public $iwd;
}
$var = new IWD();
$var->name = getcwd();
$iwd = $var->name;
// Script include needs to be run before function pid_text().
include_once ('../gallery_ctrl_body.php');
# Function to identify the property sub-division from directory path characters using preg_match()
# and define a Property ID Text:
function pid_text($sub_div) { //Need parameter/s in function():
$sub_div_abb = sub_div_abb($sub_div); //20200220 code
$psc = 'ORL'; // Start of Property System Code.
$str = getcwd();
if(preg_match("/{$sub_div}/i", $str)) {
$psc = $psc . $sub_div_abb . strtoupper(basename(__DIR__)); //sub_div_abb may already defined in UPPERCASE.
$pid_textname = constant("PROPERTY_TEXT") . $psc; //strtoupper($psc);
}
return $pid_textname; //Return value AFTER IF statement. REQUIRED!
}
$pid_text = pid_text($sub_div = 'test_200828');
// Define the imagepath and variables:
define('IMAGEPATH', dirname(__FILE__).'/'); // '/' is required.
$img_height = 'height: 400px'; // default image height for gallery.
$img_width = 'width: 600px'; // default image width for gallery.
$img_counter = 0;
$img_style = 'style="' . $img_width . '"'; // only needs to be set once here.
$img_total = 0;
// Create the final array with function call to get images:
$arrayFinal = getImages();
// Calculate total number of images before displaying <div>
// If calculated inside a foreach() $img_counter always equals $img_total.
$img_total = count($arrayFinal);
echo '<div class="property_text-container">';
echo 'Gallery for ' . $pid_text;
echo '</div>';
//<!-- Slideshow container -->
echo '<div class="slideshow-container">';
// foreach ($array1 as $value){
foreach ($arrayFinal as $value){
$img_counter ++; // Set before function() when using function call.
// Call the function located in gallery_ctrl_body:
createGallery($img_width, $img_style, $filename, $img_name, $img_src, $img_counter, $img_total, $value, $pid_text);
}
// <!-- Next and previous buttons -->
echo '<a class="prev" onclick="plusSlides(-1)">❮</a>';
echo '<a class="next" onclick="plusSlides(1)">❯</a> ';
echo '</div>';
// <!-- END: div class="slideshow-container"-->
echo '<br>';
// <!-- Dot container -->
echo '<div class="dot-container">';
// <!-- The dots/circles -->
echo '<div style="text-align:center">';
$slide_counter = 0;
while ($slide_counter < $img_total) {
$slide_counter ++;
echo '<span class="dot" onclick="currentSlide(' . $slide_counter . ')"></span> ';
}
echo '</div>';
echo '</div>';
// Script include needs to be run at end of page including it.
include_once ('../gallery_ctrl_script.php');
如果将完全相同的if条件放在函数findImage03($img_src03,$img_label03)中(该函数位于foreach循环外部),然后从foreach循环内部调用,则代码无法工作
// Define the function to get all the images in the current folder:
function getImages(){
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var){
$img_src = basename($var);
// Assign specific values to the array as you cycle through the collection. Order here will not affect final array order.
if (substr($img_src, 0, 5) == 'bed_1'){
if (!isset($arrayBed_1)) {
$arrayBed_1 = array();
}
$arrayBed_1[] = $img_src;
} elseif (substr($img_src, 0, 5) == 'bed_2'){
if (!isset($arrayBed_2)) {
$arrayBed_2 = array();
}
$arrayBed_2[] = $img_src;
} elseif (substr($img_src, 0, 5) == 'bed_3'){
if (!isset($arrayBed_3)) {
$arrayBed_3 = array();
}
$arrayBed_3[] = $img_src;
// continue for each type required.
} }
} //End of foreach().
// Create the pre final array for other arrays to push to which defines the sort order:
$arrayPreFinal = array();
if (isset($arrayExt_f)){ // ext_f = exterior front
foreach ($arrayExt_f as $val){
array_push($arrayPreFinal, $val);
}
}
if (isset($arrayExt_r)){ // ext_r = exterior rear
foreach ($arrayExt_r as $val){
array_push($arrayPreFinal, $val);
}
}
if (isset($arrayBed_1)){ // bed_1 = bedroom 1
foreach ($arrayBed_1 as $val){
array_push($arrayPreFinal, $val);
}
}
if (isset($arrayBed_2)){ // bed_2 = bedroom 2
foreach ($arrayBed_2 as $val){
array_push($arrayPreFinal, $val);
}
}
// continue for as many variances as require.
return $arrayPreFinal;
} // End of function()
foreach(glob(IMAGEPATH.*.{jpg,png,gif,jpg,png,gif}),glob_大括号,作为$var03){
$img_src03=basename($var03);
$img_label03=pathinfo($var03,pathinfo_文件名);
//正在尝试使用函数调用运行if条件函数。函数在foreach循环之外。未返回任何内容。
findImage03($img_src03,$img_label03);
}
函数findImage 03($img\u src03,$img\u label03){
//在循环使用$img_src03值时,为数组指定特定值:
如果(substr($img_src03,0,5)=‘ext_f’){
如果(!isset($array33)){
$array33=数组();
}
$array33[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img\u src03,0,5)='ext\u r'){
如果(!isset($array33)){
$array33=数组();
}
$array33[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img_src03,0,6)=‘ext_po’){
如果(!isset($array34)){
$array34=数组();
}
$array34[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img_src03,0,3)=‘bed’){
如果(!isset($array35)){
$array35=数组();
}
$array35[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}elseif(substr($img_src03,0,3)='bth'){
如果(!isset($array36)){
$array36=数组();
}
$array36[]=$img_src03;
echo$img_src03':Image label='.img_label($img_label03)。“
”;
}
}
$arrayFinal=array\u merge($array33、$array34、$array35、$array36);
echo“这是$arrayFinal:
”;打印($arrayFinal);回声“
”;
foreach(glob(IMAGEPATH."*.{jpg,png,gif,JPG,PNG,GIF}", GLOB_BRACE) as $var03){
$img_src03 = basename($var03);
$img_label03 = pathinfo($var03, PATHINFO_FILENAME);
// Trying to use a function call to run the if conditional. Function is outside the foreach loop. Nothing returned.
findImage03($img_src03, $img_label03);
}
function findImage03($img_src03, $img_label03){
// Assign specific values to the arrays as you cycle through $img_src03 values:
if (substr($img_src03, 0, 5) == 'ext_f'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 5) == 'ext_r'){
if (!isset($array33)) {
$array33 = array();
}
$array33[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 6) == 'ext_po'){
if (!isset($array34)) {
$array34 = array();
}
$array34[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bed'){
if (!isset($array35)) {
$array35 = array();
}
$array35[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
} elseif (substr($img_src03, 0, 3) == 'bth'){
if (!isset($array36)) {
$array36 = array();
}
$array36[] = $img_src03;
echo $img_src03 . ' : Image label = ' . img_label($img_label03) . '<br>';
}
}
$arrayFinal = array_merge($array33, $array34, $array35, $array36);
echo 'This is $arrayFinal:<br><pre>'; print_r($arrayFinal); echo '</pre><br>';
// Set $iwd Image Working Directory:
// Required before gallery_ctrl_body.php inlcude as $iwd is needed before include.
// class IWD changes outside the class.
class IWD {
// Properties
public $iwd;
}
$var = new IWD();
$var->name = getcwd();
$iwd = $var->name;
// Script include needs to be run before function pid_text().
include_once ('../gallery_ctrl_body.php');
# Function to identify the property sub-division from directory path characters using preg_match()
# and define a Property ID Text:
function pid_text($sub_div) { //Need parameter/s in function():
$sub_div_abb = sub_div_abb($sub_div); //20200220 code
$psc = 'ORL'; // Start of Property System Code.
$str = getcwd();
if(preg_match("/{$sub_div}/i", $str)) {
$psc = $psc . $sub_div_abb . strtoupper(basename(__DIR__)); //sub_div_abb may already defined in UPPERCASE.
$pid_textname = constant("PROPERTY_TEXT") . $psc; //strtoupper($psc);
}
return $pid_textname; //Return value AFTER IF statement. REQUIRED!
}
$pid_text = pid_text($sub_div = 'test_200828');
// Define the imagepath and variables:
define('IMAGEPATH', dirname(__FILE__).'/'); // '/' is required.
$img_height = 'height: 400px'; // default image height for gallery.
$img_width = 'width: 600px'; // default image width for gallery.
$img_counter = 0;
$img_style = 'style="' . $img_width . '"'; // only needs to be set once here.
$img_total = 0;
// Create the final array with function call to get images:
$arrayFinal = getImages();
// Calculate total number of images before displaying <div>
// If calculated inside a foreach() $img_counter always equals $img_total.
$img_total = count($arrayFinal);
echo '<div class="property_text-container">';
echo 'Gallery for ' . $pid_text;
echo '</div>';
//<!-- Slideshow container -->
echo '<div class="slideshow-container">';
// foreach ($array1 as $value){
foreach ($arrayFinal as $value){
$img_counter ++; // Set before function() when using function call.
// Call the function located in gallery_ctrl_body:
createGallery($img_width, $img_style, $filename, $img_name, $img_src, $img_counter, $img_total, $value, $pid_text);
}
// <!-- Next and previous buttons -->
echo '<a class="prev" onclick="plusSlides(-1)">❮</a>';
echo '<a class="next" onclick="plusSlides(1)">❯</a> ';
echo '</div>';
// <!-- END: div class="slideshow-container"-->
echo '<br>';
// <!-- Dot container -->
echo '<div class="dot-container">';
// <!-- The dots/circles -->
echo '<div style="text-align:center">';
$slide_counter = 0;
while ($slide_counter < $img_total) {
$slide_counter ++;
echo '<span class="dot" onclick="currentSlide(' . $slide_counter . ')"></span> ';
}
echo '</div>';
echo '</div>';
// Script include needs to be run at end of page including it.
include_once ('../gallery_ctrl_script.php');
在研究这个问题时,我想我发现了几个问题,所以我不确定这是不是因为我不是一名程序员
//设置$iwd映像工作目录:
//在gallery\u ctrl\u body.php导入之前需要,因为在包含之前需要$iwd。
//类IWD在类外更改。
IWD类{
//性质
公费$iwd;
}
$var=新的IWD();
$var->name=getcwd();
$iwd=$var->name;
//需要在函数pid_text()之前运行脚本include。
包括一次('../gallery\u ctrl\u body.php');
#函数使用preg_match()从目录路径字符识别属性子分区
#并定义属性ID文本:
函数pid_text($sub_div){//函数()中需要参数:
$sub_div_abb=sub_div_abb($sub_div);//20200220代码
$psc='ORL';//属性系统代码的开始。
$str=getcwd();
if(preg_match(“/{$sub_div}/i”,$str)){
$psc=$psc.$sub_div_abb.strtoupper(basename(_DIR__));//sub_div_abb可能已经用大写字母定义。
$pid_textna