我有一个数组,可能包含数字或关联键,或两者:
$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"元素).
数据库结构:
id galleryId type file_name description
1 `artists_2010-01-15_7c1ec` `image` `band602.jpg` `Red Umbrella Promo`
2 `artists_2010-01-15_7c1ec` `image` `nov7.jpg` `CD Release Party`
3 `artists_2010-01-15_7c1ec` `video` `band.flv` `Presskit`
Run Code Online (Sandbox Code Playgroud)
我要为应用程序的一个部分提取图像,在另一个部分提取视频等.为每个部分制作多个mysql查询是否更好:
$query = mysql_query("SELECT * FROM galleries WHERE galleryId='$galleryId' && type='image');
Run Code Online (Sandbox Code Playgroud)
...或者我应该构建一个关联数组,只要我需要使用结果集,就一遍又一遍地遍历数组?
谢谢你的想法.
I have some Perl code (for performance analysis) first developed under Linux which now needs to be ported to the mainframe. Apparently REXX is the scripting language of choice on that platform but this Perl script relies heavily on associative arrays (basically arrays where the index is a string).
Is there a way that in REXX? How would I code up something like:
$arr{"Pax"} = "Diablo";
$arr{"Bob"} = "Dylan";
print $arr{"Pax"} . "\n";
if (defined $arr{"no"}) {
print "Yes\n";
} …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值不断被删除.有没有办法让值附加到数组?如果没有,我怎么能这样做?
javascript for关键字将遍历对象的所有属性.如果在循环体内修改了对象,会发生什么?
例如,以下代码是否正常?
for(var key in obj)
if (whatever(obj[key]))
delete obj[key];
Run Code Online (Sandbox Code Playgroud)
如果此代码以确定的方式工作,并且最好所有密钥obj都只测试一次,那就OK .相比之下,在.NET或Java中,类似的构造通常会抛出异常.
作为前言,我在Windows 7(64位)上运行Java版本6(更新33),使用clooj作为我的IDE.我没有尝试在任何其他系统中重现我的问题.我对Clojure很有经验,但对Java一点也不熟悉.
我试图解决的问题的全部内容很难描述,但它归结为:我想说我想制作一个带有一个参数的宏,一个关联映射,并返回一个元素的向量地图与他们的订单保存.
=>(defmacro vectorize-a-map
[associative-map]
(vec associative-map))
=>#'ns/vectorize-a-map
=>(vectorize-a-map {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8}
=>[[:a 1] [:b 2] [:c 3] [:d 4] [:e 5] [:f 6] [:g 7] [:h 8]]
Run Code Online (Sandbox Code Playgroud)
这是有效的,但添加另一个元素到地图和订单搞砸...
=>(vectorize-a-map {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9}
=>[[:a 1] [:c 3] [:b 2] [:f 6] [:g 7] [:d 4] [:e 5] [:i 9] …Run Code Online (Sandbox Code Playgroud) 我有一个关联数组的变量列表,我想迭代并检索它们的键/值对.
我通过列出其所有键并获取值来迭代单个关联数组,即.
for key in "${!queue1[@]}" do
echo "key : $key"
echo "value : ${queue1[$key]}"
done
Run Code Online (Sandbox Code Playgroud)
棘手的部分是关联数组的名称是变量变量,例如,给定count = 5,关联数组将被命名为queue1,queue2,queue3,queue4,queue5.
我试图根据计数替换上面的序列,但到目前为止,括号和eval的每个组合都没有产生更多的错误替换错误.例如下面:
for count in {1,2,3,4,5} do
for key in "${!queue${count}[@]}" do
echo "key : $key"
echo "value : ${queue${count}[$key]}"
done
done
Run Code Online (Sandbox Code Playgroud)
非常感谢帮助!
问题:我无法在while循环中更新数组.插图(不是实际问题):
declare -A wordcounts
wordcounts["sentinel"]=1000
ls *.txt | while read f; do
# assume that that loop runs multiple times
wordcounts[$f]=$(wc -w $f)
echo ${wordcounts[$f]} # this prints actual data
done
echo ${!wordcounts[@]} # only prints 'sentinel'
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为管道在子shell中运行后循环.循环对变量所做的所有更改wordcounts仅在循环内可见.
说export wordcounts没有用.
唉,我似乎需要管道和while read部件,所以重写上面的代码的方法for并不是我想要的.
有没有合法的方法来更新循环中的关联数组形式,或者一般的子shell?
我有一个文件,将数字分配给md5sums,如下所示:
0 0000001732816557DE23435780915F75
1 00000035552C6F8B9E7D70F1E4E8D500
2 00000051D63FACEF571C09D98659DC55
3 0000006D7695939200D57D3FBC30D46C
4 0000006E501F5CBD4DB56CA48634A935
5 00000090B9750D99297911A0496B5134
6 000000B5AEA2C9EA7CC155F6EBCEF97F
7 00000100AD8A7F039E8F48425D9CB389
8 0000011ADE49679AEC057E07A53208C1
Run Code Online (Sandbox Code Playgroud)
另一个文件在每行中包含三个md5sums,如下所示:
00000035552C6F8B9E7D70F1E4E8D500 276EC96E149571F8A27F4417D7C6BC20 9CFEFED8FB9497BAA5CD519D7D2BB5D7
00000035552C6F8B9E7D70F1E4E8D500 44E48C092AADA3B171CE899FFC6943A8 1B757742E1BF2AA5DB6890E5E338F857
Run Code Online (Sandbox Code Playgroud)
我想要的是用第一个文件的整数替换第二个文件中的第一个和第三个md5sums.目前我正在尝试以下awk脚本:
awk '{OFS="\t"}FNR==NR{map[$2]=$1;next}
{print map[$1],$2,map[$3]}' mapping.txt relation.txt
Run Code Online (Sandbox Code Playgroud)
问题是尽管第一个文件在硬盘驱动器上仅为5.7g,但脚本需要更多的16g内存.
我有一组按来源分类的表格,例如:"客户","股票","Floorstock".它们看起来像这样:
每个都包含价格列表,需要在表格的最后一行添加和总计.
我在将"总计"数组链接到"表"数组时遇到问题 - 特别是在每个表的末尾显示每个总数
到目前为止,每个表的总数是在另一个函数中生成的,其中sourceTotals是一个实例数组:
public function setSourceTotalsArray($data)
{
$this->sourceTotals = $data["source_totals"];
return $this->sourceTotals;
}
Run Code Online (Sandbox Code Playgroud)
这将返回一个如下所示的数组:
array(4) { ["Floorstock"]=> int(0) ["Stock"]=> int(0) ["Client"]=> float(32.18) }
Run Code Online (Sandbox Code Playgroud)
这是我定义$pdf对象的地方:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$data = json_decode($_POST["printData"],true);
$pdf = new PDF();
// Column headings
$month = $pdf->getMonth($data['month']);
$year = $data['year'];
$pdf->SetFont('Arial','',10);
$pdf->AddPage();
$pdf>title(array('month'=>$month,'year'=>$year,'showroom'=>ucfirst($data['showroom_name'])));
$pdf->tableBody($data);
if(!empty($data["order_detail"])){
$customerOrders = array();
$stockOrders = array();
$floorstockOrders = array();
$otherOrders = array();
foreach($data["order_detail"] as $o){
switch($o["orderSource"]){
case 'Client':
$customerOrders[] = $o;
break;
case 'Stock':
$stockOrders[] …Run Code Online (Sandbox Code Playgroud)