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 - Fatal编程技术网

Php 循环遍历具有特殊条件的数组

Php 循环遍历具有特殊条件的数组,php,arrays,Php,Arrays,我想用if语句保存一些代码,所以我在这里做一个循环 如何在不创建新阵列的情况下执行以下操作?我想继续依靠第一个$arr 这是一个简单的例子,我的数组实际上比它大很多:)所以请记住,我必须检查firstname的项,然后做一些事情 $arr = ['man_firstname', 'man_lastname', 'woman_firstname', 'woman_lastname']; // just making sure the value from db is set // otherw

我想用if语句保存一些代码,所以我在这里做一个循环

如何在不创建新阵列的情况下执行以下操作?我想继续依靠第一个$arr

这是一个简单的例子,我的数组实际上比它大很多:)所以请记住,我必须检查firstname的项,然后做一些事情

$arr = ['man_firstname', 'man_lastname', 'woman_firstname', 'woman_lastname'];

// just making sure the value from db is set 
// otherwise set it to an empty string
foreach ($arr as $item) {
    isset($db[$item]) ? $$item = $db[$item] : $$item = '';
}

// with that loop I have now $man_firstname, $man_lastname, $woman_firstname
// and $woman_lastname  are all set to their db values or are empty

// now I want to echo both first name 
// and last name of the man and woman but ONLY WHEN THEIR FIRST NAMES IS NOT EMPTY

foreach (?? as ??) {
    if (!empty(firstname)) {
        // echo both first name and last name
    }
}

// desired results: echo "john doe" only if firstname of the man is set, 
// the same with "jenna smith" if her first name is not set then do 
// nothing with her array item```

如果我正确理解了你的问题,你会想要这样的东西:

$arr = ['john', 'doe', 'john', '', 'jenna', 'smith', '', 'smith'];

if (count($arr) % 2) {
   throw new Exception('Missing person name / surname !');
}

for ($i=0; $i < count($arr); $i+=2) {
   if ($arr[$i] && $arr[$i+1]) {
      echo "{$arr[$i]} {$arr[$i+1]}<br>";
   }
}
$arr=['john','doe','john','jenna','smith','smith'];
如果(计数($arr)%2){
抛出新异常(“失踪人员姓名!”);
}
对于($i=0;$i”;
}
}

顺便说一句,请记住数组中的名称+姓氏应该是偶数-否则您将不知道缺少了什么-名称或姓氏以及缺少的是什么人。这就是我在这种情况下添加异常生成的原因。

如果我正确理解了您的问题,您希望这样:

$arr = ['john', 'doe', 'john', '', 'jenna', 'smith', '', 'smith'];

if (count($arr) % 2) {
   throw new Exception('Missing person name / surname !');
}

for ($i=0; $i < count($arr); $i+=2) {
   if ($arr[$i] && $arr[$i+1]) {
      echo "{$arr[$i]} {$arr[$i+1]}<br>";
   }
}
$arr=['john','doe','john','jenna','smith','smith'];
如果(计数($arr)%2){
抛出新异常(“失踪人员姓名!”);
}
对于($i=0;$i”;
}
}

顺便说一句,请记住数组中的名称+姓氏应该是偶数-否则您将不知道缺少了什么-名称或姓氏以及缺少的是什么人。这就是我在这种情况下添加异常生成的原因。

向我们展示代码中使用的2个数组输入数据。还要添加预期结果。这将帮助我们了解您的问题并为您提供解决方案。对我来说,
array\u merge()
array\u combine()
以及
array\u filter()
array\u chunk()
将完成这项工作。这个人的名字和姓氏是否根据
$arr
项目中的
组合而成?我觉得你的第一个循环是错误的。您仅根据所需键的长度获取该数据显示您在代码中使用的2个数组输入数据。还要添加预期结果。这将帮助我们了解您的问题并为您提供解决方案。对我来说,
array\u merge()
array\u combine()
以及
array\u filter()
array\u chunk()
将完成这项工作。这个人的名字和姓氏是否根据
$arr
项目中的
组合而成?我觉得你的第一个循环是错误的。您仅根据所需密钥的长度获取该数据