2017-05-07 87 views
0

我有下面的PHP代碼,創建用foreach JSON出數組:PHP創建JSON用foreach

$array = array('one', 'two', 'three', 'four'); 

foreach ($array as $key => $value) { 
    $temp = array(
       'text' => 'Hello', 
       'text1' => 5, 
       'collect' => array(
           $value => array(
              'xx' => 'yy', 
              'key' => $key 
             ) 
          ) 
      ); 


    echo json_encode($temp); 
} 

輸出是這樣的:

{ 
    "text":"Hello", 
    "text1":5, 
    "collect":{"one":{"xx":"yy","key":0}} 
} 

{ 
    "text":"Hello", 
    "text1":5, 
    "collect":{"two":{"xx":"yy","key":1}} 
} 

{ 
    "text":"Hello", 
    "text1":5, 
    "collect":{"three":{"xx":"yy","key":2}} 
} 

{ 
    "text":"Hello", 
    "text1":5, 
    "collect":{"four":{"xx":"yy","key":3}} 
} 

但我想這一點:

{ 
    "text":"Hello", 
    "text1":5, 
    "collect": { 
     "one":{"xx":"yy","key":0}, 
     "two":{"xx":"yy","key":1}, 
     "three":{"xx":"yy","key":2}, 
     "four":{"xx":"yy","key":3} 
    } 
} 

我得到單個4單JSON對象,但我只需要一個與收集對象。

我不明白這一點...

回答

0

試試這個簡單的代碼,在此我們foreach之前有一個聲明。

Try this code snippet here

<?php 

ini_set('display_errors', 1); 
$array = array('one', 'two', 'three', 'four'); 

$temp = array(
    'text' => 'Hello', 
    'text1' => 5, 
    'collect' => array() 
); 
$collect = array(); 
foreach ($array as $key => $value) 
{ 
    $collect[$value] = array(
     'xx' => 'yy', 
     'key' => $key 
    ); 
} 
$temp["collect"]=$collect; 
echo json_encode($temp); 

輸出:

{ 
    "text": "Hello", 
    "text1": 5, 
    "collect": { 
     "one": { 
      "xx": "yy", 
      "key": 0 
     }, 
     "two": { 
      "xx": "yy", 
      "key": 1 
     }, 
     "three": { 
      "xx": "yy", 
      "key": 2 
     }, 
     "four": { 
      "xx": "yy", 
      "key": 3 
     } 
    } 
} 
+0

很酷謝謝:)! –

+0

@MartinMechani歡迎... :) –

0

您可以通過需要循環和這些值數組追加到temp數組的「收集」鍵。

$array = array('one', 'two', 'three', 'four'); 
$temp = array(
    'text' => 'Hello', 
    'text1' => 5, 
    'collect' => array() 
); 

foreach ($array as $key => $value) { 
    $temp['collect'][$value] = array(
     'xx' => 'yy', 
     'key' => $key 
    ); 
} 

echo json_encode($temp); 

以下是演示:https://eval.in/788929

+0

不行不行。結果是:'[{「text」:「Hello」,「text1」:5,「collect」:{「one」:{「xx」:「yy」,「key」:0}}},{「text 「:」 你好 「 」text1「 中的:5, 」收「:{ 」二「:{ 」XX「: 」YY「, 」關鍵「:1}}},{ 」文「: 」你好「,」 文本1 「5,」 收 「:{」 三化 「:{」 XX 「:」 YY」, 「重點」:2}}},{ 「文」: 「你好」, 「text1」 中的:5, 「收」: {「four」:{「xx」:「yy」,「key」:3}}}]' –

+0

Thanks !!!!!!!!! –

1

我想教育讀者的一對夫婦的替代方法以及亮點,其他兩個答案不必要的實例化collect子陣之前到循環(Sahil的答案出於某種原因做了兩次)。

結果數組的初始輸入數組和靜態元素應該放置在其他答案的開始位置。純粹由於個人喜好,我將使用short array syntax

輸入:

$array=['one','two','three','four']; 
$result=['text'=>'Hello','text1'=>5]; // <-- no 'comment' element declared 

現在對於遍歷$array和構建結果的動態元素的不同的方法。

方法#1:array_walk()

array_walk($array,function($v,$k)use(&$result){ 
    $result['collect'][$v]=['xx'=>'yy','key'=>$k]; 
}); 

方法#2:array_map()

array_map(function($k,$v)use(&$result){ 
    $result['collect'][$v]=['xx'=>'yy','key'=>$k]; 
},array_keys($array),$array); 

陣圖是低效率的,因爲它需要被傳遞給函數的額外陣列。

方法3:foreach()

foreach($array as $k=>$v){ 
    $result['collect'][$v]=['xx'=>'yy','key'=>$k]; 
} 

$result在這一點上看起來是這樣的:

array (
    'text' => 'Hello', 
    'text1' => 5, 
    'collect' => array (
     'one' => array ('xx' => 'yy', 'key' => 0), 
     'two' => array ('xx' => 'yy', 'key' => 1), 
     'three' => array ('xx' => 'yy', 'key' => 2), 
     'four' => array ('xx' => 'yy', 'key' => 3) 
    ) 
) 

foreach()是最簡單和最容易閱讀對於這種情況,但重要的是要了解和比較與php的數組函數相比,以確保您對任何給定的項目都使用最好的方法。

對於任何人想知道什麼是&use(&$result)是幹什麼的,那就是這是在anonymous function(又名closure)用來做$result變量「修改」功能中的reference

最後轉換使用json_encode()和顯示與echo到JSON:

echo json_encode($result); 

所有上述方法的產品相同的期望的輸出:

{"text":"Hello","text1":5,"collect":{"one":{"xx":"yy","key":0},"two":{"xx":"yy","key":1},"three":{"xx":"yy","key":2},"four":{"xx":"yy","key":3}}} 

這裏是Demo of all three methods