Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

how get each single column data from php multidimensional array?

how get each single column data from php multidimensional into single column array?

like $_test = = array(
    array(
        'id' => 1001,
        'first_name' => 'kalpesh',
        'last_name' => 'gamit',
    ),
    array(
        'id' => 1002,
        'first_name' => 'kartik',
        'last_name' => 'patel',
    ),
    array(
        'id' => 2002,
        'first_name' => 'smith',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 4004,
        'first_name' => 'patel',
        'last_name' => 'Doe',
    )
);

want result like id OR first_name only without use loop like while, foreach, for etc

Array
(
    [0] => 1001
    [1] => 1002
    [2] => 2002
    [3] => 4004
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

update your script like below and check it please but it will work only in PHP 5.5 greater than version only...

 $_test = = array(
    array(
        'id' => 1001,
        'first_name' => 'kalpesh',
        'last_name' => 'gamit',
    ),
    array(
        'id' => 1002,
        'first_name' => 'kartik',
        'last_name' => 'patel',
    ),
    array(
        'id' => 2002,
        'first_name' => 'smith',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 4004,
        'first_name' => 'patel',
        'last_name' => 'Doe',
    )
);

$ids_only = array_column($records, 'id');
print_r($ids_only);

Results for ID

Array
(
    [0] => 1001
    [1] => 1002
    [2] => 2002
    [3] => 4004
)

Results for first_name

$first_name = array_column($records, 'id');
print_r($first_name);
Array
(
    [0] => Kalpesh
    [1] => kartik
    [2] => smith
    [3] => patel
)

run above php script and please check....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...