数组中的数组

时间:2012-03-23 11:38:15

标签: php arrays associative-array

我对关联数组中的关联数组有点挣扎。重点是我总是需要在数组中深入钻取,我只是没有做到这一点。

$array['sections']['items'][] = array (
      'ident'         =>  $item->attributes()->ident,
      'type'          =>  $questionType,
      'title'         =>  $item->attributes()->title,
      'objective'     =>  (string) $item->objectives->material->mattext,
      'question'      =>  (string) $item->presentation->material->mattext,
      'possibilities' =>  array (
          // is this even neccesary to tell an empty array will come here??
          //(string) $item->presentation->response_lid->render_choice->flow_label->response_label->attributes()->ident => (string) $item->presentation->response_lid->render_choice->flow_label->response_label->material->mattext
      )
);

foreach ($item->presentation->response_lid->render_choice->children() as $flow_label) {
    $array['sections']['items']['possibilities'][] = array (
        (string) $flow_label->response_label->attributes()->ident => (string) $flow_label->response_label->material->mattext
    );
}

所以'possibilities'=> array()包含一个数组,如果我在其中放置一个值,就像注释说明我得到了我需要的东西。但是一个数组包含多个值,所以我试图在位置上放置多个值 $ array ['sections'] ['items'] ['possibilities'] []

但是这会输出值存储在不同的级别。

...
[items] => Array
   (
      [0] => Array
         (
            [ident] => SimpleXMLElement Object
               (
                  [0] => QTIEDIT:SCQ:1000015312
               )
            [type] => SCQ
            ...
            [possibilities] => Array
               (
               )
         )

      [possibilities] => Array
         (
            [0] => Array
               (
                  [1000015317] => 500 bytes
               )
            [1] => Array 
               ...

我想要完成的是上面的foreach代码是第一个[possible] =>数组包含第二个信息。当然,第二个将消失。

4 个答案:

答案 0 :(得分:1)

您的$array['sections']['items']是一系列项目,因此您需要指定要添加可能性的项目:

$array['sections']['items'][$i]['possibilities'][]

其中$ i是循环中的计数器。

答案 1 :(得分:0)

现在您要将数组附加到[项目]。但是你想将它们附加到[items]的子元素:

你这样做:

$array['sections']['items']['possibilities'][] = ...

但它应该是这样的:

$array['sections']['items'][0]['possibilities'][] = ...

答案 2 :(得分:0)

我有:

import UIKit
import CoreData

class EditExpensesViewController: UIViewController {

    var send_array = [Expenses]() // Defined from the previous view controller

    override func viewDidLoad() {
        super.viewDidLoad()
        print(send_array)

    }
}

如何获取IsCheked值为true的每个SEC?

答案 3 :(得分:0)

$array['sections']['items']是一组项目,按照填充possibilities键的方式,每个项目都有自己的possibilities。因此,要访问正在循环的项目的possibilities,您需要按照第一个答案中的说明通过传递索引来指定$array['sections']['items']中的哪一个。 为了简化操作,您可以尝试

  1. 将项目数组(第一个=的RHS)保存到一个单独的变量中,而不是同时定义并附加到主数组中。

  2. 设置该变量的可能性。

  3. 将该变量附加到主$array['sections']['items']

相关问题