0

Here is the situation: There are thousand of data. But not all of that are unique. The first foreach loop is unique. So, I am putting the size in a Array . Next loop might have same size. So, I am checking the size of a variable. If the size found in the Array , that means its not unique otherwise the size of that variable would be in the Array. The problem is I'm getting common and unique (both) size in the Array.

PHP Code:

$counter = array();
foreach ($result_all as $data){
    $message =  $data['msg'];
    $size_of_message = strlen($message);

    if(contains($message,$chittagong)){
       if(empty($counter)){
           $counter[] = $size_of_message;
       }else{
           foreach($counter as $a) {
               if ($size_of_message !== $a)
                   $counter[] = $size_of_message;
           }
       }
    }
}

Result:

Array
(
    [0] => 153
    [1] => 122
    [2] => 165
    [3] => 165
)

The result I am expecting:

Array
(
    [0] => 153
    [1] => 122
    [2] => 165
)
2
  • please provide input Commented Oct 24, 2018 at 12:45
  • To know if a value is already in your array, use this method : php.net/manual/fr/function.in-array.php No need to manually loop through like you tried to do Commented Oct 24, 2018 at 12:47

4 Answers 4

0

The problem lies here:

           foreach($counter as $a) {
               if ($size_of_message !== $a)
                   $counter[] = $size_of_message;
           }

You are comparing $size_of_message with every existing element, and for most of them if statement will return true, adding new element to counter. And you want to add it only if no element matches it. So you need to use in_array() function instead of foreach:

if (!in_array($size_of_message, $counter) {
  $counter[] = $size_of_message;
}

In this case you also don't need to check if array is empty.

Sign up to request clarification or add additional context in comments.

1 Comment

Amazing! Your solution just worked! Thanks a lot!
0

You can easily use the native function array_unique

$array = [0=>153, 1=>122, 2=>165, 3=>165];
$array_unique = array_unique($array);
print_r($array_unique);

1 Comment

I found the solution! I didn't know about Array Unique, It seems this function works too. Thank you.
0

Try

$counter = array();
foreach ($result_all as $data){
    $message =  $data['msg'];
    $size_of_message = strlen($message);
    if(contains($message,$chittagong)){
       if(empty($counter)){
           $counter[] = $size_of_message;
       }
       if(!empty($counter)){
           foreach($counter as $a) {
               if ($size_of_message !== $a)
                   $counter[] = $size_of_message;
           }
       }
    }
}

or use Array Unique after foreach

1 Comment

I found the solution! I didnt know about Array Unique, seems this function works too. Thanks btw.
0

Maybe using array keys unicity is an idea :

$result = array();
foreach ($result_all as $data){
    $result[strlen($data['msg'])] = true;
}
$result = array_keys($result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.