【发布时间】:2018-02-20 21:56:21
【问题描述】:
这些数组包含相同的键,就像关注主题一样简单(prop 是唯一的):
Check if associative array contains value, and retrieve key / position in array
<?php
$array = array(
array("prop" => "1", "content" => "text"),
array("prop" => "2", "content" => "text"),
array("prop" => "3", "content" => "text"),
array("prop" => "4", "content" => "text")
);
$found = current(array_filter($array, function($item) {
return isset($item['prop']) && 3 == $item['prop'];
}));
print_r($found);
我得到了prop 3:
Array
(
[prop] => 3
[content] => text
)
所以我想将$array 中的值替换为:
array("prop" => "3", "content" => "replaced text")
【问题讨论】:
-
什么?你想要什么结果?
-
array("prop" => "3", "content" => "replaced text") in $array
-
prop总是唯一的吗? -
yes 总是独一无二的
标签: php arrays associative-array