0
当我遍历给定的阵列,它突然将回到起点:混合阵列串
这开始于一个类方法:
$where = 'WHERE ';
array_walk($columns, array(&$this, 'walker_cols'), $where);
这是回调
function walker_cols($item, $key, &$where)
{
static $temp;
if (is_array($item))
{
$temp = $key;
array_walk($item, array(__CLASS__, __FUNCTION__), $where);
}
var_dump($item);
var_dump($where);
return $where .= $temp ? "{$temp} = {$item} AND " : "{$key} = {$item} AND ";
}
给定的数组:
$columns = array(
'assoc_key_a' => 'assoc_val_a'
,'assoc_key_b' => array(
0 => 'num_val_a'
,1 => 'num_val_b'
)
);
的所需输出:
WHERE assoc_key_a = assoc_val_a AND assoc_key_b = num_val_a AND assoc_key_b = num_val_b
现在从var_dump
我的结果如下:
input: "assoc_val_a"
output: "WHERE "
input: "num_val_a"
output: "WHERE assoc_key_a = assoc_val_a AND "
input: "num_val_b"
output: "WHERE assoc_key_a = assoc_val_a AND assoc_key_b = num_val_a AND "
input: {
[0] => "num_val_a"
[1] => "num_val_b"
}
output: "WHERE assoc_key_a = assoc_val_a AND "
如果有另一种方式来为所需的输出,我会高兴地走它。我已经试过用array_walk_recursive()
来做,但是使用那个功能,我无法得到assoc_key_b
,因为它直接跳到了子阵列中。
感谢您的任何帮助。 我很坚持。
注:这可能是因为你要问'如果(is_array($项目))继续;'第二次。当迭代数组递归时,它可以是你直接跳出去的,所以继续到目前为止是唯一的选择。 – kaiser