0

I've converted this array from stdclass object.

I'm using var_dump to print the output of an array which is something like this:

array (size=1)
  'fruits' => 
    array (size=2)
      0 => 
        object(stdClass)[2]
            public 'name' => string 'apple' (length=5)
            public 'origin' => string 'kashmir' (length=7)
            public 'number' => int 50
      1 => 
        object(stdClass)[1]
            public 'name' => string 'orange' (length=6)
            public 'origin' => string 'nevada' (length=6)
            public 'number' => int 20

I know var_dump also gives the datatype and length. However, I want the output to be something like this:

1.{
   'name':'apple',
   'origin':'kashmir',
   'number':50
   },
2.{
  'name':'orange',
  'origin':'nevada',
  'number':20
  }

Can anybody please help me?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user2509780
  • 121
  • 2
  • 9

3 Answers3

0

try the json_encode($array) function http://php.net/manual/en/function.json-encode.php

Sevag Akelian
  • 251
  • 1
  • 2
  • 7
  • if I use json_encode(), it'll give a json string like {"fruits":[{"name":"apple","origin":"kashmir","number":50},{"name":"orange","origin":"nevada","number":20}]} However, I want the output the to be formatted as above. – user2509780 Jul 05 '13 at 13:25
0

If you want the same output as your example you will probably need to implement a serializer. There are also this functions that you could use: print_r, serialize

EDIT

Try this script:

foreach($array['fruits'] as $key=>$fruit) {
   echo ($key+1) . ".{\n";
   echo "\t'name':'$fruit->name',\n";
   echo "\t'number':'$fruit->number'\n";
   echo "},\n";
}

Note that I have not tested this script.

lexmihaylov
  • 717
  • 3
  • 8
0
echo "<pre>"; print_r($myarray); echo "</pre>";

that gives something like you asked.

Yordi
  • 195
  • 1
  • 8
  • Thank you, that does make it much more readable. I get the output like this: Array ( [fruits] => Array ( [0] => stdClass Object ( [name] => apple [origin] => kashmir [number] => 50 ) [1] => stdClass Object ( [name] => orange [origin] => nevada [number] => 20 ) Is there anyway I could remove the word "array" and "stdClass Object"? – user2509780 Jul 05 '13 at 13:58