1

Believe me, I have tried everything that is there in stack overflow!

So, I got this 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"
        }
    ]
}

and i am accessing it like this -

$jo = json_decode($j);
for($i = 1; $i < count($jo->itempicture); $i++) {
    foreach($jo->itempicture[$i] as $prop=>$val) {
        echo $val.",";
    }
    echo '<br>';
}

and I'm getting this output -

001,image1,shirt,Yes,2018-06-02 11:53:57,
002,image2,shirt,Yes,2018-06-02 11:53:57,

The main prob with this output is the "," at the last. I am unable to remove it!

Tried everything - This - Remove the last character from string

with substr, rtrim, implode... EVERYTHING!

It's not working!

Munim Munna
  • 17,178
  • 6
  • 29
  • 58
Abhijeetc50
  • 79
  • 2
  • 11
  • 2
    You are echoing, i guess rtrim did work in that case, but if you create a variable $string += $val.","; and then echo $string; I'm very sure that rtrim() will work... echo rtrim($string, ','); – Roy Bogado Jun 13 '18 at 08:02
  • An alternative to @Roy's solution is after the foreach loop to store in a variable and echo it after the foreach loop using `substr($string, 0, -1);` – JustCarty Jun 13 '18 at 08:09
  • Well foreach is "for each", you could use a "for" instead. Mkes things easier to know the last one. – Frédéric Clausset Jun 13 '18 at 08:35
  • Have a look at this question https://stackoverflow.com/questions/1070244/how-to-determine-the-first-and-last-iteration-in-a-foreach-loop. But it is still awkward to use a foreach like this I think. – Frédéric Clausset Jun 13 '18 at 08:37

4 Answers4

2

A version using implode() which means that you have to convert from JSON into arrays (the default is to convert to objects, so add true as second paramter to 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>";

Prints out...

<pre>001,image1,shirt,Yes,2018-06-02 11:53:57<br/>
002,image2,shirt,Yes,2018-06-02 11:53:57<br/></pre>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • I think this is the better of all solutions. Unless you have a very good reason to decode the JSON as an object. `implode` does just what you need and keeps the code much simpler. – apriede Jun 13 '18 at 08:26
  • remove the "
    " tags and got this error  - 
    array_shift() expects parameter 1 to be array, null given in line 15  @nigel
    – Abhijeetc50 Jun 13 '18 at 09:00
  • Did you add single quotes round the JSON ($j), I've added this to the answer. – Nigel Ren Jun 13 '18 at 09:04
  • Also make sure you have the right `json_decode()` line. – Nigel Ren Jun 13 '18 at 09:07
1

Here is the solution

$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>";
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
0

You could do something like this

 $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>';
}

That would prevent from writing the last ',' by comparing if your current iteration is the end of the loop

Gregoire Ducharme
  • 1,095
  • 12
  • 24
0

If you know that the last element would always be 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>';
}
Akshay
  • 2,244
  • 3
  • 15
  • 34