2

PHPで2D配列を定義しようとしています。状況を確認できるように、いくつかのコンセプトコードがあります。

    class Testing { 
        protected $requiredFieldsByReferenceType = array(
           ['Book']['volume'] => true,
           ['Book']['source'] => true,
           ['Book Section']['volume'] => true,
           ['Book Section']['source'] => true,
           ['Chart or Table']['volume'] => true,
           ['Chart or Table']['source'] => true
        );
        print_r($requiredFieldsByReferenceType);
     }//End Testing

スローされるエラー:

解析エラー:構文エラー、予期しない'['、予期する')'

4

11 に答える 11

5

他の答えは良いです。
array() を使用した構文は次のとおりです。

$requiredFieldsByReferenceType = array('Book'=>array('volume' => true,
                                                     'source' => true),
                                       'Book Section'=>array('volume' => true,
                                                             'source' => true)
                                       );
于 2009-06-05T02:35:45.133 に答える
5

array()配列値の宣言内でも使用する必要があります。

protected $myArray = array(
    "Book" => array(
        "item1" => true,
        "item2" => true
    ),
    "Chest" => array(
        "item1" => true,
        "item2" => false
    )
);
于 2009-06-06T16:00:23.757 に答える
1
$requiredFieldsByReferenceType ['Book']['volume'] = true;
$requiredFieldsByReferenceType ['Book']['source'] = true;
$requiredFieldsByReferenceType ['Book Section']['volume'] = true;
于 2009-06-05T02:28:08.940 に答える
1

1 つのステートメントで配列を割り当てる回答のみが、コンストラクターにすべてを配置しない限り、コンテキスト (クラス プロパティの定義) で機能します。同様に、print_r がメソッドに含まれていないと機能しないと思います...

于 2009-06-05T03:40:25.013 に答える
1
$arr1 = array(
    array(value1,value2,value3),
    array(value4,value5,value6),
    array(value7,value8,value9),
);
于 2012-04-20T09:37:27.363 に答える
0

それを行う別の方法は、array() 関数をネストすることです。

 $requiredFieldsByReferenceType = array(
           'Book' => array('volume' => true,
                                       'source' => true),
           'Book Section' => array('volume' => true,
                                                     'source' => true),
         ...
        );
于 2009-06-05T02:36:42.823 に答える
0

PHP は多次元配列を処理しません。配列の配列として構築する必要があります。

protected $myArray = array(
  'Book' => array(
    'item1' => true,
    'item2' => true,
  ),
  'Chest' => array(
  ),
    'item1' => true,
    'item2' => false,
);
于 2009-06-06T16:01:53.787 に答える
0
Class TestClass {
    protected $myArray = array(
        "Book" => array('item1' => true, 'item2' => false),
        "chest" => array('item1' => true, 'item2' => false),
    );
}
于 2009-06-06T16:02:50.917 に答える
0

配列を定義する構文はarray(key0 => value0, key1 => value1, key2 => value2, ...). PHP の 2 次元配列は、配列内の値としての複数の配列であるため、次のようになります。

$myArray =
    array(
        'Book' =>
            array(
                'item1' => true,
                'item2' => true
            ),
        'Chest' =>
            array(
                'item1' => true,
                'item2' => false
            )
    );
于 2009-06-06T16:04:08.360 に答える
0

代わりにこれを行います:

    $myArray = array(
            'book' => array(
                'item1' => true,
                'item2' => true
            ),
            'chest' => array(
                'item1' => true,
                'item2' => true,
            )
        );

ちなみに、このように属性を初期化するべきではありません。むしろ、ゲッター/セッターを使用してください。

class TestClass {
    protected $_myArray;

    public function __construct()
    {
        $this->setMyArray();
    }

    public function setMyArray()
    {
        $this->_myArray = array(
            'book' => array(
                'item1' => true,
                'item2' => true
            ),
            'chest' => array(
                'item1' => true,
                'item2' => true,
            )
        );
    }
}


$foo = new TestClass();
print_r($foo);
于 2009-06-06T16:04:14.147 に答える
0

ここで配列に追加するスタイルを混在させているようです。

試す

$arr = array(
    'key' => array ('key2' => 'value 1', 'key3' => 'value2'),
    'key12' => array('key4' => 'value4')
);

また

$arr = array();

$arr['key1'] = array();
$arr['key2'] = array();

$arr['key1']['key3'] = 'value1';

(私の例は同じデータ構造を生成しないことに注意してください。異なる方法を示しただけです)

于 2009-06-06T16:04:21.683 に答える