I'm trying to add 4 arrays into one array ($all_prices
) and then check the values of each key in each individual array against $all_prices
to make sure that they are unique. If they are not I want to add a 0
to the end of if to make it unique (so 0.50
becomes 0.500
).
For some reason I'm getting the following error, despite the fact that I already changed the data type from decimal to varchar:
array_count_values(): Can only count STRING and INTEGER values!
Edit Here is a snippet from dd($all_prices)
array(4) { [0]=> array(9) { ["14.45"]=> string(8) "sample 1" ["12.40"]=>
string(8) "sample 2" ["14.13"]=> string(8) "sample 3" ["15.11"]=>
string(8) "sample 4"
Code:
$all_prices = [$list_a_prices, $list_b_prices, $list_c_prices, $list_d_prices];
$price_count = array_count_values($all_prices);
foreach($list_b_prices as $key => $value){
if($price_count[$key] >= 2){
$key . "0";
}
}
Where am I going wrong? Is there is a way to leave the data type as Decimal?
var_dump($all_prices);
?array_count_values()
requires a single-dimensional array, but you're supplying a 2-dimensional array. The function can't search inside arrays — you need to flatten the array first. So create a new array containing all the values you need, and then apply the function, and it should work.