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';
}
Related
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 had this response from method post
array(7) {
["enable"]=>
array(2) {
[0]=>
string(2) "on"
[1]=>
string(2) "on"
}
["value"]=>
array(2) {
[0]=>
string(8) "R$ 10,00"
[1]=>
string(8) "R$ 10,00"
}
["zip_code"]=>
array(2) {
[0]=>
string(9) "57200-970"
[1]=>
string(9) "57200-990"
}
["address"]=>
array(2) {
[0]=>
string(28) "Avenida Floriano Peixoto"
[1]=>
string(33) "Povoado Tabuleiro dos Negros"
}
["neighborhood"]=>
array(2) {
[0]=>
string(6) "Centro"
[1]=>
string(4) "Bairro Vermelho"
}
["city"]=>
array(2) {
[0]=>
string(6) "Penedo"
[1]=>
string(6) "Penedo"
}
["state"]=>
array(2) {
[0]=>
string(2) "AL"
[1]=>
string(2) "AL"
}
}
I need first use the foreach to get the $_POST['active'] and get value from another arrays index
foreach (Request::post('enable') as $k => $v) {
print Request::post(array("zip_code", $k)) . "\n";
// I hope same result of $_POST['zip_code'][$k]
}
the real problem is my function
public static function post($key, $clean = false) {
$result = is_array($key) ? array_search($key, $_POST) : $_POST[$key];
if (!empty($result)) {
return ($clean) ? trim(strip_tags($result)) : $result;
}
return NULL;
}
if param key is an array get value from this key array
for example $_POST['a']['b']['c']
[EDIT]
I improve the function, thanks #mickmackusa
public static function post($key, $clean = false) {
$focus = $_POST;
if (is_array($key)) {
foreach ($key as $k) {
if (!isset($focus[$k])) {
return NULL;
}
$focus = $focus[$k];
if (!is_array($focus)) {
$result = $focus;
}
}
} else {
$result = empty($focus[$key]) ? NULL : $focus[$key];
}
return ($clean ? trim(strip_tags($result)) : $result);
}
I think line1 inside post() is fouling things up. Depending on $key's type, you are declaring $result's value as a key or a value. But then seemingly treating both possibilities as a value in the next condition and returning it.
public static function post($key, $clean=false) {
$result = is_array($key) ? array_search($key, $_POST) : $_POST[$key];
// if $key is an array, then $result is the key of the found value within $POST or FALSE if not found
// if $key is not an array, then $result is the value of $_POST[$key]
if (!empty($result)) {
return ($clean) ? trim(strip_tags($result)) : $result;
}
return NULL;
}
The !empty condition is checking for NOT falsey values in $result.
See all falsey values # empty() manual, including 0 which can be a totally legitimate index/key, and more.
Instead, I think you want to use:
public static function post($key,$clean=false){
$focus=$_POST;
foreach($key as $k){
if(!isset($focus[$k])){
return NULL;
}else{
$focus=$focus[$k];
if(!is_array($focus)){
$result=$focus;
}
}
}
return ($clean?trim(strip_tags($result)):$result);
}
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?
Suppose I have a JSON string:
$json = '{"lemon":"test",
"orange":["toto", "tata", "tete"],
"zob":[{"id":"0"}, {"id":"1"}]}';
I'd like to cycle through that encoded object to modify every string in it, so I have a recursive function:
function doObject($__obj){
$__obj = cycleObject($__obj);
return $__obj;
}
function cycleObject($__obj){
$type = gettype($__obj);
foreach($__obj as $var => &$val){
switch(gettype($val)){
case 'object':
cycleObject($val);
break;
case 'array':
cycleObject($val);
break;
case 'string':
if($type == 'object'){
$__obj->$var = $val.'-ok';
}else{
if($type == 'array'){
$__obj[$var] = $val.'-ok';
}
}
break;
}
}
return $__obj;
}
And I call the function:
$obj = doObject(json_decode($json));
var_dump($obj);
Which gives :
object(stdClass)#1 (3) {
["lemon"]=> string(7) "test-ok"
["orange"]=> array(3) {
[0]=> string(4) "toto"
[1]=> string(4) "tata"
[2]=> string(4) "tete" }
["zob"]=> array(2) {
[0]=> object(stdClass)#2 (1) {
["id"]=> string(4) "0-ok" }
[1]=> object(stdClass)#3 (1) {
["id"]=> string(4) "1-ok" }
}
}
Now my problem is, for some reason, I am unable to modify directly inside an array composed by string, or should I say, the modified string inside an array (and not inside an object inside an array) because the array loses its reference. How do I fix that so in orange I instead obtain:
[0]=> string(7) "toto-ok"
[1]=> string(7) "tata-ok"
[2]=> string(7) "tete-ok"
Your array of strings isn't being scrutinized correctly by your function. Basically, in each array you need a second check to see if you are dealing with another array/object or a string, otherwise regular arrays of strings are being bypassed....oddly enough. The following should work for you:
$json = '{"lemon":"test",
"orange":["toto", "tata", "tete"],
"zob":[{"id":"0"}, {"id":"1"}]}';
function doObject($__obj){
$__obj = cycleObject($__obj);
return $__obj;
}
function cycleObject($__obj){
foreach($__obj as $key => &$val){
if(is_object($val)) {
cycleObject($val);
}
if(is_array($val)) {
foreach($val as &$v) {
if(is_object($v) || is_array($v)) {
cycleObject($v);
} else {
$v .= '-ok';
}
}
}
if(is_string($val)) {
$val .= '-ok';
}
}
return $__obj;
}
$obj = doObject(json_decode($json));
var_dump($obj);
This produced the results you were looking for in my local environment.
object(stdClass)#1 (3) {
["lemon"]=>
string(7) "test-ok"
["orange"]=>
array(3) {
[0]=>
string(7) "toto-ok"
[1]=>
string(7) "tata-ok"
[2]=>
string(7) "tete-ok"
}
["zob"]=>
array(2) {
[0]=>
object(stdClass)#2 (1) {
["id"]=>
string(4) "0-ok"
}
[1]=>
object(stdClass)#3 (1) {
["id"]=>
string(4) "1-ok"
}
}
}
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);