2016-12-16 2 views
0

두 개의 별도 JSON 배열 (array1 및 array2)을 제공하는 PHP 함수가 있습니다. 어떻게이 같은 속성이있는 객체에 병합 할 수 있습니다 사전에별도의 배열을 가진 두 배열의 객체를 속성으로 만듭니다.

{ 
    "array1": [ {"type": "column", "valueField": ..., "descriptionField": ..., }] 
    "array2": [ {"type": "column", "valueField": ..., "descriptionField": ..., }] 
} 

감사를

+0

코드를 표시하고 결과로 원하는 것은 무엇입니까? 당신은 당신의 시도로 어떤 결과를 얻고 있습니까? 또한, [How to ask] (http://stackoverflow.com/help/how-to-ask) – Nytrix

+0

'$ result = "[$ array1, $ array2]"에 대해 읽어야합니다. 어쩌면 – AbraCadaver

답변

1

이것은 단지 당신이 그것을 할 수있는 방법의 간단한 예를 들어, 당신은 그러나 당신이 좋아 따라이를 개선 할 수있다 어떤 상황이 필요한지.

// Initialising arrays 
$array1 = ['type' => 'column', 'valueField' => '.1.', 'descriptionField' => '.11.']; 
$array2 = ['type' => 'column', 'valueField' => '.2.', 'descriptionField' => '.22.']; 

// Turn them manually into jsons 
$obj1 = json_encode($array1); 
$obj2 = json_encode($array2); 

// Merge the two jsonified arrays in a single array with whichever keys you prefer 
$mix = ['array1' => $obj1, 'array2' => $obj2]; 

// Turn the merged "mix" array into json 
$mix = json_encode($mix); 

// Check the output 
printf($mix); 

/* Prints out: 

{ 
    "array1":"{"type":"column", "valueField":".1.", "descriptionField":".11."}", 
    "array2":"{"type":"column", "valueField":".2.", "descriptionField":".22."}" 
} 

*/ 

당신이 SANDBOX에서의 그것으로 주변에 바이올린 수는, 그것으로 재미 좀.

+0

Tnx! 이것은 나를 위해 완벽하게 일했습니다! – CostCare

+0

@CostCare 문제 없습니다. 내가 널 도울 수있어서 기뻐. :] – Mihailo

관련 문제