Php 向数组中添加元素
我有一个如下所示的数组:Php 向数组中添加元素,php,multidimensional-array,Php,Multidimensional Array,我有一个如下所示的数组: array 0 => array 'title' => string 'Ireland - Wikipedia, the free encyclopedia' 'url' => string 'http://en.wikipedia.org/wiki/Ireland' 1 => array 'title' => string 'Ireland's home for accomm
array
0 =>
array
'title' => string 'Ireland - Wikipedia, the free encyclopedia'
'url' => string 'http://en.wikipedia.org/wiki/Ireland'
1 =>
array
'title' => string 'Ireland's home for accommodation, activities.'
'url' => string 'http://www.ireland.com/'
array_walk( $result, function( &$el) { $el['score'] = 0; });
我想给每个元素加上0分。我原以为这个简单的foreach循环可以达到目的,但……嗯……它没有:/
public function setScore($result)
{
foreach($result as $key)
{
$key = array('title', 'url', 'score' => 0);
}
return $result;
}
有人能帮我吗
谢谢foreach使用阵列的副本。您可以随心所欲地修改$key,它不会反映在原始数组上
不过,您可以通过引用使用$key,然后它将按预期工作:
foreach ($result as &$value) {
$value['score'] = 0;
}
手动输入:foreach处理数组的副本。您可以随心所欲地修改$key,它不会反映在原始数组上
不过,您可以通过引用使用$key,然后它将按预期工作:
foreach ($result as &$value) {
$value['score'] = 0;
}
手动输入:您在此处创建一个新数组,但不进行任何操作:
foreach($result as $key){
$key = array('title', 'url', 'score' => 0);
}
您要做的是修改对现有引用的引用:
foreach($result as &$key){ # Note the '&' here
$key['score'] = 0;
}
您可以在此处创建一个新阵列,但不进行任何操作:
foreach($result as $key){
$key = array('title', 'url', 'score' => 0);
}
您要做的是修改对现有引用的引用:
foreach($result as &$key){ # Note the '&' here
$key['score'] = 0;
}
虽然deceze是正确的,但您也可以使用以下方法来完成此操作:
array
0 =>
array
'title' => string 'Ireland - Wikipedia, the free encyclopedia'
'url' => string 'http://en.wikipedia.org/wiki/Ireland'
1 =>
array
'title' => string 'Ireland's home for accommodation, activities.'
'url' => string 'http://www.ireland.com/'
array_walk( $result, function( &$el) { $el['score'] = 0; });
虽然deceze是正确的,但您也可以使用以下方法来完成此操作:
array
0 =>
array
'title' => string 'Ireland - Wikipedia, the free encyclopedia'
'url' => string 'http://en.wikipedia.org/wiki/Ireland'
1 =>
array
'title' => string 'Ireland's home for accommodation, activities.'
'url' => string 'http://www.ireland.com/'
array_walk( $result, function( &$el) { $el['score'] = 0; });
下面是一个如何实现这一点的示例
$array = array( array( 'title' => "Ireland - Wikipedia, the free encyclopedia", 'url' => "http://en.wikipedia.org/wiki/Ireland"), array( 'title' => "Ireland's home for accommodation, activities.", 'url' => "http://www.ireland.com/" ) );
function setScore( $result )
{
foreach( $result as &$element )
{
$element['score'] = 0;
}
return $result;
}
$array = setScore( $array );
print_r( $array );
你也可以这样做:
function setScore( &$result )
{...}
然后就是:
setScore( $array );
下面是一个如何实现这一点的示例
$array = array( array( 'title' => "Ireland - Wikipedia, the free encyclopedia", 'url' => "http://en.wikipedia.org/wiki/Ireland"), array( 'title' => "Ireland's home for accommodation, activities.", 'url' => "http://www.ireland.com/" ) );
function setScore( $result )
{
foreach( $result as &$element )
{
$element['score'] = 0;
}
return $result;
}
$array = setScore( $array );
print_r( $array );
你也可以这样做:
function setScore( &$result )
{...}
然后就是:
setScore( $array );
您正在更改$key不好,或者如果您确实必须这样做,那么请使用引用,即我认为您不希望使用的&$key,您可能需要$res=array并使用$res[]=array。。。。然后打印_r$res如果您正在更改$key不好,或者如果您真的必须这样做,那么请使用引用,即我认为您不希望使用的$key,您可能需要$res=array并使用$res[]=array。。。。然后打印\r$rest这个答案可能有点混乱,因为不能通过引用访问数组键。实际上,您正在访问数组值–名为$key。foreach$result作为$key=>&$value的答案可能有点混乱,因为不能通过引用访问数组键。实际上,您正在访问数组值–名为$key。foreach$result as$key=>&$value非常感谢你们的帮助,我想我现在对foreach循环的理解更好了。非常感谢你们的帮助,我想我现在对foreach循环的理解更好了。