-1
How to my array combine :
$visitor_tracvking_data = json_encode($array);
print_r($visitor_tracvking_data);

Output print_r this:

    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)

How to out put this type:-

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)
GIPSSTAR
  • 2,050
  • 1
  • 25
  • 20

1 Answers1

0

Just loop through the array and generate a new array with tracktitle key -

$new = array();

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

return $new;

OR for json encoded string -

return json_encode($new);
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • @GaurangP `$new` will hold the array.if you want to return the array the return `$new` or for json return json_encode($new). Simple. – Sougata Bose May 29 '15 at 07:54
  • My output:- How to out put this type: in display. But your output that in:- Output print_r this – GIPSSTAR May 29 '15 at 08:00
  • @GaurangP check - https://eval.in/372338. The way you want. just two new keys instead of `0` & `1`. – Sougata Bose May 29 '15 at 08:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79104/discussion-between-gaurang-p-and-b0s3). – GIPSSTAR May 29 '15 at 09:25