Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP数组中使用变量_Php_Arrays_Variables - Fatal编程技术网

如何在PHP数组中使用变量

如何在PHP数组中使用变量,php,arrays,variables,Php,Arrays,Variables,我有以下代码: foreach ($row as $result) { if (empty($row[28])) { $string28 = ''; } else { $string28 = '<div class="add_img"> <h1>Connexion</h1> <img src="images/

我有以下代码:

    foreach ($row as $result) {
    if (empty($row[28])) {
        $string28 = '';
    } else {
        $string28 = '<div class="add_img">
                       <h1>Connexion</h1>
                       <img src="images/' . $row[28] . '.jpeg">
                     </div>';
    }
}


foreach ($row as $result) {
    if (empty($row[30])) {
        $string30 = '';
    } else {
        $string30 = '<div class="add_img">
                      <h1>Fixation</h1>
                      <img src="images/' . $row[30] . '.jpeg">
                     </div>';
    }
}

foreach ($row as $result) {
    if (empty($row[31])) {
        $string31 = '';
    } else {
        $string31 = '<div class="add_img">
                       <h1>Schéma</h1>
                       <img src="images/' . $row[31] . '.jpeg">
                     </div>';
    }
}

$applications = array($string28, $string30, $string31);
if (empty($applications)) {
    $vide = "<h1>Pas D'Application Pour Ce Produit</h1>";
}
foreach($row as$result){
if(空($row[28])){
$28='';
}否则{
$28
连接
';
}
}
foreach($row作为$result){
if(空($row[30])){
$30='';
}否则{
$30
固定
';
}
}
foreach($row作为$result){
if(空($row[31])){
$31='';
}否则{
$31
雪玛
';
}
}
$applications=array($string28、$string30、$string31);
if(空($应用程序)){
$vide=“Pas D’Application Pour Ce Produit”;
}
我想说的是:如果所有变量都是空的,那么告诉我:

Pas D'Application Pour Ce Productuit(翻译为:本产品无应用)


但是当我使用print\r函数时,它告诉我数组没有数据要处理。
请告诉我需要帮助。

多亏了所有的高级

您的
foreach
循环是错误的。您使用的是整个数组,而不是
foreach
循环中使用的元素

你用过

foreach ($row as $result) { //You are doing something with $row } foreach($row作为$result){ //您正在使用$row执行某些操作 } 相反,正确的用法是

foreach ($row as $result) { //Do something with $result } foreach($row作为$result){ //用$result做某事 }
希望有帮助:)

您在foreach循环中没有正确访问行。使用foreach($row as$result)时,需要将数组元素引用为$result,而不是row。如果需要标识指定的数组键,可以使用foreach($key=>$result的行)来指定

例如,在第一个循环中,您应该使用:

$string28 = '';  //Initialize your variable so can be used after the foreach loop exits
foreach ($row as $key => $result) {
    if($key == 28 && empty($result[$key]) {
        $string28 = '';
    } else {
        $string28 = '<div class="add_img"><h1>Connexion</h1><img src="images/'.$result[$key].'.jpeg">
    }
}
$string28=''//初始化变量,以便在foreach循环退出后使用
foreach($key=>$result的行){
如果($key==28&&empty($result[$key]){
$28='';
}否则{
$string28='Connexion
}
}
对脚本中的其他循环重复此操作

编辑

是的,您可以设置一个foreach循环,该循环将遍历所有变量并为您创建变量。根据您的问题,如果您没有任何应用程序,将显示一条错误消息。您只需根据该条件设置一个标志即可。您可以这样做:

$noApps = true;
$applications = array();
foreach($row as $key => $result) {
    if(isset($result[$key]) && empty($result[$key])) {
       $applications[$key] = '<div class="add_img"><h1>Connexion</h1><img src="images/'.$result[$key].'.jpeg'>;
    $noApps = false;
    }
}

if($noApps) {
    echo "<h1>Pas D'Application Pour Ce Produit</h1>";
} else {    
    print_r($applications);  //For viewing/debugging purposes
}
$noApps=true;
$applications=array();
foreach($key=>$result的行){
if(设置($result[$key])&&empty($result[$key])){
$applications[$key]='Connexion;
$noApps=假;
}
}
若有($noApps){
echo“Pas D’Application Pour Ce Productuit”;
}否则{
打印($applications);//用于查看/调试
}

您的设计看起来很糟糕。您实际上是在重复相同的功能3次。 while循环不起作用,因为在它们中使用了
$row
,而不是
$result
变量。 第二点是,
$string28、$string30、$string31
被创建为您在
while loops
中编写的if条件中的局部变量。一旦控件退出
“if”
循环,这些变量就什么都不是。要解决这个问题,请尝试初始化它们以清空数据,如
$string28=“”
在代码的起点处


但是,我建议您首先查看代码的结构。

您在哪里使用print\r。Empty函数检查数组是否为空,而不是每个单独的变量。那么如何将它用于所有变量?循环遍历它们,并单独检查它们是否为空。像您的数组一样,在数组中循环并检查数组使用empty()或isset()的y值。isset()实际上会告诉您这样的变量是否已经初始化且不是null。请在foreach循环之外设置$stringXX变量。我认为您存在范围问题,因为变量已初始化(并死亡)在foreach循环内部。我编辑了我的答案,向您展示了我的意思。需要检查什么?请详细说明。我可以对所有变量使用一个foreach循环,因为我有34个变量吗?