-2

I have the array data as drawn image description here

and I want to display data like this

0 = "16-11-1996 / IGD"
01 = "CBC", "DIFF"
04 = "GLUS "," UR "," CRE " 

I tried to use this PHP splitting arrays into groups based on equal values method but it was not what I expected.

i am just learning coding and i am not what way or what keywords should i use to find a solution. i hope someone can help me,

4
  • 1
    can you please copy the picture into a codeblock for ease of reading and copying for those that may want to help? Commented Mar 19, 2021 at 5:04
  • 1
    Also, what's the issue? "Not what I expected" doesn't help. Commented Mar 19, 2021 at 15:59
  • I'm sorry if my question is not clear, I will correct my question again. I've got the answer to my question. and @jspit's answer is the same as the script I used. thanks Commented Mar 22, 2021 at 3:46
  • which part can I mark when my question is resolved? Commented Mar 22, 2021 at 3:49

2 Answers 2

1

A simple solution that uses foreach to create a new array.

Input:

$data = [
    ["OTHER_ID"=>"16-11-1996 / IGD", "SPEC_TYPE"=>"0"],
    
    ["OTHER_ID"=>"CBC", "SPEC_TYPE"=>"01"],
    ["OTHER_ID"=>"DIFF", "SPEC_TYPE"=>"01"],

    ["OTHER_ID"=>"GLUS", "SPEC_TYPE"=>"04"],
    ["OTHER_ID"=>"UR", "SPEC_TYPE"=>"04"],
    ["OTHER_ID"=>"CRE", "SPEC_TYPE"=>"04"],
];

Create a array grouped by "SPEC_TYPE".

$arr = [];
foreach($data as $row){
  $key = $row["SPEC_TYPE"];
  $arr[$key][] = $row["OTHER_ID"];
}

The array has "SPEC_TYPE" as a key and all "OTHER_ID" in a subarray. With another foreach we can now do the output. We cleverly use the implode function for this.

foreach($arr as $key => $others){
  echo $key.' = "'.implode('", "',$others).'"<br>';
}

Output for the example data above:

0 = "16-11-1996 / IGD"
01 = "CBC", "DIFF"
04 = "GLUS", "UR", "CRE"

Here you can try it yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry i was late updating my question. Thank you @jspit for your help, I can solve this problem. and thanks for this script I can use it too.
0

Try this I kept it as simple as I could since you are a beginner. First we take the unique spec types by using array_map to take out only spec types and then array_unique for taking out the unique values. After that for every spec id we would want to search the matching spec types and filter them out. For this we use array_filter and then again another array_map to take out the other ids and finally an implode function for converting the array into a string.

$data = [
    ["OTHER_ID"=>"A", "SPEC_TYPE"=>"1"],
    ["OTHER_ID"=>"B", "SPEC_TYPE"=>"2"],
    ["OTHER_ID"=>"C", "SPEC_TYPE"=>"1"],
    ["OTHER_ID"=>"D", "SPEC_TYPE"=>"3"],
    ["OTHER_ID"=>"E", "SPEC_TYPE"=>"3"]
];

$result = array_map(function ($a) { return $a["SPEC_TYPE"]; }, $data);
$result = array_unique($result);

$response=[];
foreach($result as $r) {
   $response[$r] = array_filter($data, function ($d) use($r) { return $r==$d["SPEC_TYPE"]; }); 

   $response[$r] = implode(array_map(function ($a) { return $a["OTHER_ID"]; }, $response[$r]), ", ");
}

echo json_encode($response);
// {"1":"A, C","2":"B","3":"D, E"};

In laravel you have helper functions to reduce the unnecessary code so we can reduce our array_map with array_pluck. Also array_where could be used in place of array_filter but in this case it would have the same callback and functionality so let's keep the filter for now. Below code isn't tested but should do the thing.

$result = array_pluck($data, "SPEC_TYPE");
$result = array_unique($result);

$response=[];
foreach($result as $r) {
  $response[$r] = array_filter($data, function ($d) use($r) { return $r==$d["SPEC_TYPE"]; }); 
  
  $response[$r] = implode(array_pluck($response[$r], "OTHER_ID"), ", ");
}

Resources to study more on this: Laravel helper functions: https://laravel.com/docs/5.0/helpers PHP array functions: https://www.w3schools.com/php/php_ref_array.asp

1 Comment

sorry i was late updating my question.I can solve this problem. Thank you @Beshambher for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.