【发布时间】:2012-06-12 19:38:36
【问题描述】:
<?php
$facet_fields = array (
"dx-tx295" => array (
"T",1348,),
"dx-z1" => array (
"F",1,
"G",2,
"E",3,
"D",4,
"C",5,
"H",6,
"B",7,
"I",8,
"J",9,
"K",10,
"A",11,
"L",12,
"M",13,)
)
$catalogue = array();
$value = array();
for ($x=1;$x<count($facet_fields);$x++)
{
for($y=0;$y<count($facet_fields[$x]);$y+2)
{
$current = $facet_fields[$x];
$spot = ($current[$y] => $current[$y+1]);
array_push($value, $spot);
}
}
for($i=0;$i<count($facet_fields);$i++)
{
$spot2 = ($facet_fields[$i]=>$value);
array_push($catalogue, $spot2);
}
print $catalogue
print $value
?>
我正在尝试将第一个数组中的每个数字分配给其关联的字母,然后将关联添加到新数组值中的某个位置。然后我试图将值数组关联到 facet_field 数组并将它们添加到目录中。分配的关联有问题,例如 $spot 行。
可以这样做吗?或者我不能在关联中包含这么多变量吗?如何在这样的数组中创建关联数组?
谢谢。
【问题讨论】:
-
php.net/manual/en/language.types.array.php 你的数组声明有问题。
-
除了提供的代码和所需输出的示例之外,最好添加。
-
@SomeKittens:虽然我确实看到 OP 的数组声明可能不如它们可能的那么好(假设关联数组比数字索引数组更适合需求)我没有看到任何问题他们被宣布的方式。请赐教。
-
虽然它们在语法上是正确的,但看起来 Tom 试图做的是创建一个关联数组(它可能是数字)。目前他的数组包含字符串和整数对象。
-
@sberry 所需的输出将给出:dx-tx295 T 1348 dx-z1 F 1 G 2 E 3 ...等等这样每个值数组都关联到其在目录中的位置下,并且数组中的每个字母值后面都有一个关联的数字值。谢谢大家。
标签: php arrays for-loop associations push