替代同时通过键和值通过参考: [英] Alternatives to Pass both Key and Value By Reference:

查看:192
本文介绍了替代同时通过键和值通过参考:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能向我解释为什么你不能传递一个重要的参考?

例如:

 如果(is_array($哪里)){
    的foreach($其中,为&放大器; $关键=>&安培; $值){
        $键=秒($键);
        $值=秒($值);
    }
    未设置($键,$值);
}

抛出:

 致命错误:关键要素不能上线2 linkstest.php参考

我才可以使用array_map类似的东西?
所有我想要做的是遍历一个关联数组,和逃避都与我的秒()函数键和值。

阵图是我很难理解:

我曾尝试与array_map很多东西,但我不能让它作用于键直接。

我会得到任何性能优势利用阵图比只用foreach循环?

我不喜欢的foreach的是,我不能采取行动在阵列上直接,必须处理创建临时数组和取消它们:

 的foreach($其中,为$关键=> $值){
 $其中,[安全($键)] =安全($值);
}

如果发现某事在关键逃脱这可能会失败,加入了新的元素,并保持转义之一。

所以我是坚持了这样的事?

  $ temparr =阵列();
的foreach($其中,为$关键=> $值){
 $ temparr [安全($键)] =安全($值);
}
其中,$ = $ temparr;
未设置($ temparr);

任何替代方案?


解决方案

  

有人能向我解释为什么你不能传递一个重要的参考?


由于语言不支持此。你会pssed找到大多数语言这种能力的硬$ P $,因此,长期的


  

所以我是坚持了这样的事?


是的。最好的方法是创建适当的键一个新的数组。


  

任何替代方案?


提供更好的替代品的唯一办法就是知道你的具体情况。如果您的键映射到表的列名,那么最好的方法就是离开键作为是和他们在SQL的使用时间躲避他们的。

Can someone explain to me why you can't pass a key as reference?

Ex:

if(is_array($where)){
    foreach($where as &$key => &$value){
        $key = sec($key);
        $value = sec($value);
    }
    unset($key, $value);
}

Throws:

Fatal error: Key element cannot be a reference in linkstest.php on line 2   

Can I do something similar using array_map? All I want to do is iterate over an associative array, and escape both the key and value with my sec() function.

Array map is difficult for me to understand:

I have tried many things with array_map, but I can't get it to act on the keys directly.

Would I get any performance benefit using array map than just using a foreach loop?

What I don't like about foreach is that I can't act on the array directly, and have to deal with creating temporary arrays and unsetting them:

foreach($where as $key => $value){
 $where[secure($key)] = secure($value);
}

This might fail if it finds something to escape in the key, adding a new element, and keeping the unescaped one.

So am I stuck with something like this?

$temparr = array();
foreach($where as $key => $value){
 $temparr[secure($key)] = secure($value);
}
$where = $temparr;
unset($temparr);

Any alternatives?

解决方案

Can someone explain to me why you can't pass a key as reference?

Because the language does not support this. You'd be hard-pressed to find this ability in most languages, hence the term key.

So am I stuck with something like this?

Yes. The best way is to create a new array with the appropriate keys.

Any alternatives?

The only way to provide better alternatives is to know your specific situation. If your keys map to table column names, then the best approach is to leave the keys as is and escape them at their time of use in your SQL.

这篇关于替代同时通过键和值通过参考:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆