亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以給定的格式填充數組?

如何以給定的格式填充數組?

PHP
慕后森 2023-06-30 16:07:24
我想形成一個像這樣的數組:$routes = [            array("key" => 0, "title" => "All"),            array("key" => 1, "title" => "Web Series"),            array("key" => 2, "title" => "Reality Shows"),            array("key" => 3, "title" => "Singing"),            array("key" => 4, "title" => "Vendors")        ];在這里,標題(網絡連續劇、真人秀、歌唱)應該來自數據庫的響應:    $projectTypes = ProjectType::select('project_type_title')->get()->all();如何使用這些值填充像上面這樣的數組?筆記:array("key" => 0,       "title" => "All") 必須預定義并且不從數據庫中獲取,只有其他項目類型才會從數據庫中獲取。我的數據庫包含以下值:array ( 0 => 'Web series', 1 => 'Drama', 2 => 'Movie', )
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

請隨意使用下面的示例


   $projectTypes = [array("key" => 1, "title" => "Web Series"),

        array("key" => 2, "title" => "Reality Shows"),

        array("key" => 3, "title" => "Singing"),

        array("key" => 4, "title" => "Vendors")];


$routes = [["key" => 0, 

      "title" => "All"]];


foreach ($projectTypes as $value) {

    $routes[] = ["key" => $value['key'], "title" => $value['title']];

}

結果:


Array ( [0] => Array ( [key] => 0 [title] => All ) 

        [1] => Array ( [key] => 1 [title] => Web Series ) 

        [2] => Array ( [key] => 2 [title] => Reality Shows ) 

        [3] => Array ( [key] => 3 [title] => Singing ) 

        [4] => Array ( [key] => 4 [title] => Vendors ) )

編輯:


$projectTypes = ProjectType::select('project_type_title')->get()->all();


$finalResult= [["key" => 0, 

      "title" => "All"]];


foreach ($projectTypes as $key => $value) {

    $finalResult[] = ["key" => $key + 1, "title" => $value];

}


查看完整回答
反對 回復 2023-06-30
  • 1 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號