需要帮助将关联数组转换为键=>值对
Need help to convert Associative Array to key=>value pair
我的代码:
global $wpdb;
$row = $wpdb->get_results( "SELECT * FROM `wp_employee`", ARRAY_A );
print_r ($row);
输出:
Array(
[0] => Array(
[job_id] => 1
[job_position] => Architect
)
[1] => Array(
[job_id] => 2
[job_position] => Civil Engineer
)
[2] => Array(
[job_id] => 3
[job_position] => Electrical Engineer
)
[3] => Array(
[job_id] => 4
[job_position] => Plumbing Engineer
)
[4] => Array(
[job_id] => 5
[job_position] => Site Engineer
)
)
我需要的输出如下:
array(
'1' => 'Architect',
'2' => 'Civil Engineer',
'3' => 'Electrical Engineer',
'4' => 'Plumbing Engineer',
'5' => 'Site Engineer'
)
我还在学习,我自己也想不通。预先感谢您帮助我
$array = [
[ 'job_id' => 1, 'job_position' => 'Architect' ],
[ 'job_id' => 2, 'job_position' => 'Civil Engineer' ],
[ 'job_id' => 3, 'job_position' => 'Electrical Engineer' ],
[ 'job_id' => 4, 'job_position' => 'Plumbing Engineer' ],
[ 'job_id' => 5, 'job_position' => 'Site Engineer' ]
];
$result = array_column($array, 'job_position', 'job_id');
print_r($result);
输出:
Array
(
[1] => Architect
[2] => Civil Engineer
[3] => Electrical Engineer
[4] => Plumbing Engineer
[5] => Site Engineer
)
我的代码:
global $wpdb;
$row = $wpdb->get_results( "SELECT * FROM `wp_employee`", ARRAY_A );
print_r ($row);
输出:
Array( [0] => Array( [job_id] => 1 [job_position] => Architect ) [1] => Array( [job_id] => 2 [job_position] => Civil Engineer ) [2] => Array( [job_id] => 3 [job_position] => Electrical Engineer ) [3] => Array( [job_id] => 4 [job_position] => Plumbing Engineer ) [4] => Array( [job_id] => 5 [job_position] => Site Engineer ) )
我需要的输出如下:
array( '1' => 'Architect', '2' => 'Civil Engineer', '3' => 'Electrical Engineer', '4' => 'Plumbing Engineer', '5' => 'Site Engineer' )
我还在学习,我自己也想不通。预先感谢您帮助我
$array = [
[ 'job_id' => 1, 'job_position' => 'Architect' ],
[ 'job_id' => 2, 'job_position' => 'Civil Engineer' ],
[ 'job_id' => 3, 'job_position' => 'Electrical Engineer' ],
[ 'job_id' => 4, 'job_position' => 'Plumbing Engineer' ],
[ 'job_id' => 5, 'job_position' => 'Site Engineer' ]
];
$result = array_column($array, 'job_position', 'job_id');
print_r($result);
输出:
Array
(
[1] => Architect
[2] => Civil Engineer
[3] => Electrical Engineer
[4] => Plumbing Engineer
[5] => Site Engineer
)