search

Home  >  Q&A  >  body text

php - The most convenient or best way to convert objects to arrays

What is the convenient way to convert objects to arrays
Original data

$data = {
    "1": {
        "id": 1,
        "name": "超市类",
        "pid": 0,
    },
    "3": {
        "id": 3,
        "name": "餐饮类",
        "pid": 0,
    },
    "4": {
        "id": 4,
        "name": "造型类",
        "pid": 0,
    },
}

Target Result

$data = [{
    "id": 1,
    "name": "超市类",
    "pid": 0,
},{
    "id": 3,
    "name": "餐饮类",
    "pid": 0,
},{
    "id": 4,
    "name": "造型类",
    "pid": 0,
}]
怪我咯怪我咯2727 days ago504

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-06-05 11:10:09

    I think the original data is json, so

    $data = json_decode($data, true);
    $data = array_values($data);
    $data = json_encode($data);

    reply
    0
  • 高洛峰

    高洛峰2017-06-05 11:10:09

    A very stupid method (array)$data, this actually works too

    reply
    0
  • Cancelreply