回调函数array_filter()只传递数组的值,而不传递键.
如果我有:
$my_array = array("foo" => 1, "hello" => "world");
$allowed = array("foo", "bar");
Run Code Online (Sandbox Code Playgroud)
删除$my_array不在$allowed数组中的所有键的最佳方法是什么?
期望的输出:
$my_array = array("foo" => 1);
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数组:
array( array(), array(), array(), array() );
Run Code Online (Sandbox Code Playgroud)
主数组中的数组包含4个键及其值.所有数组中的键都是相同的,如下所示:
array( 'id' => 'post_1',
'desc' => 'Description 1',
'type' => 'type1',
'title' => 'Title'
);
array( 'id' => 'post_2',
'desc' => 'Description 2',
'type' => 'type2',
'title' => 'Title'
);
Run Code Online (Sandbox Code Playgroud)
所以我想创建另一个数组并提取id和type值并将它们放在一个新数组中,如下所示:
array( 'post_1' => 'type1', 'post_2' => 'type2'); // and so on
Run Code Online (Sandbox Code Playgroud)
此数组中的键将是id键旧数组的值,它们的值将是type键的值.
那么有可能实现这一目标吗?我试过搜索php.net数组函数,但我不知道使用哪个函数?
当我使用多列的pluck时,我得到这个:
{"Kreis 1 \/ Altstadt":"City","Kreis 2":"Enge","Kreis 3":"Sihifeld","Kreis 4":"Hard","Kreis 5 \/ Industriequartier":"Escher Wyss","Kreis 6":"Oberstrass","Kreis 7":"Witikon","Kreis 8 \/ Reisbach":"Weinegg","Kreis 9":"Altstetten","Kreis 10":"Wipkingen","Kreis 11":"Seebach","Kreis 12 \/ Schwamendingen":"Hirzenbach"
Run Code Online (Sandbox Code Playgroud)
但我需要这个吗?
["Rathaus","Hochschulen","Lindenhof","City","Wollishofen","Leimbach","Enge","Alt-Wiedikon","Friesenberg","Sihifeld","Werd","Langstrasse","Hard","Gewerbechule","Escher Wyss","Unterstrass","Oberstrass","Fluntern","Hottingen","Hirslanden","Witikon","Seefeld","M\u00fchlebach","Weinegg","Albisrieden","Altstetten","H\u00f6ngg","Wipkingen","Affoltern","Oerlikon","Seebach","Saatlen","Schwamendingen-Mitte","Hirzenbach"]
Run Code Online (Sandbox Code Playgroud)
任何建议我怎么能这样做?这是我的方法:
public function autocomplete_districts(Request $request)
{
$district = $request->input('query');
// $ass = /DB::table('districts')->select(array('district', 'region'))->get();
// dd($ass);
$data = Districts::whereRaw('LOWER(district) like ?', [strtolower('%'.$district . '%')])->orWhereRaw('LOWER(region) like ?', [strtolower('%'.$district . '%')])->pluck('region','district');
return response()->json($data);
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的二维数组,如下所示:
Array
(
[0] => Array
(
[0] => abc
[1] => 123
[2] => aaaaa
)
[1] => Array
(
[0] => def
[1] => 456
[2] => ddddd
)
[2] => Array
(
[0] => ghi
[1] => 789
[2] => hhhhhhh
)
)
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个有效的函数,它将返回一个只包含每个子数组的第一个'n'列的数组.换句话说,如果n = 2,那么返回的数组将是:
Array
(
[0] => Array
(
[0] => abc
[1] => 123
)
[1] => Array
(
[0] => def
[1] => 456
)
[2] => Array
(
[0] => ghi …Run Code Online (Sandbox Code Playgroud)