让我们考虑以下数组:
$data = array(
'0' => array(
'id' => '0',
'guid' => '22dd39bf-f6d6-4283-b87c-370354a7c2dd',
'age' => '32',
'name' => 'Harriet Vazquez',
'gender' => 'female',
'email' => 'harrietvazquez@applica.com',
'tags' => array(
'0' => 'sit',
'1' => 'mollit',
'2' => 'cillum',
'3' => 'irure',
),
'friends' => array(
'0' => array(
'id' => '0',
'name' => 'Long Dejesus',
),
'1' => array(
'id' => '1',
'name' => 'Carrillo Hodge',
),
'2' => array(
'id' => '2',
'name' => 'Coffey Greene',
),
'3' => array(
'id' => '3',
'name' => 'Stephanie Chavez',
),
'4' => array(
'id' => '4',
'name' => 'Richmond Mitchell',
),
)
),
'1' => array(
'id' => '1',
'guid' => '3df3ae55-03f3-4d7d-9c70-c7010a100886',
'age' => '36',
'name' => 'David Lynch',
'gender' => 'male',
'email' => 'davidlynch@applica.com',
'tags' => array(
'0' => 'id',
'1' => 'ad',
'2' => 'labore',
'3' => 'ad',
'4' => 'veniam',
'5' => 'nulla',
),
'friends' => array(
'0' => array(
'id' => '0',
'name' => 'Diana Watts',
),
'1' => array(
'id' => '1',
'name' => 'Patty Crawford',
),
'2' => array(
'id' => '2',
'name' => 'Terrell Larson',
),
)
),
'2' => array(
'id' => '2',
'guid' => 'da2c9f3f-ac85-4dfd-a43c-e55e476596ca',
'age' => '25',
'name' => 'Hardin Murphy',
'gender' => 'male',
'email' => 'hardinmurphy@applica.com',
'tags' => array(
'0' => 'laborum',
'1' => 'labore',
'2' => 'dolor',
'3' => 'excepteur',
'4' => 'est',
),
'friends' => array(
'0' => array(
'id' => '0',
'name' => 'Mandy Roberts',
),
'1' => array(
'id' => '1',
'name' => 'Walker Young',
),
'2' => array(
'id' => '2',
'name' => 'Middleton Baldwin',
),
'3' => array(
'id' => '3',
'name' => 'Tillman Harmon',
),
)
)
);
现在让我们创建一个带有一些条件的循环
$accepted = array('age', 'name', 'email');
foreach ($data as $idx => $row)
{
foreach ($row as $key => $value)
{
if(!is_array($value) && in_array($key, $accepted))
{
var_dump($value) . PHP_EOL;
}
}
}
上面的代码将产生如下输出:
字符串(2)“32” string(15) "哈丽特·巴斯克斯" 字符串(26)“harrietvazquez@applica.com” 字符串(2)“36” 字符串(11)“大卫林奇” 字符串(22)“davidlynch@applica.com” 字符串(2)“25” string(13) “哈丁·墨菲” 字符串(24)“hardinmurphy@applica.com”
现在的问题是:如果我continue
在语句中添加语句,是否有任何差异(我的意思是性能) else
?
$accepted = array('age', 'name', 'email');
foreach ($data as $idx => $row)
{
foreach ($row as $key => $value)
{
if(!is_array($value) && in_array($key, $accepted))
{
var_dump($value) . PHP_EOL;
}
else
{
continue;
}
}
}