# php - Can you make the numbers? My code outputs correct but isn't accepted?

Get the solution ↓↓↓The question I'm working on is:

You are given an array representing the number of 0s, 1s, 2s, ..., 9s you have. The function will look like:

can_build([#0s, #1s, #2s, ..., #9s], [num1, num2, ...])

Write a function that returns true if you can build the following numbers using only the digits you have.

Example:canBuild([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [3]) ➞ false

I basically wrote:

```
function canBuild($digits, $arr) {
if(array_unique($digits) === 0 && $arr === []) {
return true;
} elseif ($arr === [] && array_unique($digits) !== 0) {
return false;
}
$result = [];
$test = [];
for($i = 0; $i < count($arr); $i++) {
$test[$i] = str_split($arr[$i]);
}
$digit = array_unique($digits);
$results = [];
foreach($test as $k => $v) {
for($j = 0; $j < count($test[$k]); $j++) {
if(in_array($v[$j], $digit)) {
array_push($results, "true");
} else {
array_push($results, "false");
}
}
}
if(array_unique($results) === "true") {
return true;
} else {
return false;
}
}
canBuild([1, 1, 0, 0, 0, 0, 0, 0, 1, 0], [1, 80, 0]); // false
```

But for some reason I keep getting: **"CanBuildNumbersUsingDigits tests FAILED: Failed asserting that false is true.""** Can anyone see something wrong with my code? Thanks.

### Answer

#### Solution:

Looking at your code:

```
array_unique($results) === "true"
```

and

```
array_unique($digits) !== 0
```

are semantically incorrect.`array_unique()`

would return a value of type *array* which can never be equal to any scalar value or string since you are doing a strict comparison.

Moreover, your code doesn't account for count of digits but rather checks if count of a digit is present in the array.

**Solution:**

To solve this problem, you just have to loop over the`$arr`

array of numbers,`str_split`

them into individual characters(like you did) and keep decrementing these string digits from actual count of each digit given in`$digits`

. If any digit count happens to be less than zero, meaning we don't have that much count of that digit, we return`false`

since we can't *form* that number. If all is well, we return`true`

in the end.

```
function canBuild($digits, $arr) {
foreach($arr as $num){
$chars = str_split($num);
foreach($chars as $char){
$digits[intval($char)]--;
if($digits[intval($char)] < 0) return false;
}
}
return true;
}
```

**Share solution ↓**

### Additional Information:

**Date the issue was resolved:**

Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org

### Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

### Similar questions

Find the answer in similar questions on our website.

### Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.