Reputation: 1589
I have tried array_merge and array_unique but it couldn't helped me out.
Here is my code:
public function syncContacts() {
$data['data'] = $this->main_manager->select_all('users'); # get all user data
$info[0] = [
"contact_name" => "a",
"number" => "031651651"
];
$info[1] = [
"contact_name" => "b",
"number" => "+923402382972"
];
$info[2] = [
"contact_name" => "c",
"number" => "31651651"
];
$info[3] = [
"contact_name" => "d",
"number" => "6165165123323"
];
$info[4] = [
"contact_name" => "e",
"number" => "316516512113"
];
for ($i = 0; $i < count($info); $i++) { # info array loop
if (substr($info[$i]['number'], 0, 1) == "+") { # finds + sign in numbers
for ($x = 0; $x < count($data['data']); $x++) { # user data array loop
if (
$info[$i]['number'] == $data['data'][$x]['country_code'] . $data['data'][$x]['phone_number'] # first condition
|| $info[$i]['number'] == $data['data'][$x]['country_code'] . 0 . $data['data'][$x]['phone_number'] # second condition
) {
$finalData['data']['registered'][$i]['name'] = $info[$i]['contact_name'];
$finalData['data']['registered'][$i]['number'] = $info[$i]['number'];
} else {
$finalData['data']['unregistered'][$i]['name'] = $info[$i]['contact_name'];
$finalData['data']['unregistered'][$i]['number'] = $info[$i]['number'];
$result = array_unique(array_merge($finalData['data']['unregistered'][$i], $info[$i]));
//
}
} # end of user data array loop
} else { # finds 0 in numbers
for ($x = 0; $x < count($data['data']); $x++) { # user data array loop
if (
$info[$i]['number'] == $data['data'][$x]['phone_number'] # first condition
|| 0 . $info[$i]['number'] == $data['data'][$x]['phone_number'] # second condition
|| $info[$i]['number'] == 0 . $data['data'][$x]['phone_number'] # third condition
) {
$finalData['data']['registered'][$i]['name'] = $info[$i]['contact_name'];
$finalData['data']['registered'][$i]['number'] = $info[$i]['number'];
} else {
$finalData['data']['unregistered'][$i]['name'] = $info[$i]['contact_name'];
$finalData['data']['unregistered'][$i]['number'] = $info[$i]['number'];
$result = array_unique(array_merge($finalData['data']['unregistered'][$i], $info[$i]));
// $result = array_merge($finalData['data']['unregistered'][$i], $info[$i]);
// $finalData['data']['unregistered'][$i] = array_unique($result);
}
} # end of user data array loop
}
} #end of info array loop
print_r($finalData);
die();
}
I am making a sync contacts webservice.
In $data array I am getting data from users table and I have 8 record in it. 3 record matched from my given array which is $info and two are unmatched. I made an array which is $finalData['data'].
I have to separate the record in registered and unregistered key of $finalData array. I have separated matched numbers in registered key of an array but couldn't separate the correct unmatched numbers in unregistered key of the array.
I am getting this result:
Array
(
[data] => Array
(
[unregistered] => Array
(
[0] => Array
(
[name] => a
[number] => 031651651
)
[1] => Array
(
[name] => b
[number] => +923402382972
)
[2] => Array
(
[name] => c
[number] => 31651651
)
[3] => Array
(
[name] => d
[number] => 6165165123323
)
[4] => Array
(
[name] => e
[number] => 316516512113
)
)
[registered] => Array
(
[0] => Array
(
[name] => a
[number] => 031651651
)
[1] => Array
(
[name] => b
[number] => +923402382972
)
[2] => Array
(
[name] => c
[number] => 31651651
)
)
)
)
Upvotes: 0
Views: 96
Reputation: 780
Match index of first array with another and unset the matched index and then you will get the unregistered numbers.
Upvotes: 1
Reputation: 19
How about using a hash like array? See example here http://php.net/manual/en/language.types.array.php
Upvotes: 1