Skip adding to array if it exists in PHP
up vote
2
down vote
favorite
I am using in_array
function to skip adding the same addresses to an array.
Code:
$addresses_list = array();
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
if (!in_array($row, $addresses_list)) {
array_push($addresses_list, $row);
}
}
I want to skip adding to the arrays if $row['address']
is the same as an address of $addresses_list['address']
.
php arrays
add a comment |
up vote
2
down vote
favorite
I am using in_array
function to skip adding the same addresses to an array.
Code:
$addresses_list = array();
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
if (!in_array($row, $addresses_list)) {
array_push($addresses_list, $row);
}
}
I want to skip adding to the arrays if $row['address']
is the same as an address of $addresses_list['address']
.
php arrays
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am using in_array
function to skip adding the same addresses to an array.
Code:
$addresses_list = array();
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
if (!in_array($row, $addresses_list)) {
array_push($addresses_list, $row);
}
}
I want to skip adding to the arrays if $row['address']
is the same as an address of $addresses_list['address']
.
php arrays
I am using in_array
function to skip adding the same addresses to an array.
Code:
$addresses_list = array();
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
if (!in_array($row, $addresses_list)) {
array_push($addresses_list, $row);
}
}
I want to skip adding to the arrays if $row['address']
is the same as an address of $addresses_list['address']
.
php arrays
php arrays
edited yesterday
William Jones
6110
6110
asked yesterday
Pardis Ak
327
327
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can create a temporary variable to store the value of address
already stored in the $addresses_list
array. Whenever, you are storing a $row
in the $addresses_list
array, you can store the address
key value in the temporary variable.
Now, everytime you need to check against the temporary variable, whether the key value already exists or not.
Note that even PHP documentation recommends using operator against
array_push()
. So I have changed your code to use instead.
Note: If you use array_push() to add one element to the array, it's
better to use $array = because in that way there is no overhead of
calling a function.
Try below (explanation in comments):
$addresses_list = array();
$temp_addresses = array(); // Create a temp array
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// check in temporary variable instead
if (!in_array($row['address'], $temp_addresses)) {
// add to temp array to avoid duplicate entry in next loop
$temp_addresses = $row['address'];
$addresses_list = $row;
}
}
If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY
on address
. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.
$addresses_list = array();
// It is a good practice to have query string in a separate variable
$sql = "SELECT lat,lng,address
FROM api_order
where userid=683
GROUP BY address;"
$stmt_select_address_result = $databaseManager->connect()->prepare($sql);
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// No need for any checks, as you are getting unique addresses only
$addresses_list = $row;
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can create a temporary variable to store the value of address
already stored in the $addresses_list
array. Whenever, you are storing a $row
in the $addresses_list
array, you can store the address
key value in the temporary variable.
Now, everytime you need to check against the temporary variable, whether the key value already exists or not.
Note that even PHP documentation recommends using operator against
array_push()
. So I have changed your code to use instead.
Note: If you use array_push() to add one element to the array, it's
better to use $array = because in that way there is no overhead of
calling a function.
Try below (explanation in comments):
$addresses_list = array();
$temp_addresses = array(); // Create a temp array
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// check in temporary variable instead
if (!in_array($row['address'], $temp_addresses)) {
// add to temp array to avoid duplicate entry in next loop
$temp_addresses = $row['address'];
$addresses_list = $row;
}
}
If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY
on address
. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.
$addresses_list = array();
// It is a good practice to have query string in a separate variable
$sql = "SELECT lat,lng,address
FROM api_order
where userid=683
GROUP BY address;"
$stmt_select_address_result = $databaseManager->connect()->prepare($sql);
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// No need for any checks, as you are getting unique addresses only
$addresses_list = $row;
}
add a comment |
up vote
2
down vote
accepted
You can create a temporary variable to store the value of address
already stored in the $addresses_list
array. Whenever, you are storing a $row
in the $addresses_list
array, you can store the address
key value in the temporary variable.
Now, everytime you need to check against the temporary variable, whether the key value already exists or not.
Note that even PHP documentation recommends using operator against
array_push()
. So I have changed your code to use instead.
Note: If you use array_push() to add one element to the array, it's
better to use $array = because in that way there is no overhead of
calling a function.
Try below (explanation in comments):
$addresses_list = array();
$temp_addresses = array(); // Create a temp array
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// check in temporary variable instead
if (!in_array($row['address'], $temp_addresses)) {
// add to temp array to avoid duplicate entry in next loop
$temp_addresses = $row['address'];
$addresses_list = $row;
}
}
If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY
on address
. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.
$addresses_list = array();
// It is a good practice to have query string in a separate variable
$sql = "SELECT lat,lng,address
FROM api_order
where userid=683
GROUP BY address;"
$stmt_select_address_result = $databaseManager->connect()->prepare($sql);
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// No need for any checks, as you are getting unique addresses only
$addresses_list = $row;
}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can create a temporary variable to store the value of address
already stored in the $addresses_list
array. Whenever, you are storing a $row
in the $addresses_list
array, you can store the address
key value in the temporary variable.
Now, everytime you need to check against the temporary variable, whether the key value already exists or not.
Note that even PHP documentation recommends using operator against
array_push()
. So I have changed your code to use instead.
Note: If you use array_push() to add one element to the array, it's
better to use $array = because in that way there is no overhead of
calling a function.
Try below (explanation in comments):
$addresses_list = array();
$temp_addresses = array(); // Create a temp array
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// check in temporary variable instead
if (!in_array($row['address'], $temp_addresses)) {
// add to temp array to avoid duplicate entry in next loop
$temp_addresses = $row['address'];
$addresses_list = $row;
}
}
If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY
on address
. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.
$addresses_list = array();
// It is a good practice to have query string in a separate variable
$sql = "SELECT lat,lng,address
FROM api_order
where userid=683
GROUP BY address;"
$stmt_select_address_result = $databaseManager->connect()->prepare($sql);
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// No need for any checks, as you are getting unique addresses only
$addresses_list = $row;
}
You can create a temporary variable to store the value of address
already stored in the $addresses_list
array. Whenever, you are storing a $row
in the $addresses_list
array, you can store the address
key value in the temporary variable.
Now, everytime you need to check against the temporary variable, whether the key value already exists or not.
Note that even PHP documentation recommends using operator against
array_push()
. So I have changed your code to use instead.
Note: If you use array_push() to add one element to the array, it's
better to use $array = because in that way there is no overhead of
calling a function.
Try below (explanation in comments):
$addresses_list = array();
$temp_addresses = array(); // Create a temp array
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// check in temporary variable instead
if (!in_array($row['address'], $temp_addresses)) {
// add to temp array to avoid duplicate entry in next loop
$temp_addresses = $row['address'];
$addresses_list = $row;
}
}
If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY
on address
. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.
$addresses_list = array();
// It is a good practice to have query string in a separate variable
$sql = "SELECT lat,lng,address
FROM api_order
where userid=683
GROUP BY address;"
$stmt_select_address_result = $databaseManager->connect()->prepare($sql);
$stmt_select_address_result->execute();
while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC)) {
// No need for any checks, as you are getting unique addresses only
$addresses_list = $row;
}
edited yesterday
answered yesterday
Madhur Bhaiya
14.1k52035
14.1k52035
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237974%2fskip-adding-to-array-if-it-exists-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password