我有一些这样的代码:
Dim col As Collection = New Collection
col.Add(value1, "key1")
col.Add(value2, "key2")
' later...
For Each item As String In col
' want to get valueX and keyX here; currently, "item" holds the value
Next
Run Code Online (Sandbox Code Playgroud)
如何在循环中获取值和键?也许还有另外一门课可以简化这一过程?
我有一个关联数组,在迭代这个数组时,使用foreach循环.Flex正在失去订单.这太烦人了.
为什么会这样?
我怎么能避免这个?
有什么方法在PHP中我可以获得PHP中的多维数组的n级密钥?
这是我的关联数组作为输出我想这将包含所有键的值的数组object_id,你可以有从结构本身也有许多嵌套的水平object_id,所以我怎么能得到的所有值的键object_id?
array
'cart' =>
array
12061 =>
array
'object_id' => string '12061' (length=5)
'discriminator' => string 'SimpleProductOffering' (length=21)
'spec' =>
array
100012061 =>
array
'object_id' => string '100012061' (length=9)
'discriminator' => string 'CompositeProductSpecification' (length=29)
'trait' =>
array
'MAIN_CPE' =>
array
'object_id' => string '1000000000015' (length=13)
'is_configurable' => string '0' (length=1)
'trait_value' =>
array
10001 =>
array
'object_id' => string '10001' (length=5)
'collateral' =>
array
empty
'collateral' =>
array
empty
'offer_type' => null
'price' …Run Code Online (Sandbox Code Playgroud) 假设我有一个关联数组:
$array = array(
"key1" => "value",
"key2" => "value2");
Run Code Online (Sandbox Code Playgroud)
而且我想把键全部大写.我怎么做比一般化的方式(意思是我可以应用用户定义的函数来应用键名)?
我有这个php关联数组.
array(
'Location_1' => 'Link_1',
'Location_2' => 'Link_2'
)
Run Code Online (Sandbox Code Playgroud)
我想使用看起来像这样的json_encode()将它转换为json输出;
[{"Location_name":"Location_1","Link_name":"Link_1"},{"Location_name":"Location_2","Link_name":"Link_2"}]
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?对我来说,具有挑战性的部分似乎是如何在前面添加Location_name和Link_name.非常感谢你.
我正在编写我的第一个D程序,并试图了解如何实现关联数组.不断出现的问题是,如果我创建一个像这样的数组:
import std.stdio;
import std.string;
import std.array;
void main(string[] args) {
int[string] arr = ["first" : 1, "second" : 2];
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.但如果我尝试将arr移动到main之外 - 进入结构体,我会收到错误说:错误:非常量表达式.
这会引发错误:
import std.stdio;
import std.string;
import std.array;
struct foo {
int[string] arr = ["first" : 1, "second" : 2];
}
void main(string[] args)
{ /* do stuff with foo */ }
Run Code Online (Sandbox Code Playgroud)
我确定这是一个非常简单的修复,但这是我第一次尝试D.
假设我有一个这样的数组:
$people = array(
array(
'name' => 'Bob',
'job' => 'Carpenter'
),
array(
'name' => 'George',
'job' => 'Programmer'
),
array(
'name' => 'Clint',
'job' => 'Actor'
)
);
Run Code Online (Sandbox Code Playgroud)
我想知道所有这些人的名字.
我知道我可以这样做:
$names = array();
foreach($people as $person)
$names[] = $person['name'];
Run Code Online (Sandbox Code Playgroud)
但是,让我说我很懒,所以我为它创建了一个函数:
/**
* Returns an Array of Values paired with the specified $key
* in the $array of Associative Arras.
*
* @param array $array The Array of Associative Arrays.
* @param string $key The Key within each Associative Array …Run Code Online (Sandbox Code Playgroud) 我有阵列:
array(2=>'0',3=>'1',5=>'3',4=>'4',7=>'2')
Run Code Online (Sandbox Code Playgroud)
注意:似乎没有任何顺序.因此,请保留计划以查找数字顺序.
如何修复此关联数组的第i个元素?
例如,当i是4该值应为4,当i是1该值应0.
array_shift()从来没有解决这个要求.很清楚循环逻辑解决了这个问题.
假设我有一个Articles系列,并且想获取热门文章。
因此,首先,我在scopePopular()方法中创建一个Article方法。很简单的。
现在,我想获取他们id的,因此我将这样做:
Article::popular->pluck('id');
Run Code Online (Sandbox Code Playgroud)
结果将是一个关联数组:
[
0 => 1,
1 => 34,
2 => 17
...
];
Run Code Online (Sandbox Code Playgroud)
我想要一个没有键的普通数组,例如:
[1, 34, 17]
Run Code Online (Sandbox Code Playgroud)
我知道我可以做这样的事情:
array_values(Article::popular->pluck('id'));
Run Code Online (Sandbox Code Playgroud)
但是我相信Laravel有更清洁的方法来做到这一点。有任何想法吗?
我想改变我的阵列,我怎样才能做出这种改变.
Array (
[0] => 53720
[1] => Array(
['Build Quality'] => 1=>10,
2=>9,
3=>7
['Versatality'] => 1=>9,
2=>8,
3=>7
['value'] => 1=>8,
2=>6,
3=>5
)
);
至:
Array (
53720 =>['Build Quality' => [1=>10,
2=>9,
3=>7],
'Versatality' => [1=>9,
2=>8,
3=>7],
'value' => [1=>8,
2=>6,
3=>5]
]
);
function get_array(){
$factor = array([0] => 'Build Quality' [1] => 'Versatality' [2] => 'Value');
$rank = array([0] => 1=>10,2=>9,3=>7 [1] => 1=>9,2=>8,3=>7 [2] => 1=>8,2=>6,3=>5);
$assoc_array = array_combine($factor, $rank);
$post_id = …Run Code Online (Sandbox Code Playgroud)