在PHP中将数组元素转换为逗号分隔数组

时间:2021-02-19 13:40:52

I have a this type of array, i want to covert some elements in to comma separeted

我有一个这种类型的数组,我想将一些元素转换为逗号separeted

 Array
(
    [0] => Array
        (
            [tution_provider] => sd
            [app_subject_name] => KE2-Management Accounting Information
            [exam_activity] => 734
            [icasl_no] => 1746828
            [app_subject_id] => 1013
            [exam_session] => 000091
            [id] => 724
        )

    [1] => Array
        (
            [tution_provider] => sdfsdf
            [app_subject_name] => KE3B-Fundamentals of Law
            [exam_activity] => 734
            [icasl_no] => 1746828
            [app_subject_id] => 1016
            [exam_session] => 000091
            [id] => 725
        )

    [2] => Array
        (
            [tution_provider] => sdfsdfsdf
            [app_subject_name] => KE4-Processes, Assurance & Ethics
            [exam_activity] => 734
            [icasl_no] => 1746828
            [app_subject_id] => 1017
            [exam_session] => 000091
            [id] => 726
        )

)

And I want to get out put from the

而且我想从中脱颖而出

app_subject_id

app_subject_id

as a comma separated array. how can i do this?

作为逗号分隔的数组。我怎样才能做到这一点?

this is the out put that I want

这是我想要的输出

Array ( [0] => 1013 [1] => 1016 [2] => 1017 [3] => 1013 [4] => 1016 [5] => 1017 )

数组([0] => 1013 [1] => 1016 [2] => 1017 [3] => 1013 [4] => 1016 [5] => 1017)

3 个解决方案

#1


5  

$array = array_column($array, 'app_subject_id'); //Get an array of just the app_subject_id column
$array = implode(',', $array); //creates a string of the id's separated by commas

Output:

输出:

1013,1016,1017

#2


4  

A one liner will do it:

一个班轮将这样做:

$appSubjectId = implode(',', array_column($originalArray, 'app_subject_id'));

http://php.net/manual/en/function.implode.php

http://php.net/manual/en/function.implode.php

http://php.net/manual/en/function.array-column.php

http://php.net/manual/en/function.array-column.php

#3


0  

finally i have done this, thanks for your great support.

最后我做到了这一点,感谢您的大力支持。

       $subjctss = $myarray;

     if (!empty($subjctss)) {

                                        $subid_arr = array();
                                        foreach ($subjctss as $values) {
                                            $app_sub = $values['app_subject_id'];
                                            $subid_arr[] = $app_sub;

                                            echo '<ul><li>' . $values['app_subject_name'] . '</li>

                            </ul>';
                                        }
                                        if (!empty($subid_arr) && is_array($subid_arr)) {
//                                            $app_sub_comma_seprated = implode(',', $subid_arr);
                                            print_r($subid_arr);
                                        }
                                    }

#1


5  

$array = array_column($array, 'app_subject_id'); //Get an array of just the app_subject_id column
$array = implode(',', $array); //creates a string of the id's separated by commas

Output:

输出:

1013,1016,1017

#2


4  

A one liner will do it:

一个班轮将这样做:

$appSubjectId = implode(',', array_column($originalArray, 'app_subject_id'));

http://php.net/manual/en/function.implode.php

http://php.net/manual/en/function.implode.php

http://php.net/manual/en/function.array-column.php

http://php.net/manual/en/function.array-column.php

#3


0  

finally i have done this, thanks for your great support.

最后我做到了这一点,感谢您的大力支持。

       $subjctss = $myarray;

     if (!empty($subjctss)) {

                                        $subid_arr = array();
                                        foreach ($subjctss as $values) {
                                            $app_sub = $values['app_subject_id'];
                                            $subid_arr[] = $app_sub;

                                            echo '<ul><li>' . $values['app_subject_name'] . '</li>

                            </ul>';
                                        }
                                        if (!empty($subid_arr) && is_array($subid_arr)) {
//                                            $app_sub_comma_seprated = implode(',', $subid_arr);
                                            print_r($subid_arr);
                                        }
                                    }