I am making a code that grab a url part, take values and then set on list.
Problem is on list
if (in_array($term->term_id, $regions)) {
$selected_region = 'selected';
}
else {
$selected_region = 'not';
}
This code does work, always return not.
if ($term->term_id == 2) {
$selected_region = 'varbut?';
}
This work.
$regions is variable and return this:
array(2) { [0]=> string(2) "17" [1]=> string(1) "2" }
Where is the problem to use $term_id?
$term_id with var_dump returns int(17) int(2)
On other pages, single page with only one term is working and code is this:
<?php
$id = get_the_ID();
$postterms = wp_get_post_terms($id, 'destinations'); // get post terms
$parentId = $postterms[0]->term_id; // get parent term ID
?>
<?php if (!in_array($parentId, $regions)): ?>
Why is not working on that selected function? int and string values?
Here is a full term get code:
$custom_terms = get_terms(array($taxonomies), $args);
foreach($custom_terms as $term){
if (in_array($term->term_id, $regions)) {
$selected_region = 'selected';
}
else {
$selected_region = 'not';
}
if ($term->term_id == 2) {
$selected_region = 'varbut?';
}
echo $selected_region;
var_dump($term->term_id);
}
}
It's like you said;
$regions : array(2) { [0]=> string(2) "17" [1]=> string(1) "2" }
$term->term_id : int(2)
How about converting your $regions to ints?
Related
I have an array in session.
array(2) {
[0]=> array(5) {
["id"]=> string(1) "3"
["titulo"]=> string(25) "product 1"
["quantidade"]=> int(1)
["preco"]=> string(7) "1000.00"
["image"]=> string(15) "/img/no_img.png"
}
[1]=> array(5) {
["id"]=> string(1) "1"
["titulo"]=> string(43) "product 2"
["quantidade"]=> int(1)
["preco"]=> string(6) "157.20"
["image"]=> string(14) "produtos/1.jpg"
}
}
for example, if user add the same product again (eg: id 3), I'd like to add +1 in its quantity (quantidade) only.
I tried this, but products are always creating a new array, not updating the quantity.
Any ideas why?
if(!empty($_SESSION["cart_item"])) {
if(in_array($produto, array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($produto == $k) {
if(empty($_SESSION["cart_item"][$k]["quantidade"])) {
$_SESSION["cart_item"][$k]["quantidade"] = 1;
}
$_SESSION["cart_item"][$k]["quantidade"] += 1;
}
}
}
else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
}
else {
$_SESSION["cart_item"] = $itemArray;
}
$produto is the ID I'd like to update.
The $k variable contains the current array index [0,1,2,3], not the value of the current array id.
I'd try to compare your $producto with $_SESSION["cart_item"][$k]['id'] instead. I've also changed your condition to strictly compare both values ( === ) which is always a good idea.
One more things.
I'm not sure this line is relevent
if(in_array($produto, array_keys($_SESSION["cart_item"]))) {
You are trying to check if $produto is in the keys of $_SESSION["cart_item"]. However, the keys of $_SESSION["cart_item"] are the index that starts at 0. So the id 1 might not be at position 1.
You'd be better of using another variable ( lets say $dirty) to check if your current array was updated.
if(!empty($_SESSION["cart_item"])) {
$dirty = false;
foreach($_SESSION["cart_item"] as $k => $v) {
if($produto === $_SESSION["cart_item"][$k]['id']) {
$dirty = true
if(empty($_SESSION["cart_item"][$k]["quantidade"])) {
$_SESSION["cart_item"][$k]["quantidade"] = 1;
}
$_SESSION["cart_item"][$k]["quantidade"] += 1;
}
}
if(!$dirty) {
// the id was not present in the array
// we need to add it.
$_SESSION["cart_item"][] = [...];
}
}
else {
$_SESSION["cart_item"] = $itemArray;
}
Please note that this code is untested.
My request PHP file elaborates some Ajax POST data:
POST data
data[0][id]:359
data[0][position]:1
data[1][id]:321
data[1][position]:2
data[2][id]:354
data[2][position]:3
Request.php
if(isset($_POST['data'])) {
if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
$verify = $loggedIn->verify();
if($verify['username']) {
$Profile = new Profile();
$Profile->db = $db;
//Call my function
$messages = $Profile->setOrder($_POST['data']);
}
}
}
Profile.php
function setOrder($post) {
var_dump($post);
foreach($post as $item)
{
return "Area ID ".$item["id"]." and person located ".$item["position"]."<br />";
}
}
My function returns nothing and the dump of $post is as below
array(3) {
[0]=>
array(2) {
["id"]=>
string(3) "359"
["position"]=>
string(1) "1"
}
[1]=>
array(2) {
["id"]=>
string(3) "321"
["position"]=>
string(1) "2"
}
[2]=>
array(2) {
["id"]=>
string(3) "354"
["position"]=>
string(1) "3"
}
}
Inside my function I can dump correctly something like var_dump($post[0]["id"]); so why my foreach loop is empty?
It is because you are using return inside loop. It will terminate the loop after first iteration. You need to do something like this.
$return = null;
foreach($data as $item)
{
$return .= "Area ID ".$item["id"]." and person located ".$item["position"]."<br />";
}
return $return;
I am trying to check if a value exists inside an foreach loop from a decoded json response and compare it to my own string. I need to set $response_array['status'] to "Allowed" if $domain_to_check value exists inside the $key_info['registered_domain'] array. I tried to use in_array php function to check if value exists, however i had no success and i keep getting back "Not Allowed - Domain not listed" response even when the value is inside the array. I think that the problem is with my foreach loop but for the sake of me i can't figure whats wrong.
$domain_to_check = 'domain-name.com';
$data = json_decode($returnCheckValue,true);
$key_response = $data['result'];
if ($key_response == 'success'){
foreach ($data['registered_domains'] as $key_domain_info) {
$key_listed_domain = $key_domain_info['registered_domain'];
if ($key_response == 'success' && in_array($domain_to_check, $key_listed_domain)) {
$response_array['status'] = 'Allowed';
}
else {
$response_array['status'] = 'Not Allowed - Domain not listed';
}
}
}
else {
$response_array['status'] = 'Not Allowed - Wrong Key';
}
echo json_encode($response_array);
Here is how my var_dump(); of the $data looks like
array(9) { ["result"]=> string(7) "success" ["max_allowed_domains"]=> string(1) "3" ["registered_domains"]=> array(2) { [0]=> array(5) { ["id"]=> string(2) "60" ["lic_key_id"]=> string(2) "51" ["lic_key"]=> string(13) "93248cqkdj21as" ["registered_domain"]=> string(19) "domain-name-2.com" ["item_reference"]=> string(1) "1" } [1]=> array(5) { ["id"]=> string(2) "58" ["lic_key_id"]=> string(2) "51" ["lic_key"]=> string(13) "93248cqkdj21as" ["registered_domain"]=> string(14) "domain-name.com" ["item_reference"]=> string(3) "443" } } }
Relate below code with your code. This code is working.
$domain_to_check = "domain-name.com";
$test = array("registered_domains" => array("registered_domain" => "domain-name-2.com"), array("registered_domain" => "domain-name.com"));
foreach($test as $val) {
if($val['registered_domain'] == $domain_to_check) {
$result = 'success';
break;
} else {
$result = 'failure';
}
}
echo $result;
Use php strpos
$domainStringFound = strpos($key_listed_domain, $domain_to_check);
if ($key_response == 'success' && $domainStringFound !== false) {
$response_array['status'] = 'Allowed';
}
i have array with database, and have to select only this items what have "tid" = 1
array(3) {
[1]=>
array(4) {
["tid"]=> "1"
["title"]=> "Google"
["url"]=> "http://google.com/"
["description"]=> "A very efficient search engine."
}
[2]=>
array(4) {
["tid"]=> "2"
["title"]=> "Facebook"
["url"]=> "http://facebook.com/"
["description"]=> "Trade securities, currently supports nearly 1000 stocks and ETFs"
}
[3]=>
array(4) {
["tid"]=> "1"
["title"]=> "Yandex"
["url"]=> "http://yandex.ru/"
["description"]=> "Another efficient search engine popular in Russia"
}
}
how can i select only this items from array what have "tid" = 1?
<?php
$final_arr = array();
foreach($tid_arrs as $tid_arr){
if($tid_arr['tid'] == 1){
$final_arr[] = $tid_arr;
}
}
print_r($final_arr);
?>
$filteredArray = array();
for($i = 0, $end = count($array);$i < $end;i++)
{
if($array[$i]["tid"] === "1")
{
$filderedArray[] = $array[$i];
}
}
That way $filteredArray will contain solely the items with tid 1;
Try array_filter function: http://php.net/manual/en/function.array-filter.php this should help.
print_r(array_filter($array, "filter_function"));
function filter_function($element){
return (int)$element['tid'] === 1;
}
let's say you starting array is $arr.
$result = array();
foreach ($arr as $arrItem) {
if ((array_key_exists('tid', $arrItem)) && ($arrItem['tid'] == "1")){
$result[] = $arrItem;
}
}
$result should be what you are excepted.
i am trying to sort out my array, just remove some duplicate elements etc... It all works great and looks like this when i output it
array(3) { ["addon_mat_3"]=> string(2) "15" ["addon_mat_7"]=> string(1) "7" ["addon_mat_15"]=> string(1) "9" }
The above is with this code
foreach ($new_shopping_list_array as $columnName => $columnData) {
if(is_numeric($columnName)){
unset($new_shopping_list_array[$columnName]);
}
if($columnName == 'addon_id'){
unset($new_shopping_list_array[$columnName]);
}
if($columnData == 0){
unset($new_shopping_list_array[$columnName]);
}
}
However, if i add the else as show below, which i need as it removes the first 10 characters from the array key, then i all of a sudden get a fourth element added to the array with key "0".
array(4) { [0]=> string(1) "9" [3]=> string(2) "15" [7]=> string(1) "7" [15]=> string(1) "9" }
This code
foreach ($new_shopping_list_array as $columnName => $columnData) {
if(is_numeric($columnName)){
unset($new_shopping_list_array[$columnName]);
}
if($columnName == 'addon_id'){
unset($new_shopping_list_array[$columnName]);
}
if($columnData == 0){
unset($new_shopping_list_array[$columnName]);
}else{
$new_columnName = substr($columnName, 10);
unset($new_shopping_list_array[$columnName]);
$new_shopping_list_array[$new_columnName] = $columnData;
}
}
Everything else is great, apart from that fourth element added, what am i doing wrong,
Thanks for any and all help
That code should work, but you could do the whole thing in a few less lines:
$flipped = array_flip($new_shopping_list_array);
foreach ($flipped as $value => &$key) {
if($key == "addon_id" || is_numeric($key) || $value == 0) {
unset($flipped[$value]);
} else {
$key = substr($key,10);
}
}
$new_shopping_list_array = array_flip($flipped);