Home > Software engineering >  Target index of array
Target index of array

Time:12-06

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.

CodePudding user response:

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/

CodePudding user response:

You can access array's key in the foreach as follows,

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

Read more about foreach

CodePudding user response:

You can itearate your result array again like this.

$m = array ( 
  array ( 'name' => 'xxxx1', 'med_name' => 'Ibuprofen', ), 
  array ( 'name' => 'xxxx2', 'med_name' => 'Ibuprofen', ), 
  array ( 'name' => 'xxxx5', 'med_name' => 'Ibuprofen', ), 
  array ( 'name' => 'xxxx3', 'med_name' => 'Penicillin', ), 
  );

$export = array ( 'Ibuprofen' => 2, 'Penicillin' => 3, 'Adderall' => 1, );
$newM = [];
foreach($m as $i) {
    if (! isset($newM[$i['med_name']])) {
        $_counting = 0; 
    }
    $_counting  ;
    $newM[$i['med_name']] = $i;
    $newM[$i['med_name']]['count'] = $export[$i['med_name']];
    $newM[$i['med_name']]['counting'] = $_counting;
    
}

print_r($newM);

output

Array
(
    [Ibuprofen] => Array
        (
            [name] => xxxx5
            [med_name] => Ibuprofen
            [count] => 2
            [counting] => 3
        )

    [Penicillin] => Array
        (
            [name] => xxxx3
            [med_name] => Penicillin
            [count] => 3
            [counting] => 1
        )

)
  •  Tags:  
  • php
  • Related