2017-03-13 222 views
1

我要合併姓名和作爲全名一起LASTNAMEJSON編碼姓名+姓氏=全名

$record[0]["firstname"]="foo"; 
$record[0]["lastname"]="bar"; 
$record[0]["age"]="20"; 

for ($i=0; $i<3; $i++) { 

    $foobar_array[] = array(
      'fullname' => [$record[$i]['firstname'], $record[$i]['lastname']], 
      'age' => $record[$i]['age'] 

    ); 


} 

echo json_encode($foobar_array); 

我想JSON字符串顯示

[{"fullname":"foo bar ","age":"20"}] 
+0

使用join array_join來記住php –

+0

''fullname'=> $ record [$ i] ['firstname']。 ''。 $ record [$ i] ['lastname']' – AbraCadaver

回答

2

此:

$foobar_array[] = array(
    'fullname' => $record[$i]['firstname'] . ' ' . $record[$i]['lastname'], 
     'age' => $record[$i]['age'] 
); 
+0

是'.'是PHP中的字符串concat運算符 –

1
$record = [ 
    [ 
     'firstname' => 'foo', 
     'lastname' => 'bar', 
     'age' => 20, 
    ] 
]; 

foreach ($record as $r) { 
    $foobar_array[] = [ 
      'fullname' => $r['firstname'].' '.$r['lastname'], 
      'age' => $r['age'] 
    ]; 
} 

echo json_encode($foobar_array); 

- >

[{"fullname":"foo bar","age":"20"}]