确定数组键值是否为空

时间:2016-04-12 09:35:52

标签: php arrays

我有一个如下所示的数组,因为你可以看到最后一个数组[adad]值是空白的,我怎么能写一个if语句来判断它是否为空。

Array
    (
        [K] => Array
            (
                [0] => mabel__chan
                [1] => mabel chan
            )

        [B] => Array
            (
                [0] => kieron br
            )

        [C] => Array
            (
                [0] => a br
                [1] => a
            )

        [adad] => Array
            (
                [0] => 
            )

    )

我尝试过这样做

if (count(array_filter($array)) == 0) {}

伪码

if(array[key] == blank) {
  echo "is blank";
} else {
  echo "isn't blank";
}

** PHP脚本这是我从mongoDB获取数据的方法* 当我使用回声时,下面的答案正常工作,但现在当我试图推入新阵列时,它已经破坏了某个地方,我再也没有数据了。

$col = "A" . $user->agencyID;
$db = $m->rules;
$collection = $db->$col;

$id = $_POST['ruleID'];

$search = array(
  '_id' => new MongoId($id)
);

$cursor = $collection->find($search);

$validTagsArray = array();
$validArray = array();

foreach ($cursor as $key => $value) {
  $temp = array_walk($array, function($v, $k) {
    if (count(array_filter($v)) === 0) {
      foreach ($value['AutoFix'] as $keyTwo => $valTwo) {
        $x = 0;
        $validTagsArray['data'][] = array($keyTwo, $x);
      }  
    } else {
      foreach ($value['AutoFix'] as $keyTwo => $valTwo) {
        $x = 0;
        foreach ($valTwo as $key => $value) {
          $x++;      
        }
        $validTagsArray['data'][] = array($keyTwo, $x);
      }            
    }
  });
}

echo json_encode($validTagsArray);

2 个答案:

答案 0 :(得分:2)

你可以试试这个 -

    dbHelper = new DBHelper(getApplicationContext());
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    Cursor cursor = db.rawQuery("select * from centuaryTbl where email='"+email+"'",null);
    if (cursor.moveToFirst())
    {
        do
        {
            String s1 = cursor.getString(cursor.getColumnIndex("s1"));
            String s2 = cursor.getString(cursor.getColumnIndex("s2"));
            String s3 = cursor.getString(cursor.getColumnIndex("s3"));


        }while (cursor.moveToNext());
    }

<强>输出

$array = array
(
    'K' => array('0' => 'mabel__chan','1' => 'mabel chan'),
    'B' => array('0' => 'kieron br'),
    'C' => array('0' => 'a br', '1' => 'a'),
    'adad' => array('0' => '')
);

$temp = array_walk($array, function($v, $k) {
    if(count(array_filter($v)) === 0) { // check the count of non-empty elements in the sub array
        echo $k . ' is empty';
    }
});

答案 1 :(得分:0)

您可以写为:

if([adad][0]== " ")
 {

 }

你也可以试试这个:

foreach($array as $key=>$value){
   if(empty($value))
     echo "empty";
   }