如何将数组转换为对象?

时间:2019-01-08 12:51:06

标签: php laravel

我正在尝试用我尝试过的对象制作数组,但没有得到下面显示的结构。 我该如何制作这种结构? 我已经编辑了我的问题,您可以看到我想要的结构。 感谢您的帮助。

$docs = Driver::where('id', '=', $driverID)->get();
if (count($docs) > 0) {
    /*final array to be passed to data*/
    $finalDataArray = array();
    foreach ($docs as $doc) {
        $finalDataArray[] = array(
            'profile_picture' => $doc->profile_picture,
            'dvla_desp' => $doc->dvla_license_desp,
            'dvla_license_expiry' => $doc->dvla_license_expiry,
            'dvla_license_img' => $doc->dvla_license_img,
            'taxi_license_desp' => $doc->taxi_license_desp,
            'taxi_license_expiry' => $doc->taxi_license_expiry,
            'taxi_license_img' => $doc->taxi_license_img,
            'taxi_desp' => $doc->taxi_badge_desp,
            'taxi_expiry' => $doc->taxi_badge_expiry,
            'taxi_badge_img' => $doc->taxi_badge_img,
            'additional_docs_desp' => $doc->additional_docs_desp,
            'additional_docs_expiry' => $doc->additional_docs_expiry,
            'additional_docs_img' => $doc->additional_docs_img,
        );
    }

想要这种结构:

[
  {
     "profile_picture" : "none"
  }
  {
     "dvla_desp": "none",
     "dvla_license_expiry": "2018-01-24",
     "dvla_license_img": example,
  },
  {
     "taxi_license_desp": "none",
     "taxi_license_expiry": "2018-01-24",
     "taxi_license_img": example,
  },
  {
     "taxi_desp": "none",
     "taxi_expiry": "2019-01-06",
     "taxi_badge_img": example,
   }
   {
     "additional_docs_desp": "none",
     "additional_docs_expiry": "2019-01-06",
     "additional_docs_img": example,
   }
]

我正在得到这个:

  "data": [
    {
        "profile_picture": example
        "dvla_desp": "none",
        "dvla_license_expiry": "2018-01-24",
        "dvla_license_img": example
        "taxi_license_desp": "none",
        "taxi_license_expiry": "2019-12-24",
        "taxi_license_img": example
        "taxi_desp": "none",
        "taxi_expiry": "2019-01-06",
        "taxi_badge_img": example
        "additional_docs_desp": "none",
        "additional_docs_expiry": "2018-12-24",
        "additional_docs_img": example
    }
]

6 个答案:

答案 0 :(得分:1)

您应该尝试以下操作:

$finalDataArray = (object)$finalDataArray;

更新后的答案

 $finalDataArray = json_encode($finalDataArray);

答案 1 :(得分:1)

现在,您正在创建一个平面关联数组。 您想在包含关联数组的数组中实现什么,因此只需执行以下操作:

$finalDataArray[] = [
    [
        'profile_picture' => $doc->profile_picture,
    ],
    [
        'dvla_desp' => $doc->dvla_license_desp,
        'dvla_license_expiry' => $doc->dvla_license_expiry,
        'dvla_license_img' => $doc->dvla_license_img,
    ],
    [
        'taxi_license_desp' => $doc->taxi_license_desp,
        'taxi_license_expiry' => $doc->taxi_license_expiry,
        'taxi_license_img' => $doc->taxi_license_img,
    ],
    [
        'taxi_desp' => $doc->taxi_badge_desp,
        'taxi_expiry' => $doc->taxi_badge_expiry,
        'taxi_badge_img' => $doc->taxi_badge_img,
    ],
    [
        'additional_docs_desp' => $doc->additional_docs_desp,
        'additional_docs_expiry' => $doc->additional_docs_expiry,
        'additional_docs_img' => $doc->additional_docs_img,
    ]
);

如果您json_encode该数组,您应该会收到所需的结构。 数组中的小型关联数组将被编码为对象,因为它们具有命名索引而不是数字。

我没有对此进行仔细检查,请随时尝试。

答案 2 :(得分:1)

这可能会解决您的问题

$val1 =(object) array('profile_picture' => "None");
$val2 =(object) array('dvla_desp' => "some value",
            'dvla_license_expiry' => "some value",
            'dvla_license_img' => "some value");
$val3 =(object) array('taxi_license_desp' => "some value",
            'taxi_license_expiry' => "some value",
      'taxi_license_img' => "some value"
     );$val4 =(object) array('taxi_desp' => "some value",
            'taxi_expiry' => "some value",
      'taxi_badge_img' => "some value"
     );$val5 =(object) array('additional_docs_desp' => "some value",
            'additional_docs_expiry' => "some value",
      'additional_docs_img' => "some value"
     );
 $finalDataArray = array($val1,$val2,$val3,$val4,$val5);

echo json_encode($finalDataArray);

答案 3 :(得分:1)

您可以使用stdClass代替Array 试试这个

$docs = Driver::where('id', '=', $driverID)->get();
if (count($docs) > 0) {
/*final array to be passed to data*/
$finalDataArray = array();
foreach ($docs as $doc) {
      $newClass1 = new \stdClass;
      $newClass1->profile_picture = $doc->profile_picture;
      $newClass2 = new \stdClass;
      $newClass2->dvla_desp = $doc->dvla_license_desp;
      $newClass2->dvla_license_expiry = $doc->dvla_license_expiry;
      $newClass2->dvla_license_img = $doc->dvla_license_img;
      $newClass3 = new \stdClass;
      $newClass3->taxi_license_desp = $doc->taxi_license_desp;
      $newClass3->taxi_license_expiry = $doc->taxi_license_expiry;
      $newClass3->taxi_license_img = $doc->taxi_license_img;
      $newClass4 = new \stdClass;
      $newClass4->taxi_desp = $doc->taxi_badge_desp;
      $newClass4->taxi_expiry = $doc->taxi_badge_expiry;
      $newClass4->taxi_badge_img = $doc->taxi_badge_img;
      $newClass5 = new \stdClass;
      $newClass5->additional_docs_desp = $doc->additional_docs_desp;
      $newClass5->additional_docs_expiry = $doc->additional_docs_expiry;
      $newClass5->additional_docs_img = $doc->additional_docs_img;
      $finalDataArray[] = [$newClass1,$newClass2,$newClass3,$newClass4,$newClass5];
}

答案 4 :(得分:0)

尝试

if (count($docs) > 0) {
            /*final array to be passed to data*/
            $finalDataArray = array();

            foreach ($docs as $key=> $doc) {

                  $docs->{$key} = $doc;

                $finalDataArray[] = array(
                    'profile_picture' => $doc->profile_picture,
                    'dvla_desp' => $doc->dvla_license_desp,
                    'dvla_license_expiry' => $doc->dvla_license_expiry,
                    'dvla_license_img' => $doc->dvla_license_img,
                    'taxi_license_desp' => $doc->taxi_license_desp,
                    'taxi_license_expiry' => $doc->taxi_license_expiry,
                    'taxi_license_img' => $doc->taxi_license_img,
                    'taxi_desp' => $doc->taxi_badge_desp,
                    'taxi_expiry' => $doc->taxi_badge_expiry,
                    'taxi_badge_img' => $doc->taxi_badge_img,
                    'additional_docs_desp' => $doc->additional_docs_desp,
                    'additional_docs_expiry' => $doc->additional_docs_expiry,
                    'additional_docs_img' => $doc->additional_docs_img,
                );
            }

答案 5 :(得分:0)

尝试一下:

$docs = Driver::where('id', '=', $driverID)->get();
if (count($docs) > 0) {
    /*final array to be passed to data*/
    $finalDataArray = array();
    foreach ($docs as $doc) {
        //convert array in object and assign it into array
        $finalDataArray[] = (object) array(
            'profile_picture' => $doc->profile_picture,
            'dvla_desp' => $doc->dvla_license_desp,
            'dvla_license_expiry' => $doc->dvla_license_expiry,
            'dvla_license_img' => $doc->dvla_license_img,
            'taxi_license_desp' => $doc->taxi_license_desp,
            'taxi_license_expiry' => $doc->taxi_license_expiry,
            'taxi_license_img' => $doc->taxi_license_img,
            'taxi_desp' => $doc->taxi_badge_desp,
            'taxi_expiry' => $doc->taxi_badge_expiry,
            'taxi_badge_img' => $doc->taxi_badge_img,
            'additional_docs_desp' => $doc->additional_docs_desp,
            'additional_docs_expiry' => $doc->additional_docs_expiry,
            'additional_docs_img' => $doc->additional_docs_img,
        );
    }