2017-10-23 2 views
0

配列/多次元配列を扱うときに非常に混乱しています。私は基本のものを知っているが、私は、次の例を動作させることはできません。配列を変更する多次元symfony

"hello":{ 
    "id":100, 
    "items":[ 
    { 
     "a":3, 
     "b":1, 
     "c":2 
    } 
    ] 
} 

そして、私のコード:

public function createHello() 
{ 
    $collection = $this->greetings->getItems(); 

    $hello = [ 
     'id'=> $this->getId(), 
     'items' => [ 
     ] 
    ]; 

    foreach ($collection as $col) { 
     $hello['items'][]['a'] = $this->getValue($col); 
    } 

    return $hello; 
} 

そして、これが表示されているものです。

hello:{ 
"id": 100, 
"items": [ 
    [], 
    { 
     "a": 3 
    } 
    ] 
} 

解決する方法この?私は配列の値と定義を引き続き変更していますが、助けは必要ありません。何か案が?

答えて

0

私は自分自身に解決策発見しました:

public function createHello() 
{ 
    $collection = $this->greetings->getItems(); 
    $hello = [ 
     'id'=> $this->getId(), 
    ]; 

    foreach ($collection as $col) { 
     $hello['items'][] = $this->getItems($col); 
    } 

    return $hello; 
} 


public function getItems($col) 
{ 
    $a = ..; 
    $b = ..; 
    $c = ..; 
    $items = [ 
     'a' => $a, 
     'b' => $b, 
     'c' => $c 
    ] 

    return $items; 
}