如果条件在关联数组中

时间:2022-08-26 14:19:53

I need to put a condition when I create one element of my array

当我创建一个数组元素时,我需要设置一个条件

foreach ($score as $item):
     if ($item['subject_id'] == "3"){
         $file_data_array[] = array(
            "y" => $item['result'],
     ////Need condition here///////////////////////////////
            "color" => '#FFF'                
            );
      }
 endforeach;

So I need a condition like

所以我需要一个像这样的条件

if ($item['confirmed'] == 1) {  
    "color" => '#FFF'
} else {
    "color" => '#000'
}

So, since we cannot put an if inside an array, how can I do my condition?

所以,既然我们不能把一个if放在一个数组里面,我该怎么办?

4 个解决方案

#1


4  

Try and use the ternary if:

如果出现以下情况,请尝试使用三元组:

foreach ($score as $item):
     if($item['subject_id'] == "3"){
         $file_data_array[] = array(
            "y" => $item['result'],
            "color" => ($item['confirmed'] == 1 ? '#FFF': '#000')
        );
endforeach;

#2


3  

"color" => ($item['confirmed']==1 ? "#FFF" : "#000")

#3


2  

foreach ($score as $item):
 if($item['subject_id'] == "3"){
     if($item['confirmed'] == 1) {
         $color = '#FFF'; 
      } else {
         $color = '#000';
      }     
     $file_data_array[] = array(
        "y" => $item['result'],
        "color" => $color);
endforeach;

#4


0  

<?php
$login_detail=array(0=>array("sumit","8101"),1=>array("ashwani","9526"),2=>array("parmod","2592"),3=>array("jitendra","5792"),4=>array("umesh","5555"));
foreach($login_detail as $value)
{
    foreach($value as $value2 )
    {
        echo $value2." &nbsp;&nbsp;";
    }
    echo "<br>";
}
$username=$_POST['txtusername'];
$password=$_POST['txtpassword'];
if($username==$login_detail && $password==$value)
{
     header("location: dashbord.php");
}
else
{
    header("location: loginexample.php?msg=user name or password is wrong");
}

#1


4  

Try and use the ternary if:

如果出现以下情况,请尝试使用三元组:

foreach ($score as $item):
     if($item['subject_id'] == "3"){
         $file_data_array[] = array(
            "y" => $item['result'],
            "color" => ($item['confirmed'] == 1 ? '#FFF': '#000')
        );
endforeach;

#2


3  

"color" => ($item['confirmed']==1 ? "#FFF" : "#000")

#3


2  

foreach ($score as $item):
 if($item['subject_id'] == "3"){
     if($item['confirmed'] == 1) {
         $color = '#FFF'; 
      } else {
         $color = '#000';
      }     
     $file_data_array[] = array(
        "y" => $item['result'],
        "color" => $color);
endforeach;

#4


0  

<?php
$login_detail=array(0=>array("sumit","8101"),1=>array("ashwani","9526"),2=>array("parmod","2592"),3=>array("jitendra","5792"),4=>array("umesh","5555"));
foreach($login_detail as $value)
{
    foreach($value as $value2 )
    {
        echo $value2." &nbsp;&nbsp;";
    }
    echo "<br>";
}
$username=$_POST['txtusername'];
$password=$_POST['txtpassword'];
if($username==$login_detail && $password==$value)
{
     header("location: dashbord.php");
}
else
{
    header("location: loginexample.php?msg=user name or password is wrong");
}