从循环中生成的字符串中删除最后一个字符
Remove the last charater from string which is generated in loop
相信我,我已经尝试了堆栈溢出中的所有方法!
所以,我得到了这个 JSON -
$j={
"itempicture": [
{
"status": "3"
},
{
"ItemCode": "001",
"ItemImage": "image1",
"ItemCategory": "shirt",
"ShowOnPOS": "Yes",
"LastModifiedOn": "2018-06-02 11:53:57"
},
{
"ItemCode": "002",
"ItemImage": "image2",
"ItemCategory": "shirt",
"ShowOnPOS": "Yes",
"LastModifiedOn": "2018-06-02 11:53:57"
}
]
}
我是这样访问它的 -
$jo = json_decode($j);
for($i = 1; $i < count($jo->itempicture); $i++) {
foreach($jo->itempicture[$i] as $prop=>$val) {
echo $val.",";
}
echo '<br>';
}
我得到了这个输出 -
001,image1,shirt,Yes,2018-06-02 11:53:57,
002,image2,shirt,Yes,2018-06-02 11:53:57,
这个输出的主要问题是最后的“,”。
我无法删除它!
什么都试过了 -
这个 -
Remove the last character from string
带有 substr、rtrim、内爆...
一切!
没用!
你可以这样做
$jo=json_decode($j);
for($i=1;$i<count($jo->itempicture);$i++){
foreach($jo->itempicture[$i] as $prop=>$val){
if ($value === end($jo->itempicture[$i])) {
echo $val;
}
else {
echo $val.",";
}
}
echo '<br>';
}
通过比较当前迭代是否是循环的末尾,可以防止写入最后一个“,”
这是解决方案
$j='{"itempicture":[
{
"status":"3"
},
{
"ItemCode":"001",
"ItemImage":"image1",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
},
{
"ItemCode":"002",
"ItemImage":"image2",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
}
]
}';
$jo=json_decode($j);
$edata = '';
for($i=1;$i<count($jo->itempicture);$i++){
$data = '';
foreach($jo->itempicture[$i] as $prop=>$val){
$data .= $val.",";
}
$edata .= rtrim($data, ",");
$edata .='<br/>';
}
echo "<pre>";
print_r($edata);
echo "</pre>";
如果你知道最后一个元素总是 LastModifiedOn
$jo=json_decode($j);
for($i=1;$i<count($jo->itempicture);$i++){
foreach($jo->itempicture[$i] as $prop=>$val){
if($prop == "LastModifiedOn")
echo $val;
else
echo $val.",";
}
echo '<br>';
}
一个使用implode()
的版本,这意味着你必须从JSON转换成数组(默认是转换成对象,所以将true作为第二个参数添加到json_decode()
) .
$j='{"itempicture":[
{
"status":"3"
},
{
"ItemCode":"001",
"ItemImage":"image1",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
},
{
"ItemCode":"002",
"ItemImage":"image2",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
}
]
}';
$jo=json_decode($j, true);
array_shift($jo['itempicture']);
$edata = '';
foreach ( $jo['itempicture'] as $item) {
$edata .= implode(",", $item).'<br/>';
}
echo "<pre>";
print_r($edata);
echo "</pre>";
打印出来...
<pre>001,image1,shirt,Yes,2018-06-02 11:53:57<br/>
002,image2,shirt,Yes,2018-06-02 11:53:57<br/></pre>
相信我,我已经尝试了堆栈溢出中的所有方法!
所以,我得到了这个 JSON -
$j={
"itempicture": [
{
"status": "3"
},
{
"ItemCode": "001",
"ItemImage": "image1",
"ItemCategory": "shirt",
"ShowOnPOS": "Yes",
"LastModifiedOn": "2018-06-02 11:53:57"
},
{
"ItemCode": "002",
"ItemImage": "image2",
"ItemCategory": "shirt",
"ShowOnPOS": "Yes",
"LastModifiedOn": "2018-06-02 11:53:57"
}
]
}
我是这样访问它的 -
$jo = json_decode($j);
for($i = 1; $i < count($jo->itempicture); $i++) {
foreach($jo->itempicture[$i] as $prop=>$val) {
echo $val.",";
}
echo '<br>';
}
我得到了这个输出 -
001,image1,shirt,Yes,2018-06-02 11:53:57,
002,image2,shirt,Yes,2018-06-02 11:53:57,
这个输出的主要问题是最后的“,”。 我无法删除它!
什么都试过了 - 这个 - Remove the last character from string
带有 substr、rtrim、内爆... 一切!
没用!
你可以这样做
$jo=json_decode($j);
for($i=1;$i<count($jo->itempicture);$i++){
foreach($jo->itempicture[$i] as $prop=>$val){
if ($value === end($jo->itempicture[$i])) {
echo $val;
}
else {
echo $val.",";
}
}
echo '<br>';
}
通过比较当前迭代是否是循环的末尾,可以防止写入最后一个“,”
这是解决方案
$j='{"itempicture":[
{
"status":"3"
},
{
"ItemCode":"001",
"ItemImage":"image1",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
},
{
"ItemCode":"002",
"ItemImage":"image2",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
}
]
}';
$jo=json_decode($j);
$edata = '';
for($i=1;$i<count($jo->itempicture);$i++){
$data = '';
foreach($jo->itempicture[$i] as $prop=>$val){
$data .= $val.",";
}
$edata .= rtrim($data, ",");
$edata .='<br/>';
}
echo "<pre>";
print_r($edata);
echo "</pre>";
如果你知道最后一个元素总是 LastModifiedOn
$jo=json_decode($j);
for($i=1;$i<count($jo->itempicture);$i++){
foreach($jo->itempicture[$i] as $prop=>$val){
if($prop == "LastModifiedOn")
echo $val;
else
echo $val.",";
}
echo '<br>';
}
一个使用implode()
的版本,这意味着你必须从JSON转换成数组(默认是转换成对象,所以将true作为第二个参数添加到json_decode()
) .
$j='{"itempicture":[
{
"status":"3"
},
{
"ItemCode":"001",
"ItemImage":"image1",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
},
{
"ItemCode":"002",
"ItemImage":"image2",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
}
]
}';
$jo=json_decode($j, true);
array_shift($jo['itempicture']);
$edata = '';
foreach ( $jo['itempicture'] as $item) {
$edata .= implode(",", $item).'<br/>';
}
echo "<pre>";
print_r($edata);
echo "</pre>";
打印出来...
<pre>001,image1,shirt,Yes,2018-06-02 11:53:57<br/>
002,image2,shirt,Yes,2018-06-02 11:53:57<br/></pre>