所以我有两个数组如下:
$arr1 = array("apple","banana","lemon");
$arr2 = array("apple"=>4,"banana"=>1,"lemon"=>1);
我使用array_fill_keys来填充上面的数组:
$arr3 = array_fill_keys($arr1,1);
$arr4 = array_fill_keys($arr2,1);
当我为上面的4个数组执行array_sum时:
echo "Sum of Array1 is " . array_sum($arr1) . "\n";
echo "Sum of Array2 is " . array_sum($arr2) . "\n";
echo "Sum of Array3 is " . array_sum($arr3) . "\n";
echo "Sum of Array4 is " . array_sum($arr4) . "\n";
结果将是:
Sum of Array1 is 0
Sum of Array2 is 6
Sum of Array3 is 3
Sum of Array4 is 2
为什么Array4返回2而不是3?
答案 0 :(得分:1)
让我们在这里简化一下你的数组
$arr1 = array("apple","banana","lemon");
$arr2 = array("apple"=>4,"banana"=>1,"lemon"=>1);
//$arr3 = array_fill_keys($arr1,1);
$arr3 = array("apple"=>1,"banana"=>1,"lemon"=>1);
//$arr4 = array_fill_keys($arr2,1);
$arr4 = array(1=>1, 4=>1);
正如您所见,数组#4($ arr4)使用array_fill_keys添加了这些键
array( 4, 1, 1 );
这些是$ arr2的值,您的输入值是
1
产生这个数组
array(
4=>1,
1=>1,
1=>1
);
现在你可能知道php数组键是唯一的,所以第一个(1)键被第二个(1)键覆盖,导致这个
$arr4 = array_sum(array(1=>1, 4=>1));
http://php.net/manual/en/function.array-fill-keys.php
因此总和为2
答案 1 :(得分:0)
原因是因为<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li><span><a href="category.html">Shop</a> <i class="icon-expand_more"></i></span>
<ul class="active">
<li>
<span><a href="category.html">Sleeping Bags</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="category.html">Medium Sleeping Bags</a></li>
<li><a href="category.html">Winter Sleeping Bags</a></li>
<li><a href="category.html">Summer Sleeping Bags</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Swaddles</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Linen</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Clothing</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">My Room</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Tool Box</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Wellbeing</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Feed</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Bath</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Carriers</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
<li>
<span><a href="category.html">Gifts</a> <i class="icon-expand_more"></i></span>
<ul class="menu">
<li><a href="/shop/wraps/fitted-wraps">Fitted Wraps</a></li>
<li><a href="/shop/wraps/merino-wraps">Merino Wraps</a></li>
<li><a href="/shop/wraps/muslin-wraps">Muslin Wraps</a></li>
<li><a href="/shop/wraps/stretch-cotton">Stretch Cotton</a></li>
</ul>
</li>
</ul>
</li>
<li><span><a href="category.html">Brands</a></span></li>
</ul>
将使用值4,1和1以及键并将其值设置为1.
array_fill_keys
将成为
$arr2 = array("apple" => 4, "banana" => 1, "lemon"=> 1);
两个键为1将取消,因此数组中只剩下两个值。然后,PHP将使用提供的值替换所有值,即一个。
您的最终数组将如下所示
$arr2 = array(4 => "apple", 1 => "banana", 1 => "lemon");
$arr2 = array(4 => 1, 1 => 1);
,在数组中添加值,即array_sum
。
答案 2 :(得分:0)
array_fill_keys()被赋予一个键数组,并使用这些键和定义的值生成一个新数组。 $ arr2包含两个相同的值&#34; 1&#34;这意味着当你使用它来生成$ arr4时,只会创建两个值。
如果您需要使用第一个数组中的键,您应该考虑使用array_keys来使用键,而不是值:
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);
$arr3 = array_fill_keys($keys1, 1);
$arr4 = array_fill_keys($keys2, 1);
这样(arr1和arr3)和(arr2和arr4)的键将匹配。
享受。