Hi to all!
May be it's a very very stupid question, but I am stuck on it...
So my problem is the second - I have array

$pacient[$val['pacientid']][] = $val['f_name']." ".$val['s_name'];

And I want to compare values in this array, so how I go throught array:

$pacient = array_unique($pacient);
                    foreach($pacient as $key_pk=>$pac)
                    {
                        $pac = array_unique($pac);
                        var_dump($pac);
                    }

and I got this var_dump():

array(1) {
  [0]=>
  string(16) "anna lukaševica"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}
array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}

So I need to compare those strings between them, i.e if(string1 != string2) {//do something}
But I can't get array value for compare.. or I am missing somthing in my logic?

Please help me!

Hi,

do you want to compare this:

array(2) {
  [0]=>
  string(16) "anna lukaševica"
  [1]=>
  string(17) "anna lukashevicha"
}

between themselves, i.e. array[0] != array[1], or against the other resulting arrays? And once you get a match, which result you want to achieve? Also, by showing how you generate this array, maybe we can suggest a better approach.

I want to compare all results between themselves, and if they matches, I want to show only one string.
So from my example it will be look like this:

anna lukaševica
anna lukashevicha(changed)

It's searching for schanged surname for one person...

Also I don't know how many results is in array, because it is dinamyc

Ok, I assume this is created by an SQL query:

$pacient[$val['pacientid']][] = $val['f_name']." ".$val['s_name'];

and that the pacientid is the same for the surnames you want to compare, so this array will create a group of names like this:

Array
(
    [1] => Array
        (
            [0] => anna lukaševica
            [1] => anna lukashevicha
            [2] => anna lukaševica
        )
)

Correct? If yes, the check could be done at database level. Anyway, by changing your loop, maybe you can get the expected result:

<?php

$values = array(
    array(
            'pacientid' => 1,
            'f_name'    => 'anna',
            's_name'    => 'lukaševica',
        ),
    array(
            'pacientid' => 1,
            'f_name'    => 'anna',
            's_name'    => 'lukashevicha',
        ),
    array(
            'pacientid' => 1,
            'f_name'    => 'anna',
            's_name'    => 'lukaševica',
        ),

    );

$pacient = array();
foreach($values as $key => $val)
{
    $pacient[$val['pacientid']][] = $val['f_name']." ".$val['s_name'];
}

$result = array();
foreach($pacient as $pac)
{
    $result[] = array_unique($pac);
}

print_r($result);

Will output:

Array
(
    [0] => Array
        (
            [0] => anna lukaševica
            [1] => anna lukashevicha
        )

)

if you're processing only one pacientid per time, then you do not need the extra array given by $result:

$result = '';

foreach($pacient as $pac)
{
    $result = array_unique($pac);
}

And you get:

Array
(
    [0] => anna lukaševica
    [1] => anna lukashevicha
)

At this point you can count the elements in the array; having more than one result will be the same of performing if(string1 != string2), so:

if(count($result) > 1)
{
    //register the surname variation
}

Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.