-2

I have been fighting with this all day and would like another set of eyes to maybe give me some insight. I'm not sure I am even approaching this the correct way. I have two arrays but these arrays have different number lists. I'm building an app with Laravel.

$naflr = array(
    0 => "NA"
    1 => "A2"
    2 => "A2"
    3 => "A1"
    4 => "A1"
    5 => "A1"
    ...
    49 =>"A3"
)

$fuzifikasi = array(
    0 => "A2"
    1 => "A1"
    2 => "A4"
    3 => "A1"
    4 => "A1"
    5 => "A1"
    ...
    48 => "A4"
)

How do I filter the value array from the NAFLR array with FUZIFIKASI, I hope the result is like this

$resul = array(
    [na] = array(
        0 => "A2"
    )

    [A2] = array(
        0 = > "A1"
        1 => "A4"
    )
    ....
)
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38
salman
  • 27
  • 7
  • What is the question @salman. Do you want to loop over `$naflr `, get the key and value of each item in that array (say value is `$v` and the key is `$k`). Then get the value (say `$v2`) from array `$fuzifikasi ` that corresponds to the key `$k` and assign this value to another array (say `$r`) with key as `$v` and value as `$v2`. Is this what you need? – endeavour Jul 15 '21 at 04:00
  • I want to create a group from an array of $naflr values, which contains the value of $fuzzification the benchmark for example if $naflr has value A2 on row 1 then the value of $fuzzification is put into group A2 – salman Jul 15 '21 at 04:36
  • _$naflr has value A2 on row 1 then the value of $fuzzification is put into group_ -> which value from `$fuzzification` – endeavour Jul 15 '21 at 05:29
  • Yes, Can you provide a sample code?? – salman Jul 15 '21 at 05:34
  • I was asking which value from the array `$fuzzification` need to put into the group `A2` – endeavour Jul 15 '21 at 05:50
  • Value whose array key is equal to $naflr – salman Jul 15 '21 at 06:31

1 Answers1

1

If I got your description correctly, then all you need to do is loop over your first array, check if an item with the same index exists in your second array (not clear from your explanation, whether those will always have the exact same number of items or not), and if so, add it to the result array using the value from the first array as key:

$result = [];

foreach($naflr as $index => $value) {
    if(isset($fuzifikasi[$index])) {
        $result[$value][] = $fuzifikasi[$index];
    }
}

var_dump($result);

https://3v4l.org/L34El

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • May I ask another question sir? – salman Jul 16 '21 at 09:47
  • Regarding this specific problem, or something else? If the latter, then you should create a new question. – CBroe Jul 16 '21 at 09:51
  • https://stackoverflow.com/questions/68417882/how-to-add-multidimensional-array-values-whose-values-are-from-different-array @CBroe I write here, I hope you will help me again :) – salman Jul 17 '21 at 06:17