배열에서 특정 요소를 가져오고 배열 키 값의 비교를 기반으로 새 배열을 만드는 방법은 무엇입니까?

PHPLover

내 질문이 어떤 사람들에게는 혼란스럽게 들릴 수 있습니다. 그러나 사실 그것은 아주 간단한 것입니다. 시나리오를 정리하겠습니다. $test_result_data다음과 같은 제목의 배열 이 있습니다.

Array
(
    [0] => Array
        (
            [test_pack_id] => 8ed32f6479a0169db3531d3366996d35
            [test_pack_name] => CPT Free Samples
            [test_pack_desc] => This package contains a free sample test of 30 minutes containing 30 questions from - Fundamentals of accounting, Mercantile Law, General Economics and Quantitative Aptitude. This is perfect to discover the beauty of online exam preparation.
            [test_pack_type_id] => 7
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 0
            [test_pack_validity_days] => 3
            [test_pack_plan] => free
            [test_pack_price] => 0.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_code] => 
            [test_pack_sold] => 22
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1337014879
            [test_pack_updated_date] => 1376886700
            [test_pack_purchase_date] => 1364454118
            [test_details] => Array
                (
                    [0] => Array
                        (
                            [test_id] => 159
                            [test_name] => CPT Sample Test
                            [test_max_score] => 120.000
                            [test_no_questions] => 30
                            [test_attempt] => 15
                            [test_not_attempt] => 7
                            [avg_test_user_time_used] => 00:04:39 Hr
                            [top_score] => 46.00
                            [avg_score] => 3
                            [lower_score] => 0.00
                        )

                )

        )

    [1] => Array
        (
            [test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
            [test_pack_name] => CPT General Economics
            [test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
<b>Total Tests in this Package : 27</b>
            [test_pack_type_id] => 7
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 6
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 359.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_code] => 
            [test_pack_sold] => 0
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1338298119
            [test_pack_updated_date] => 1343564534
            [test_pack_purchase_date] => 0
            [test_details] => Array
                (
                    [0] => Array
                        (
                            [test_id] => 580
                            [test_name] => CPT Testlet : Demand & Supply 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [1] => Array
                        (
                            [test_id] => 581
                            [test_name] => CPT Test : Micro Economics
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [2] => Array
                        (
                            [test_id] => 583
                            [test_name] => CPT Testlet : Micro Economics 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [3] => Array
                        (
                            [test_id] => 582
                            [test_name] => CPT Testlet : Micro Economics 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [4] => Array
                        (
                            [test_id] => 579
                            [test_name] => CPT Testlet : Demand & Supply 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [5] => Array
                        (
                            [test_id] => 578
                            [test_name] => CPT Test : Demand & Supply
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [6] => Array
                        (
                            [test_id] => 577
                            [test_name] => CPT Testlet : Production & Cost 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [7] => Array
                        (
                            [test_id] => 576
                            [test_name] => CPT Testlet : Production & Cost 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [8] => Array
                        (
                            [test_id] => 575
                            [test_name] => CPT Test : Production & Cost
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [9] => Array
                        (
                            [test_id] => 574
                            [test_name] => CPT Testlet : Price Determination 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [10] => Array
                        (
                            [test_id] => 573
                            [test_name] => CPT Testlet : Price Determination 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [11] => Array
                        (
                            [test_id] => 572
                            [test_name] => CPT Test : Price Determination
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [12] => Array
                        (
                            [test_id] => 571
                            [test_name] => CPT Testlet : Indian Economy - A Profile 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [13] => Array
                        (
                            [test_id] => 570
                            [test_name] => CPT Testlet : Indian Economy - A Profile 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [14] => Array
                        (
                            [test_id] => 569
                            [test_name] => CPT Test : Indian Economy - A Profile
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [15] => Array
                        (
                            [test_id] => 568
                            [test_name] => CPT Testlet : Aspects of Indian Economy 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [16] => Array
                        (
                            [test_id] => 567
                            [test_name] => CPT Testlet : Aspects of Indian Economy 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [17] => Array
                        (
                            [test_id] => 566
                            [test_name] => CPT Test : Aspects of Indian Economy
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [18] => Array
                        (
                            [test_id] => 565
                            [test_name] => CPT Testlet : Economic Reforms in India 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [19] => Array
                        (
                            [test_id] => 564
                            [test_name] => CPT Testlet : Economic Reforms in India 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [20] => Array
                        (
                            [test_id] => 563
                            [test_name] => CPT Test : Economic Reforms in India
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [21] => Array
                        (
                            [test_id] => 562
                            [test_name] => CPT Testlet : Money & Banking 1
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [22] => Array
                        (
                            [test_id] => 561
                            [test_name] => CPT Testlet : Money & Banking 2
                            [test_max_score] => 40.000
                            [test_no_questions] => 10
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [23] => Array
                        (
                            [test_id] => 560
                            [test_name] => CPT Test : Money & Banking
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [24] => Array
                        (
                            [test_id] => 559
                            [test_name] => CPT Test : General Economics 1
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                    [25] => Array
                        (
                            [test_id] => 558
                            [test_name] => CPT Test : General Economics 2
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 0.00
                            [avg_score] => 0
                            [lower_score] => 0.00
                        )

                    [26] => Array
                        (
                            [test_id] => 557
                            [test_name] => CPT Test : General Economics 3
                            [test_max_score] => 50.000
                            [test_no_questions] => 50
                            [test_attempt] => 0
                            [test_not_attempt] => 0
                            [top_score] => 
                            [avg_score] => 0
                            [lower_score] => 
                        )

                )

        )

)

이제이 배열 안에 두 개의 기본 배열이 있음을 알 수 있습니다. 이러한 각 배열에는 테스트 패키지 데이터가 포함되어 있습니다. 이 배열은 동적으로 생성되므로 매번 얼마나 많은 항목을 포함할지 말할 수 없습니다. 이제 하나를 가지고 $test_pack_id있고 키 [test_pack_id]의 값이 변수의 값과 수학하는 위의 배열에서 해당 배열 요소 만 가져오고 싶습니다 $test_pack_id. 이러한 일치하는 테스트 패키지 데이터 항목에 대해 완전히 새로운 배열을 만들고 싶습니다. 누구든지이 배열을 얻는 데 도움을 줄 수 있습니까?

다음 변수가 있다고 가정합니다.

$test_pack_id = '84880f321fc957f9b28b702d5a43f57b';//This is my input

배열 $test_result_data은 요소에이 ID를 한 번, 두 번 이상 포함하거나 포함하지 않을 수 있습니다. 그러나 일치하는 항목이 있으면 일치하는 모든 요소를 ​​반환해야합니다. 위의 경우 출력은 다음과 같습니다.

Array
(
    [0] => Array
        (
[test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
                [test_pack_name] => CPT General Economics
                [test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
    <b>Total Tests in this Package : 27</b>
                [test_pack_type_id] => 7
                [test_pack_image] => 
                [test_pack_validity_year] => 0
                [test_pack_validity_month] => 6
                [test_pack_validity_days] => 0
                [test_pack_plan] => paid
                [test_pack_price] => 359.00
                [test_pack_no_tests] => 0
                [test_pack_publish] => yes
                [test_pack_code] => 
                [test_pack_sold] => 0
                [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_created_date] => 1338298119
                [test_pack_updated_date] => 1343564534
                [test_pack_purchase_date] => 0
                [test_details] => Array
                    (
                        [0] => Array
                            (
                                [test_id] => 580
                                [test_name] => CPT Testlet : Demand & Supply 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [1] => Array
                            (
                                [test_id] => 581
                                [test_name] => CPT Test : Micro Economics
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [2] => Array
                            (
                                [test_id] => 583
                                [test_name] => CPT Testlet : Micro Economics 1
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [3] => Array
                            (
                                [test_id] => 582
                                [test_name] => CPT Testlet : Micro Economics 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )

                        [4] => Array
                            (
                                [test_id] => 579
                                [test_name] => CPT Testlet : Demand & Supply 2
                                [test_max_score] => 40.000
                                [test_no_questions] => 10
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 
                                [avg_score] => 0
                                [lower_score] => 
                            )

                        [5] => Array
                            (
                                [test_id] => 578
                                [test_name] => CPT Test : Demand & Supply
                                [test_max_score] => 50.000
                                [test_no_questions] => 50
                                [test_attempt] => 0
                                [test_not_attempt] => 0
                                [top_score] => 0.00
                                [avg_score] => 0
                                [lower_score] => 0.00
                            )




                    )

            )

    )
필립 구르 니

array_filter 사용 :

$filtered = array_filter($yourArray, function($element) use($test_pack_id) {
  return $element['test_pack_id'] == $test_pack_id;
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

TOP 리스트

  1. 1

    C # 16 진수 값 0x12는 잘못된 문자입니다.

  2. 2

    Matlab의 반복 Sortino 비율

  3. 3

    Python의 csv 파일에서 첫 번째 열 삭제

  4. 4

    개체 참조가 개체의 인스턴스로 설정되지 않았습니까? (예외 오류 ~ ASP.NET MVC)

  5. 5

    atob은 인코딩 된 base64 문자열을 디코딩하지 않습니다.

  6. 6

    EventEmitter <string>의 컨텍스트 'this'가 Observable <string> 유형의 'this'메서드에 할당되지 않았습니다.

  7. 7

    병합 셀을 사용하여 워크 시트의 데이터 필터링

  8. 8

    PhpStorm 중단 점에서 변수 값을 볼 수 없습니다.

  9. 9

    jQuery에서 이벤트 핸들러를 제거하는 가장 좋은 방법은 무엇입니까?

  10. 10

    `@ Transactional`이 있음에도 불구하고 이러한 데이터베이스 수정 사항이 롤백되지 않는 이유는 무엇입니까?

  11. 11

    ssh를 사용하여 원격에서 로컬로 파일 복사

  12. 12

    종속 사용자 정의 Lightning 선택 목록 Level2 및 Level3을 설정한 다음 Lightning 구성 요소에서 Level2를 재설정하지만 Level2 캐시 데이터가 저장됨

  13. 13

    2 개의 이미지를 단일 평면 이미지로 결합

  14. 14

    팝업처럼 위젯을 표시하는 방법

  15. 15

    [해결] 쿠키 설정 SameSite = Chrome / JSP, JAVASCRIPT에서 작동하지 않습니다.

  16. 16

    버튼 클릭을 기반으로 특정 CSS 클래스를 추가하는 방법은 무엇입니까?

  17. 17

    React 구성 요소가 자동으로 초기 상태로 다시 렌더링됩니다.

  18. 18

    연결된 서버 쿼리는 작동하지만 동일한 OPENQUERY는 "sys.servers에서 서버 'SERVER'를 찾을 수 없습니다.

  19. 19

    파일 2의 파일 1에서 동일한 줄을 조건으로 바꿉니다.

  20. 20

    아이디어 Intellij : 종속성 org.json : json : 20180813을 찾을 수 없음, maven에서 org.json 라이브러리를 가져올 수 없음

  21. 21

    상황에 맞는 메뉴 색상

뜨겁다태그

보관