0

I have a two dimensional array that is really large and varying but structured like this:

array 
  0 => 
    array 
      'id' => string '6' 
      'name' => string 'Adams Clasp' 
  1 => 
    array 
      'id' => int 185
      'name' => string 'Acrylic'
  2 => 
    array
      'id' => string '268' 
      'name' => string 'Adams Clasp (Splints)' 
  3 => 
    array 
      'id' => int 11
      'name' => string 'Arrow Clasp'
  4 => 
    array 
      'id' => int 11
      'name' => string 'Arrow Clasp' 
  5 => 
    array 
      'id' => string '0' 
      'name' => string 'Labial Bow' 
  6 => 
    array 
      'id' => string '6' 
      'name' => string 'Adams Clasp' 
  7 => 
    array 
      'id' => string '6' 
      'name' => string 'Adams Clasp'

I would like to get output that is similar to as if array_count_values() were called on the array if it were not multidimensional/ just keys and name except retaining the associated 'id' as a value and remain multidimensional. In this example output should look like:

array 
  'Adams Clasp' => 
       array 
         'id' => string '6' 
         'count' => int 3
  'Acrylic' => 
       array 
         'id' => int 185
         'count' => int 1
  'Adams Clasp (Splints)' => 
       array
         'id' => string '268' 
         'count => int 1
  'Arrow Clasp' => 
       array 
         'id' => int 11
         'count' => int 2
  'Labial Bow' => 
       array 
         'id' => string '0' 
         'count' => int 1

Am running php5.6

2 Answers 2

1

A properly constructed foreach loop will do the trick. When a name is first found, set it as a key, and declare a default subarray for it (id and count = 0). Then just increment the count value for every occurrence.

Code: (Demo)

$array=[ 
    ['id'=>'6','name'=>'Adams Clasp'],
    ['id'=>185,'name'=>'Acrylic'],
    ['id'=>'268','name'=>'Adams Clasp (Splints)'],
    ['id'=>11,'name'=>'Arrow Clasp'],
    ['id'=>11,'name'=>'Arrow Clasp'],
    ['id'=>'0','name'=>'Labial Bow'],
    ['id'=>'6','name'=>'Adams Clasp'],
    ['id'=>'6','name'=>'Adams Clasp']
];

foreach($array as $a){
    if(!isset($result[$a['name']])){
        $result[$a['name']]=['id'=>$a['id'],'count'=>0];
    }
    ++$result[$a['name']]['count'];
}
var_export($result);

Output:

array (
  'Adams Clasp' => 
  array (
    'id' => '6',
    'count' => 3,
  ),
  'Acrylic' => 
  array (
    'id' => 185,
    'count' => 1,
  ),
  'Adams Clasp (Splints)' => 
  array (
    'id' => '268',
    'count' => 1,
  ),
  'Arrow Clasp' => 
  array (
    'id' => 11,
    'count' => 2,
  ),
  'Labial Bow' => 
  array (
    'id' => '0',
    'count' => 1,
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

1

Variant uses arrays functions :

$array = [
    ['id' => '6','name' => 'Adams Clasp'],
    ['id' => 185,'name' => 'Acrylic'],
    ['id' => '268','name' => 'Adams Clasp (Splints)'],
    ['id' => 11,'name' => 'Arrow Clasp'],
    ['id' => 11,'name' => 'Arrow Clasp'],
    ['id' => '0','name' => 'Labial Bow'],
    ['id' => '6','name' => 'Adams Clasp'],
    ['id' => '6','name' => 'Adams Clasp'],

];
$ids = array_column($array , 'name' , 'id');

array_map(function($count , $id , $name)use(&$out){
    $out[$name] = ['id' =>  $id, 'count' => $count];
} , array_count_values(array_column($array , 'name')) , array_keys($ids),array_values($ids));

print_r($out);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.