开发者

Problem concatenating json_ecode'd data

开发者 https://www.devze.com 2023-04-04 01:38 出处:网络
In a while loop I set up this array: $prejsonarray[] = json_encode(array( \'dealerName\'=> $row_1, \'dealerAddress\' => $addressstring));

In a while loop I set up this array:

$prejsonarray[] = json_encode(array( 'dealerName'=> $row_1, 'dealerAddress' => $addressstring  ));

Then outside it I try to concatenate the JSON data into a single strin开发者_如何学Cg:

foreach($prejsonarray as $v){ $dealersstring .= "$v " }

And I get the error:

Parse error: syntax error, unexpected '}'

For the foreach line. However, if I echo out $prejsonarray[0] I can see it contains the expected JSON data.

Any ideas?


foreach($prejsonarray as $v){ $dealersstring .= "$v "; }

Missing the ;


You should not just concatenate JSON strings. The result is most likely not a valid JSON string, if this is what you want as result. In this case, encode the whole array:

$prejsonarray[] = array('dealerName'=> $row_1, 'dealerAddress' => $addressstring );

At the end:

$dealersstring = json_encode($prejsonarray);

However, you get the syntax error because you are missing a semicolon ;:

foreach($prejsonarray as $v){ $dealersstring .= "$v "; }
//                                                   ^

Instead of the concatenating array values this way, you should use implode [docs]:

$dealersstring = implode(' ', $prejsonarray);


try this

foreach($prejsonarray as $v){ $dealersstring .= "$v "; }

you just have to add a semi colon.

the reason why the parser throws an error "unexpected }" because after the end of a line there needs to be a statement terminator which is none other than ";" so when it looks for a semicolon but in your case it has a " } "

0

精彩评论

暂无评论...
验证码 换一张
取 消