我注意到在Javascript中,在for..in循环中用作索引的变量将始终是一个string偶数,即使我按以下方式定义它:
var s_array = new Array();
s_array[0] = 'foo';
s_array[1] = 'bar';
for(i in s_array){
alert(typeof(i)); // String
}
Run Code Online (Sandbox Code Playgroud)
为什么它被认为是一个string而不是一个number?
我正在尝试理解这段代码:
<?php
$list = array(-10=>1, 2, 3, "first_name"=>"mike", 4, 5, 10=>-2.3);
print_r(array_keys($list));
?>
Run Code Online (Sandbox Code Playgroud)
输出:
Array ( [0] => -10 [1] => 0 [2] => 1 [3] => first_name [4] => 2 [5] => 3 [6] => 10 )
Run Code Online (Sandbox Code Playgroud)
我想知道为什么[4] => 2 and why [5] => 3我认为这是[4] => 4 and [5] => 5因为它们都在索引4和5.我对这个阵列到底发生了什么感到困惑,如果有可能,有人能指出我正确的方向,谢谢.
我在twig编码,以显示我从PHP获得的值,我希望数组的键名出现在值的上方.
{% if user.address %}
<tr>
{% for address in user.address %}
{% for parts in address %}
<td width="25%">
{{ parts }}
</td>
{%endfor%}
{% endfor %}
</tr>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
在{% for address in user.address %}我想要的部分 ({{address.key}}或获得数组的键名所需的真实句子)
数组如下:
-address : array:4 [?
"Door" => array:1 [?
0 => "225"
]
"Street" => array:1 [?
0 => "Pinky street"
]
"District" => array:1 [?
0 => "District north"
]
"City" => array:1 [?
0 => …Run Code Online (Sandbox Code Playgroud) 我希望能够使用数字键检索数组的值.问题是,如果密钥超出了数组长度,我需要它再次遍历数组.
$my_array = array('zero','one','two','three','four','five','six','seven');
function loopArrayValues($array,$key){
//this is what is needed to return
return
}
echo "Key 2 is ".loopArrayValues($my_array,2)."<br />";
echo "Key 11 is ".loopArrayValues($my_array,11)."<br />";
echo "Key 150 is ".loopArrayValues($my_array,11)."<br />";
Run Code Online (Sandbox Code Playgroud)
预期产量:
Key 2 is two
Key 11 is three
Key 150 is three
Run Code Online (Sandbox Code Playgroud)
我的研究参考:
我的形成功能:
function loopArrayValues($array,$key){
$infinate = new InfiniteIterator(new ArrayIterator($array));
foreach( new LimitIterator($infinate,1,$key) as $value){
$return=$value;
}
return $return;
}
Run Code Online (Sandbox Code Playgroud)
该功能有效,但我有一个问题:这是获得预期结果的好方法吗?
我有数组,并使用array_keys来获取键:
$arr = array( 1 => 1,
2 => 3,
3 => 2,
5 => 0,
6 => 0 );
$new_arr = array_keys($arr);
Run Code Online (Sandbox Code Playgroud)
现在,如果value不为零,我想获取array_keys.我怎样才能做到这一点?
请帮忙.
我想使用 获取多维数组中的值PHP。我将键传递给函数,如果键包含值(即没有任何数组值),它将返回该值。但如果该键包含一个数组值,它将返回整个子数组。
我正在为此添加一个示例数组。
<?php
// prepare a list of array for category, subcategory etc...
$category = array(
'Account' => array(
'Show Balance' => array(
'Recharge' => 2300,
'Success' => 12000,
'Failure' => 25000,
),
'Balance History' => 'your balance is very low for last 2 years',
'Mini Statement' => 'This is your mini statement. You can have a look of your transaction details',
'Last Transaction' => 25000
),
'Deposit' => array(
'Deposit Limit' => 40000,
'Make Deposit' => …Run Code Online (Sandbox Code Playgroud) $products = array(
'paper' => "Paper Section" => array
(
'copier' => "Copier and Multipurpose",
'inkjet' => "Inkjet Printer",
),
'pens' => "Pen Section" => array
(
'ball' => "Ballpoint Pens",
'hilite' => "Highlighters"
),
'misc' => "Miscellaneous Section" => array
(
'tape' => "Sticky Tape",
'glue' => "Adhesive"
)
);
echo "<pre>";
foreach ($products as $section => $items)
foreach ($items as $key => $value)
echo "$section:\t$key\t($value)<br />";
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
显然我在这里要做的是为$ section集分配索引,并且我在尝试这样做时遇到错误.有没有其他方法可以做到这一点,还是在PHP中是不可能的?
假设我们有两个数组:
$a=array('1'=>'Apple','2'=>'Microsoft',
'3'=>'Microapple','4'=>'Applesoft','5'=>'Softapple');
$b=array(1,3);
Run Code Online (Sandbox Code Playgroud)
其中$ b数组表示要区分的数组$ a的键.
我们希望使用以下值接收另一个数组$ c:
$c=array('2'=>'Microsoft','4'=>'Applesoft','5'=>'Softapple');
Run Code Online (Sandbox Code Playgroud)
在php手册中有两个功能:
array_diff($array1,$array2); //difference of values
array_diff_key($array1,$array2);//difference of keys
Run Code Online (Sandbox Code Playgroud)
但以上都不适用于此.
我们应该做什么?
编辑
谢谢大家的贡献.
我对预定义的两个数组执行了一些基准测试,如下所示
for ($i=0; $i < 10000; $i++) { //add 10000 values
$a[]=mt_rand(0, 1000000); //just some random number as a value
}
for ($i=0; $i < 10000; $i++) { //add 10000 values as keys of a
$b[]=mt_rand(0, 1000);
} //randomly from 0 to 1000 (eg does not cover all the range of keys)
Run Code Online (Sandbox Code Playgroud)
每次测试也进行了10000次,平均Nanne解决时间为:
0.013398
以下之一decereé …
我想以数字方式对数组进行排序,即188,188-1,188-2,222,222-1,222,-2等.这就是数组当前的样子:
[188-1] => Array
(
[time] => 1
)
[188-2] => Array
(
[time] => 2
)
[188-3] => Array
(
[time] => 3
)
[188] => Array
(
[notes] => frog stand notes
)
[489] => Array
(
[notes] => notes
)
[489-1] => Array
(
[weight] => 10
[reps] => 30
)
[489-2] => Array
(
[weight] => 20
[reps] => 30
)
[489-3] => Array
(
[weight] => 30
[reps] => 30
)
[492-1] => Array
( …Run Code Online (Sandbox Code Playgroud) 我需要根据一些值来选择和分组一些项目,并且使用关联的多维数组很容易:
$Groups = array(
"Value1" = array("Item1", "Item3"),
"Value2" = array("Item2", "Item4")
);
Run Code Online (Sandbox Code Playgroud)
但是有些项目没有价值,所以我的数组将是这样的:
$Groups = array(
"Value1" = array("Item1", "Item3"),
"Value2" = array("Item2", "Item4")
"" = array("Item5", "Item6")
);
Run Code Online (Sandbox Code Playgroud)
我已经对其进行了测试(也处于foreach循环中),而且一切似乎都可以正常工作,但我对php还是很陌生,我担心使用空键可能会给我带来意想不到的问题。
使用带有空键的关联数组是否有任何问题?
这是不好的做法吗?
如果是这样,我怎么能达到目标?
array-key ×10
php ×9
arrays ×5
extract ×1
for-in-loop ×1
indexing ×1
javascript ×1
ksort ×1
performance ×1
sub-array ×1
symfony ×1
twig ×1
typeof ×1