-1

I'm working on a quick project that will call a webservice, to return data. This is written in php, how do I format the output to make it readable?

the output returns this:

object(stdClass)#2 (2) {
  ["pid"]=>
  string(9) "12345678"
  ["quantities"]=>
  object(stdClass)#3 (1) {
    ["quantity"]=>
    array(2) {
      [0]=>
      object(stdClass)#4 (2) {
        ["_"]=>
        string(5) "2.000"
        ["sid"]=>
        string(2) "001"
      }
      [1]=>
      object(stdClass)#5 (2) {
        ["_"]=>
        string(5) "2.000"
        ["sid"]=>
        string(2) "002"
      }
    }
  }
}

I want the output to look like this:

pid: 12345678
sid: 001  quantity: 2.00
sid: 002  quantity: 1.00

Can someone help me to make the output format readable for users? I have no clue how to do it in php. Appreciate everyone's help.

deceze
  • 510,633
  • 85
  • 743
  • 889
user2885241
  • 71
  • 2
  • 3
  • 11
  • The result is an obj so `echo $yourObj->pid` should give you "12345678", no? If you know the format you can find what you want, so `echo $yourObj->quantities->quantity` is an array, you can do a foreach to get sid and quantity value – Mickaël Leger Mar 13 '18 at 15:34
  • 1
    The output you posted is generated by [`var_dump()`](http://php.net/manual/en/function.var-dump.php). The only purpose in life for `var_dump()` is to help on debugging. It doesn't help on something else, not even on [so]. – axiac Mar 13 '18 at 15:35
  • @axiac i thought i was the only one to find it useless ... i read json, french, english, italian, german, but i get stuck on var_dumpian. – YvesLeBorg Mar 13 '18 at 15:37
  • @user2885241 : any reason to avoid `json` ? It is readable by humans and machine alike, in many languages. – YvesLeBorg Mar 13 '18 at 15:38
  • 1
    The best function for dumping code to be posted on [so] is [`var_export()`](http://php.net/manual/en/function.var-export.php) but apparently this is a well-kept secret. Only a handful of people know about it :-( – axiac Mar 13 '18 at 15:39

1 Answers1

0

try something like this :

$readable = 'pid: ';
$readable .= $object->pid;
foreach($object->quantities as $q){
    $readable .= ' sid: ' . $q->sid;
    $readable .= ' quantity: ' . $q->_;
}

making an echo of $readable should give what you want

kevinniel
  • 1,088
  • 1
  • 6
  • 14