2015-05-29 105 views
-1
How to my array combine : 
$visitor_tracvking_data = json_encode($array); 
print_r($visitor_tracvking_data); 

輸出的print_r這樣:如何一個陣列在PHP結合

array (size=6) 
    0 => 
    array (size=2) 
     'tracktitle' => string 'search_engine' (length=13) 
     'direct' => string '261' (length=3) 
    1 => 
    array (size=2) 
     'tracktitle' => string 'search_engine' (length=13) 
     'social_media' => string '3' (length=1) 
    2 => 
    array (size=2) 
     'tracktitle' => string 'search_engine' (length=13) 
     'search' => string '3' (length=1) 
    3 => 
    array (size=2) 
     'tracktitle' => string 'browser' (length=7) 
     'chrome' => string '168' (length=3) 
    4 => 
    array (size=2) 
     'tracktitle' => string 'browser' (length=7) 
     'firefox' => string '68' (length=2) 
    5 => 
    array (size=2) 
     'tracktitle' => string 'browser' (length=7) 
     'netscape' => string '31' (length=2) 

如何出把這個類型: -

array (size=2) 
    0 => 
    array (size=2) 
     'tracktitle' => string 'search_engine' (length=13) 
     'direct' => string '261' (length=3) 
     'social_media' => string '3' (length=1) 
     'search' => string '3' (length=1) 
    1 => 
    array (size=2) 
     'tracktitle' => string 'browser' (length=7) 
     'chrome' => string '168' (length=3) 
     'firefox' => string '68' (length=2) 
     'netscape' => string '31' (length=2) 
+1

好。有人要這樣做。或者把你的代碼放在你爲什麼而努力的地方? –

+0

循環和使用array_push –

+0

http://stackoverflow.com/questions/1558291/php-merge-2-multidimensional-array –

回答

0

只是遍歷數組,併產生帶有tracktitle鍵的新陣列 -

$new = array(); 

foreach($array as $key_top => $value) { 
    foreach($value as $key => $val) { 
     $new[$value['tracktitle']][$key] = $val; 
    } 
} 

return $new; 

或用於JSON編碼字符串 -

return json_encode($new); 
+0

爲什麼downvote? –

+0

如何返回值? –

+0

@GaurangP'$ new'將存放數組。如果你想返回數組返回'$ new'或者json返回json_encode($ new)。簡單。 –