-2

I have two arrays that contain the value.

$list_partners_edr =
Array
(
    [0] => AIDA
    [1] => AMAN
    [2] => CDS
    [3] => CISForm
    [4] => ICJ Makassar
    [5] => ICJR
    [6] => IJRS
    [7] => Kemitraan
    [8] => LBH APIK
    [9] => LBH Makassar
    [10] => LBHM
    [11] => LeIP
    [12] => Peace Gen
    [13] => PEKKA
    [14] => PUSHAM UII
    [15] => Rumah KitaB
    [16] => SAPDA
    [17] => SIGAB
    [18] => Solo Bersimfoni
    [19] => TAF
    [20] => TII
    [21] => WAHID F
    [22] => YPP
)

note: this $list_partners_edr data can be retrieved from the field of [acronym] => AIDA from $partners_edr_data

It's actually less practical, I'm using 2 different arrays even though the partner information is already in the $partners_edr_data array. The script should be able to do it. Any suggestions?

and

$partners_edr_data = 
array(184)
(
    [0] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1272
            [edr_status] => 
        )

    [1] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1273
            [edr_status] => 
        )

    [2] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1274
            [edr_status] => 
        )

    [3] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1275
            [edr_status] => 
        )

    [4] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1276
            [edr_status] => 
        )

    [5] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1277
            [edr_status] => 
        )

    [6] => Array
        (
            [id] => 81
            [acronym] => AIDA
            [start_date] => 2022-05-20
            [end_date] => 2024-12-31
            [edr_code] => 1278
            [edr_status] => 
        )

    [7] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1710
            [edr_status] => ACHIEVED
        )

    [8] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1711
            [edr_status] => ONGOING
        )

    [9] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1712
            [edr_status] => ONGOING
        )

    [10] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1713
            [edr_status] => ACHIEVED
        )

    [11] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1714
            [edr_status] => ONGOING
        )

    [12] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1715
            [edr_status] => ONGOING
        )

    [13] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1716
            [edr_status] => ONGOING
        )

    [14] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1717
            [edr_status] => NOTACHIEVED
        )

    [15] => Array
        (
            [id] => 93
            [acronym] => AMAN
            [start_date] => 2022-06-20
            [end_date] => 2024-12-31
            [edr_code] => 1718
            [edr_status] => ACHIEVED
        )

....
    [183] => Array
        (
            [id] => 87
            [acronym] => YPP
            [start_date] => 2022-06-01
            [end_date] => 2024-12-31
            [edr_code] => 1466
            [edr_status] => 
        )

The expected result/goal is 3 groups of categories and 23 (representation of partners) values inside like below:

$_ach = [0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] (JSON encoded)
$_ong = [7,5,8,6,9,10,15,2,4,9,8,6,2,9,10,4,12,4,12,13,8,8,8] (JSON encoded)
$_nach = [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (JSON encoded)

Those values above will be fed to highchart column as below:

enter image description here

Have been crawling around within StackOverflow and others without no clue how to achieve those results. Any advice, a shorter way, or any thoughts? Thanks in advance.

  • Is this a MINIMAL [mcve]? Rarely do we ever need 22 of anything. Please always share your array data via `var_export()` formatting instead of `print_r()` so that contributors can instantly use your sample data in their test scripts. If `edr_code` is irrelevant, please remove it from your sample data payload. – mickmackusa May 24 '23 at 08:14
  • 1
    Also, if `start_date` and `end_date` are irrelevant, please remove all of those elements from your sample payload. Do you actually need those three separate PHP variables as a result? Might you be just as happy to generate a single array with 3 rows of data? – mickmackusa May 24 '23 at 08:20

1 Answers1

0

Actually, I didn't like my own answer as it is too long, impractical, and wordy. But at least the results are as I expected:

Here is the code:

        for ($i=0; $i < count($list_partners_edr); $i++) { 
          // code...
          foreach ($partners_edr_data as $key=>$value) {
              if ($value ['acronym'] == $temp_mitra_edr[$i]) {
                if ($value ['edr_status'] == 'ACHIEVED') {
                    $arr_transpose[$i][] = "ACHIEVED";
                }
                elseif ($value ['edr_status'] == 'ONGOING' || empty($value ['edr_status'])) {
                    $arr_transpose[$i][] = "ONGOING";
                }                
                elseif ($value ['edr_status'] == 'NOTACHIEVED') {
                    $arr_transpose[$i][] = "NOTACHIEVED";
                }                 
                else {
                  $arr_transpose[$i][] = "0";
                }
              }
          }
        }

        foreach($arr_transpose as $k => $v) {
            $result[$k] = array_count_values($v);
        }

        $key_to_check1 = "ACHIEVED";
        $key_to_check2 = "ONGOING";
        $key_to_check3 = "NOTACHIEVED";

        for ($i=0; $i < count($result); $i++) { 
            if(isset($result[$i][$key_to_check1])){
                $_ach[$i] = $result[$i][$key_to_check1];
            }
            else {
                $_ach[$i] = 0;
            }   
            if(isset($result[$i][$key_to_check2])){
                $_ong[$i] = $result[$i][$key_to_check2];
            }
            else {
                $_ong[$i] = 0;              
            }  
            if(isset($result[$i][$key_to_check3])){
                $_nach[$i] = $result[$i][$key_to_check3];
            }                                       
            else {
                $_nach[$i] = 0;              
            }  
        }

and here is the result:

Array
(
    [0] => 0
    [1] => 2
    [2] => 0
    [3] => 2
    [4] => 5
    [5] => 2
    [6] => 1
    [7] => 0
    [8] => 0
    [9] => 3
    [10] => 2
    [11] => 0
    [12] => 0
    [13] => 0
    [14] => 0
    [15] => 0
    [16] => 1
    [17] => 0
    [18] => 0
    [19] => 0
    [20] => 0
    [21] => 4
    [22] => 1
)
Array
(
    [0] => 7
    [1] => 7
    [2] => 8
    [3] => 3
    [4] => 4
    [5] => 8
    [6] => 14
    [7] => 2
    [8] => 4
    [9] => 6
    [10] => 6
    [11] => 6
    [12] => 2
    [13] => 9
    [14] => 7
    [15] => 3
    [16] => 10
    [17] => 4
    [18] => 12
    [19] => 13
    [20] => 8
    [21] => 4
    [22] => 8
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 1
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    [12] => 0
    [13] => 0
    [14] => 3
    [15] => 1
    [16] => 1
    [17] => 0
    [18] => 0
    [19] => 0
    [20] => 0
    [21] => 0
    [22] => 0
)

This is solved based on: Check if a key exists and get a corresponding value from an array in PHP

Still waiting on any advice, a shorter way, or any thoughts? Thanks.