Bill Gates - 5 months ago 12

PHP Question

I've an array with following fields name and count.

I would like to give dynamic height and width depending upon the count of array with PHP or Javascript.

I've predefine maximum and minimum height also so just need a function which can return height for all.

For example my array is :

`$arr = array(`

array('name'=>'a','count'=>'2'),

array('name'=>'b','count'=>'3'),

array('name'=>'c','count'=>'1'),

array('name'=>'d','count'=>'2')

);

a - 2

b - 3

c - 1

d - 2

Let's assume my min. height is 110 and max. height is 150

so c is smallest so its height will be 110

and b is largest so its height will be 150

and a and d have same value so should be same height as 130 between both

and it should be dynamic so can work for any no. of count and any no. variables.

How can I do all this with 1 function ?

Please suggest some link or something

Answer

This code calculate height on base of value of count item

```
$arr = array(
array('name'=>'a','count'=>'2'),
array('name'=>'b','count'=>'3'),
array('name'=>'c','count'=>'1'),
array('name'=>'d','count'=>'2')
);
$cnt = array_column($arr, 'count');
$cnt_min = min($cnt);
$cnt_max = max($cnt);
$min = 110;
$max = 150;
foreach($arr as &$item)
$item['heaight'] = $min + floor(($item['count']-$cnt_min)*($max-$min)/($cnt_max-$cnt_min));
print_r($arr);
```

Source (Stackoverflow)

Comments