我有一个数组:
Array
(
[0] => Array
(
[id] => 31299
[name] => 37322426212
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322426212
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[1] => Array
(
[id] => 4373
[name] => 37322983029
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322983029
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[2] => Array
(
[auth_type] => ani
[name] => 37322983029
[ani] => 37322983029
[orig_enabled] => Array
(
[0] => on
)
[orig_rate_table] => 7
)
)
0,1,2的项目可能更多(3,4 ...... 10 ......等等)。
我要做的是找到包含键ani
和name = 37322983029
的数组并替换
name
使用'###'和ani
清空:
[mane]=>"###",//where name = '37322983029'
[ani]=> //where ani = '37322983029'
我尝试str_replace
但没有成功。
怎么办呢?
答案 0 :(得分:0)
不确定您使用str_replace
的方式,但以下内容应该有效(假设ani
和mane
(或name
?)实际上是数组中的字符串索引 - 从你的例子中不明显):
foreach($myArray as &$val) {
if($val['ani'] == 37322983029) {
$val['ani'] = '';
$val['name'] = "###";
}
}
重要请注意使用&
通过引用传递值,这样您就可以使用数组的实际元素,而不是复制...这样,当您修改{时{1}}您实际上正在修改原始数组。没有&符号,循环中初始数组将保持不变。
另请注意 - 您的示例中不清楚您是仅要测试$val
还是ani==37322983029
。我相信您可以修改上面的代码以同时具备这两个条件(或参见Alexxus的答案)
完整的代码示例(已测试,正在运行):
name=37322983029
生成输出
<?php
$a = Array(Array('a'=>4, 'b'=>5),Array('a'=>6, 'b'=>7));
echo '<pre>';
print_r($a);
foreach($a as &$v){
if($v['a']==6) {
$v['a'] = 12;
$v['b'] = '';
}
}
print_r($a);
echo '</pre>';
?>
答案 1 :(得分:0)
我想,这就是你要找的东西:
foreach($yourArray as &$a)
{
if($a["ani"] == 37322983029 and $a["ani"] == $a["name"])
{
$a["name"] = "###";
$a["ani"] = "";
}
}