【问题标题】:PHP get key value pairs out from an array within a new array (array filtering, array reducing)PHP从新数组中的数组中获取键值对(数组过滤,数组减少)
【发布时间】:2017-06-14 13:27:08
【问题描述】:

我有一个这样的关联数组:

[1] => Array
        (
            [logo] => 
            [starting] => 
            [prelim_info] => 
            [state_entry] => 1
            [district_entry] => 3
            [CLIx_code] => 1009
            [survey_time] => 2017-05-29 09:38:00.0
            [Implement_module] => 
            [CLIxModule_Impl] => 1
            [Noimplement_reason] => 
            [Other_Reason] => 
            [implementedModule_Name] => 7 10 11 12
            [Lab_Open] => 3
            [Lab_Management] => 1
            [Planned_CLIxPeriods] => 2
            [ReasonsCancellation_CLIxClass] => 5
            [PowerCut_Visit] => 2
            [Observe_session:Session_observe] => 2
            [Observe_session:grade_observe] => 
            [Observe_session:grade_other] => 
            [Observe_session:Module_Observed] => 
            [Observe_session:Unit_Observed] => 
            [Observe_session:Lesson_Observed] => 
            [Observe_session:time_Sufficient] => 
            [Observe_session:Student_workindependent] => 
            [Observe_session:groupsinteracting] => 
            [Observe_session:groupshelping] => 
            [Observe_session:Students_Mothertongue] => 
            [Observe_session:Students_handbook] => 
            [Observe_session:Students_reflections] => 
            [Observe_session:teacherpresent] => 
            [Observe_session:encourage_platform] => 
            [Observe_session:encourage_classdisc] => 
            [Observe_session:mothertongue_teacher] => 
            [Observe_session:teacher_handbook] => 
            [Observe_session:Teacher_prepared] => 
            [TPDcertification_support1:TPD_Cert_Subject1] => 
            [TPDcertification_support1:tchrSupport_TPD1] => 
            [TPDcertification_support1:Source_Support] => 
            [TPDcertification_support2:TPD_Cert_Subject2] => 
            [TPDcertification_support2:tchrSupport_TPD2] => 
            [TPDcertification_support2:Source_Support] => 
            [TPDcertification_support3:TPD_Cert_Subject] => 
            [TPDcertification_support3:tchrSupport_TPD3] => 
            [TPDcertification_support3:Source_Support] => 
            [TPDcertification_support4:TPD_Cert_Subject] => 
            [TPDcertification_support4:tchrSupport_TPD4] => 
            [TPDcertification_support4:Source_Support] => 
            [teacher_feedback] => Regarding the old modules, maths teachers said that it was very useful as well as workbook.
            [FSP_feedback] => NA
            [TPDsuppport_certification:Supt_TPDcertification] => 1
            [TPDsuppport_certification:Source_Supportcertification] => 4
            [teacherSupport_moduleImplement] => They had training organised by the clix team and RMSA.
            [HighPoint] => NA
            [LowPoint] => There was no clix class since school reopen.
            [schlcontact_FSP_nooftimes] => 0
            [FT_contactreason] => 5
            [FT_otherreason] => 
            [Support_fromTE] => 2
            [Action_FT] => 1
            [Specific_ActionFT] => School lab need to recheck for start rolling out the modules.
            [Action_CLIxTeam] => 2
            [Sepecific_ActionCLIx] => 
            [Action_State] => 1
            [Specific_ActionState] => To issue the class period include in their regular time table.
            [Action_TPD] => 2
            [Specific_ActionTPD] => 
            [Session_observe2:Module_Second] => 
            [Session_observe2:Observe_grade] => 
            [Session_observe2:other_grade] => 
            [Session_observe2:Observation_Module2] => 
            [Session_observe2:Observation_Unit2] => 
            [Session_observe2:Observation_Lesson2] => 
            [Session_observe2:time_Sufficient2] => 
            [Session_observe2:Student_workindependent2] => 
            [Session_observe2:groupsinteracting2] => 
            [Session_observe2:groupshelping2] => 
            [Session_observe2:Students_Mothertongue2] => 
            [Session_observe2:Students_handbook2] => 
            [Session_observe2:Students_reflections2] => 
            [Session_observe2:teacherpresent2] => 
            [Session_observe2:encourage_platform2] => 
            [Session_observe2:encourage_classdisc2] => 
            [Session_observe2:mothertongue_teacher2] => 
            [Session_observe2:teacher_handbook2] => 
            [Session_observe2:Teacher_prepared2] => 
            [teacher_feedback2] => 
            [FSP_feedback2] => 
            [TPD_certification:tcher_TPD] => 
            [TPD_certification:certification_course_tchr_suport] => 
            [teachersupport_moduleImplement2] => 
            [school_location:Latitude] => 23.7428370300
            [school_location:Longitude] => 92.7227306900
            [school_location:Altitude] => 1014.0000000000
            [school_location:Accuracy] => 18.0000000000
            [generated_note_name_110] => 
            [meta:instanceID] => uuid:2568400e-0ec3-421d-94bc-10653732e6d2
        )

我想要这个数组中的一些键值对用于数据分析和可视化。输出应如下所示:-

array(
    [CLIx_code] => 1009
    [state_entry] => 1
    [district_entry] => 3
    ...
)

基本上,我想要一个包含一些过滤后的键值对的数组,我可以在其上操作一些数学函数并将它们传递给 D3.js 以进行数据可视化。

我是这样尝试的:

for ($i=0;$i<count($all_rows);$i++){
    $filteredarray[] = array($all_rows[$i]['CLIx_code'], $all_rows[$i]['state_entry'], $all_rows[$i]['district_entry']);    
}`

但我想要键值对,而不仅仅是我尝试使用 array_filter() 的值。

【问题讨论】:

  • 这是非常基本的。到目前为止,您尝试过什么?
  • 我们有什么可以帮助您的吗?为你写代码?
  • 您是否只想删除没有数字作为值的数组元素?或者是什么?展示你的编码尝试!
  • stackoverflow.com/questions/4260086/… 请参考这个问题的答案。它会帮助你。
  • @u_mulder 对不起,我没有写我的尝试

标签: php arrays filtering


【解决方案1】:

我必须假设您知道要保留和运行计算的元素的键名,所以 array_intersect_key() 是适合这项工作的工具。

代码(Demo):

$array=[
    'logo'=>'', 
    'starting'=>'', 
    'prelim_info'=>'', 
    'state_entry'=>1,
    'district_entry'=>3,
    'CLIx_code'=>1009
    // ... more elements
];
$filterkeys=[
    'state_entry',
    'district_entry',
    'CLIx_code'
    // ... more key names
];
var_export(array_intersect_key($array,array_flip($filterkeys)));

输出:

array (
  'state_entry' => 1,
  'district_entry' => 3,
  'CLIx_code' => 1009,
)

或者,如果您将 $filterkeys 键名设置为键,您可以放弃 array_flip() 调用,如下所示:

$filterkeys=[
    'state_entry'=>'',
    'district_entry'=>'',
    'CLIx_code'=>''
    // ... more key names
];
var_export(array_intersect_key($array,$filterkeys));

【讨论】:

    【解决方案2】:

    PHP: How to use array_filter() to filter array keys?

    只需参考这个问题的答案 就像您的情况一样,您需要编写如下代码:-

    <?php
    //You have below array including all keys
    $result_array = array('logo'=>'test','state_id'=>125,'prelim_info'=>'','CLIxModule_Impl'=>'5','district_entry'=>3);
    
    // Now provide only those keys which you need to filter.
    $filter_keys = array('district_entry','state_id');
    
    $filtered_array = array_filter($result_array,
        function ($key) use ($filter_keys) {
            return in_array($key, $filter_keys);
        },
        ARRAY_FILTER_USE_KEY
    );
    var_dump($filtered_array);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2018-10-20
      • 2022-01-23
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2018-02-16
      相关资源
      最近更新 更多