dsuikgi9199 2012-06-12 19:38
浏览 186

PHP在For循环中创建关联数组

<?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    
?>

I am trying to assign each number in the first array to its associated letter, and then add the association to a spot in a new array value. Then I am trying to associate the value arrays to the facet_field arrays and add them to catalogue. It is having a problem with the assigned associations such as the line with $spot.

Is it possible to do this? Or can I not include this many variables in an association? How would I make an associated array in an array like this?

Thanks.

  • 写回答

1条回答 默认 最新

  • douyou2732 2012-07-10 17:13
    关注
    1. Is it possible to do this?

      Yes, it is possible to transform the array.

    2. Or can I not include this many variables in an association?

      No, that is not a problem. The $facet_files array is small and processable. Memory problems arise only, when arrays are really, really large.

    3. How would I make an associated array in an array like this?

      By iterating over the $facet_fields array, getting its keys and values and inserting them into a new array with the desired structure. I think you make this a bit complicated, by using for-loops and indexing the $values array with the iterator variable. When you insert new values into an array they are automatically indexed numerically. In other words: stop doing $facet_fields[$x].

      You might provide the $catalog = array( key => value ); because it's not really clear which structure the target array must have.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题