我有一个数组,可能包含数字或关联键,或两者:
$x = array('a', 'b', 'c', 'foo' => 'bar', 'd', 'e');
print_r($x);
/*(
[0] => a
[1] => b
[2] => c
[foo] => bar
[3] => d
[4] => e
)*/
Run Code Online (Sandbox Code Playgroud)
我希望能够从数组中删除一个项目,重新编号非关联键以保持顺序:
$x = remove($x, "c");
print_r($x);
/* desired output:
(
[0] => a
[1] => b
[foo] => bar
[2] => d
[3] => e
)*/
Run Code Online (Sandbox Code Playgroud)
找到要删除的正确元素是没有问题的,这是问题的关键.unset不对键进行重新编号,并且array_splice对偏移量而不是键进行处理(即:从第一个示例中取出$ x,array_splice($x, 3, 1)将删除"bar"元素而不是"d"元素).
我正在开发一个项目,我需要存储一个由两个字符串键索引的数字矩阵.矩阵不是锯齿状的,即如果任何行都存在列密钥,那么它应该存在于所有行中.同样,如果任何列都存在行键,则它应存在于所有列中.
表达这一点的显而易见的方法是使用关联数组的关联数组,但这既笨拙又效率低,并且它不强制执行非锯齿状属性.是否有任何流行的编程语言提供了一种内置于语言中或作为标准库的一部分的关联矩阵?如果是这样,它们如何在API和实现级别上工作?我正在为这个项目使用Python和D,但是其他语言的例子仍然有用,因为我可以查看API并找出在Python或D中实现类似内容的最佳方法.
如果我有两个关联数组,那么对它们的值进行差异的最有效方法是什么?
例如,给定:
array1 = {
foreground: 'red',
shape: 'circle',
background: 'yellow'
};
array2 = {
foreground: 'red',
shape: 'square',
angle: '90',
background: 'yellow'
};
Run Code Online (Sandbox Code Playgroud)
我如何检查另一个,以便缺少 或 附加的项目是结果数组.在这种情况下,如果我想比较array2中的array1,它将返回:
array3 = {shape: 'circle'}
Run Code Online (Sandbox Code Playgroud)
虽然如果我在array1中比较array2,它将返回:
array3 = {shape: 'square', angle: '90'}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
在PHP中,当你有一个关联数组,例如:
$groups['paragraph'] = 3
$groups['line'] = 3
Run Code Online (Sandbox Code Playgroud)
当您不知道键的值时,访问数组的第一个或第二个元素的语法是什么?
是否有类似于C#LINQ语句的内容,您可以这样说:
$mostFrequentGroup = $groups->first()?
Run Code Online (Sandbox Code Playgroud)
要么
$mostFrequentGroup = $groups->getElementWithIndex(0)?
Run Code Online (Sandbox Code Playgroud)
或者我必须使用foreach语句并像在此代码示例的底部一样选择它们:
//should return "paragraph"
echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph'));
//should return "line"
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'date', 'date', 'line', 'line', 'line'));
//should return null
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'wholeNumber', 'paragraph', 'paragraph'));
//should return "wholeNumber"
echo getMostFrequentlyOccurringItem(array('wholeNumber', '', '', ''));
function getMostFrequentlyOccurringItem($items) {
//catch invalid entry
if($items == null) {
return null;
}
if(count($items) == 0) {
return null;
}
//sort
$groups = array_count_values($items);
arsort($groups);
//if there was a …Run Code Online (Sandbox Code Playgroud) 我是一位经验丰富的PHP开发人员,过渡到C#.目前我正在开发Windows窗体应用程序.
我在搜索中发现C#不像PHP那样以松散的方式支持关联数组.我在Dictionary上找到了关于"结构"的信息,它们似乎是类对象.
我遇到的麻烦不仅是关联数组,而是一个多维数组,我想用它来保持一系列循环中的多个计数.
应用程序正在读取文本日志文件,搜索预定义的字符串,在找到该字符串时拉出该行上的日期,并在该日期递增该字符串匹配的计数.
在PHP中,它会像这样简单:
// Initialize
$count_array[$string_date][$string_keyword] = 0;
...
// if string is found
$count_array[$string_date][$string_keyword] += 1;
...
// To ouput contents of array
foreach($count_array as $date -> $keyword_count_array) {
echo $date; // output date
foreach($keyword_count_array as $keyword -> $count) {
echo $keyword . ": " . $count;
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎更多地涉及C#(这不是一件坏事).我试过使用我在另一个类似问题上找到的建议,但我并没有真正遵循如何增加或迭代/输出内容:
// Initialize
var count_array = new Dictionary<string, Dictionary<string, int>>();
count_array = null;
...
// if string is found - I think the second reference is supposed to …Run Code Online (Sandbox Code Playgroud) 解决方案找到并投票
这是我的代码:
//go through each question
foreach($file_data as $value) {
//separate the string by pipes and place in variables
list($title, $content, $date_posted) = explode('|', $value);
//create an associative array for each input
$file_data_array['title'] = $title;
$file_data_array['content'] = $content;
$file_data_array['date_posted'] = $date_posted;
}
Run Code Online (Sandbox Code Playgroud)
会发生什么是assoc值不断被删除.有没有办法让值附加到数组?如果没有,我怎么能这样做?
我正在阅读:
标准C++库:Nicolai M. Jossuttis的教程和参考
当我打算以任何重要方式使用某些STL机制时,这是我的首选.无论如何,我很快就重读了关于std :: map和相关算法的章节,我注意到了一个我以前没想过的句子:
非常量映射为直接元素访问提供下标运算符.但是,下标运算符的索引不是元素的整数位置....等
为什么只能在关联数组中使用非常量映射?在这种情况下,提供只读语义似乎相当简单.我想如果您尝试使用不存在的键检索元素,则可能存在异常(正如您的常量所示,无法向地图添加新键/值).
我想了解这背后的理由,如果有人能说清楚的话:)谢谢!
我试图将声明为数组对象的类似数组的对象进行字符串化,并发现JSON.stringify在定义为数组对象时未正确处理类似数组的对象.
请参阅下面的更清晰, - > jsFiddle
var simpleArray = []; //note that it is defined as Array Object
alert(typeof simpleArray); // returns object -> Array Object
simpleArray ['test1'] = 'test 1';
simpleArray ['test2'] = 'test 2';
alert(JSON.stringify(simpleArray)); //returns []
Run Code Online (Sandbox Code Playgroud)
它工作正常,{"test1":"test 1","test2":"test 2"}当我改变时回来了
var simpleArray = [];到var simpleArray = {};.
有人可以提供一些亮点或一些参考,我可以阅读更多?
编辑:
问题:当typeof simpleArray = []和simpleArray = {}返回的对象时,为什么JSON.stringify {"test1":"test 1","test2":"test 2"}在两种情况下都无法返回?
我很想知道是否有某种方法可以使用关联数组来调用函数来声明参数.
例如,如果我有这个功能:
function test($hello, $world) {
echo $hello . $world;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法称之为做这样的事情?
call('test', array('hello' => 'First value', 'world' => 'Second value'));
Run Code Online (Sandbox Code Playgroud)
我熟悉使用call_user_func和call_user_func_array,但我正在寻找一些更通用的东西,当我不知道他们提前寻找什么参数时,我可以使用它来调用各种方法.
编辑:原因是为API前端创建单个界面.我接受JSON并将其转换为数组.所以,我想要调用不同的方法,并将JSON输入中的值传递给方法.由于我希望能够从这个界面调用各种不同的方法,我想要一种方法将参数传递给函数而不需要知道它们需要的顺序.我在想使用反射会得到结果我'我正在寻找.
我们了解到有许多不同的散列算法/函数,我很好奇javascript使用了哪一个(v8,如果实现很重要).