-2

I have an array result like this

Array
(
    [e] => 6
    [i] => 5
    [s] => 4
    [n] => 0
    [t] => 0
)

And i need to change the array format to this,

Array
(
    [0] => stdClass Object
        (
            [type] => e
            [count] => 6
        )

    [1] => stdClass Object
        (
            [type] => i
            [count] => 5
        )

    [2] => stdClass Object
        (
            [type] => s
            [count] => 4
        )

    [3] => stdClass Object
        (
           [type] => n
           [count] => 0
        )

    [4] => stdClass Object
        (
           [type] => t
           [count] => 0
        )
)

Is there any way to change ?

u_mulder
  • 54,101
  • 5
  • 48
  • 64
ashokan
  • 70
  • 5

1 Answers1

1
$newArray = [];
foreach ($array as $k => $v) {
    $obj = new stdClass;
    $obj->type = $k;
    $obj->count = $v;
    $newArray[] = $obj;
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64