GCiri
GCiri

Reputation: 59

Count how many char in array numbers

I've an array got with preg_match_all. This array contain 4 values and each value is a number. I want to display ONLY the value that have 10 or more character. Example:

Array_Value1 = 1234567890
Array_Value2 = 01234
Array_Value3 = 449125
Array_Value4 = 991234581210

I want to show with an echo only Array_Value1 and Array_Value4 because formed of 10 or more characters! How can I do it?

I tried with count_char or str_len but I see a message that say "Array to String conversion".

Anyone can help me?

Upvotes: 0

Views: 2198

Answers (4)

thicolares
thicolares

Reputation: 295

Try this:

<?php
    $values = array(123123123, 53453453452345, 123, 9979797979797979);
    $string = implode(',',$values);
    preg_match_all('/(\d{10,})/', $string, $matches);
    print_r($matches[1]);
?>

Maybe it's more time-consuming... hehe

Upvotes: 0

Not Amused
Not Amused

Reputation: 962

foreach ($values as $value)
{
    if (strlen((string)$value) >= 10)
        echo $value;
}

Upvotes: 1

rvandoni
rvandoni

Reputation: 3397

first of all you have to put all your values inside an array:

$array = array(1234567890, 01234, 449125, 991234581210);

after it you can use a simple foreach:

foreach($array as $value) {

if(strlen((string)$value) >= 10)
 echo $value;
}

you should add a separator after each echo, or you will have an output like

1234567890991234581210

Upvotes: 2

Sahil Mittal
Sahil Mittal

Reputation: 20753

Try this-

foreach($my_arr as $val)
{
    if(strlen((string)$val) >=10)
       // more than or equal to 10
    else
       // less than 10
}

Upvotes: 0

Related Questions