在PHP中foreach 语句可以同时用return 和 break么?
例如:
$arr = array(
'1' => array('name' => 'zhangsan','age' => 20,'sex' => 1),
'2' => array('name' => 'lisi','age' => 30,'sex' => 0),
'3' => array('name' => 'wangwu','age' => 25,'sex' => 0),
'4' => array('name' => 'liuliu','age' => 40,'sex' => 1)
);
shuffle($arr); //我想随机返回一女人的姓名和年龄(男:sex->1;女:sex->0)
foreach ($arr as $key => $value) {
if($value['sex'] == 1){
//此处return回数据
result[
'name' => $value['name'],
'age' => $value['age']
];
//如果我想在返回数据之后立即让循环终止还需要break吗?
break;
}
}
桃花长相依
慕田峪7331174
慕盖茨4494581
撒科打诨