Skip to content
Advertisement

Target index of array

I’m trying to target the index of my array. For some reason my array turns out really weird.

        foreach ($name as $n) {
          for ($x = 0; $x < count($mednames); $x++) {
            if (in_array($n, $mednames[$x])) {
              $duplicaten[] = $n;
              break;
            }
          }
        }

        $cnt = array_count_values($duplicaten);

        foreach ($cnt as $c) {
          if ($cnt > 1) {
            echo '<h2 style="color: gray;">';
            print_r($c);
            echo '</h2>';
          }
        }

Now I get the results I need. But I need the name of each index too.

Results:

var export – $name:

array ( 
0 => 'Ibuprofen', 
1 => 'Ibuprofen', 
2 => 'Penicillin', 
3 => 'Penicillin', 
4 => 'Penicillin', 
5 => 'Adderall', 
)

var_export – $mednames:

array ( 
0 => array ( 'name' => 'xxxx1', 'med_name' => 'Ibuprofen', ), 
1 => array ( 'name' => 'xxxx2', 'med_name' => 'Ibuprofen', ), 
2 => array ( 'name' => 'xxxx3', 'med_name' => 'Penicillin', ), 
3 => array ( 'name' => 'xxxx4', 'med_name' => 'Penicillin', ), 
4 => array ( 'name' => 'xxxx5', 'med_name' => 'Penicillin', ), 
5 => array ( 'name' => 'xxxx6', 'med_name' => 'Adderall', ), 
)

var_export – $cnt:

array ( 'Ibuprofen' => 2, 'Penicillin' => 3, 'Adderall' => 1, )

var_export – $c:

2
3

Now the problem here is that I need to have the name of each medicine in the $c variable/array, because I need the name of the medicine to get more information from the api. But the problem is I’m only getting the count for it. Is there a workaround for this? This is really bugging me. I’ve been stuck on this for hours.

Advertisement

Answer

You need to access the key in the second foreach:

<?php

$name = array ( 
0 => 'Ibuprofen', 
1 => 'Ibuprofen', 
2 => 'Penicillin', 
3 => 'Penicillin', 
4 => 'Penicillin', 
5 => 'Adderall', 
);

$mednames = array ( 
0 => array ( 'name' => 'xxxx1', 'med_name' => 'Ibuprofen', ), 
1 => array ( 'name' => 'xxxx2', 'med_name' => 'Ibuprofen', ), 
2 => array ( 'name' => 'xxxx3', 'med_name' => 'Penicillin', ), 
3 => array ( 'name' => 'xxxx4', 'med_name' => 'Penicillin', ), 
4 => array ( 'name' => 'xxxx5', 'med_name' => 'Penicillin', ), 
5 => array ( 'name' => 'xxxx6', 'med_name' => 'Adderall', ), 
);


foreach ($name as $n) {
  for ($x = 0; $x < count($mednames); $x++) {
    if (in_array($n, $mednames[$x])) {
      $duplicaten[] = $n;
      break;
    }
  }
}

$cnt = array_count_values($duplicaten);

print_r($cnt);
/* prints
Array
(
    [Ibuprofen] => 2
    [Penicillin] => 3
    [Adderall] => 1
)
*/


// again do foreach with $key => $value
foreach ($cnt as $name =>  $c) {
  if ($cnt > 1) {
    echo '<h2 style="color: gray;">';
    print($c);
    print('-');
    print($name);
    echo '</h2>';
  }
}
/* prints:
    <h2 style="color: gray;">2-Ibuprofen</h2><h2 style="color: gray;">3-Penicillin</h2><h2 style="color: gray;">1-Adderall</h2>
*/

You can test this in php Sandbox, that this is in fact correct behaviour:

https://sandbox.onlinephpfunctions.com/

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement