PHP variable unexpected result - php

I have the following code:
include_once("../content/includes/connect.php");
include_once("_functions.php");
//TODO: support sending variables
$check = true;
$callback = "error";
foreach ($_GET as $key => $value) {
echo "Key: {$key}<br>";
echo "Value: {$value}<br>";
echo "<pre>";
print_r(checkRules("register", $key, $value));
echo "</pre>";
list($pass, $errormessage) = checkRules("register", $key, $value);
echo "Pass: {$pass}<br>";
echo "Errormessage: {$errormessage}<br><br>";
if (!$pass) {
$check = false;
$callback = "error";
break;
}
}
if ($check) {
$callback = "register_success";
}
echo json_encode(array(
"callback" => $callback
));
SQL::close();
And this gives me the following HTML page:
Key: email
Value: a#a.aa
Array
(
[pass] => 1
[errormessage] =>
)
Pass:
Errormessage:
{"callback":"error"}
Now I do not get why the list($pass, $errormessage) = checkRules("register", $key, $value); does not work, when I clearly see that with print_r() it has the results.

list(...) = array assignment only works when the array is indexed, but checkRules returns an associative array.
You need to write:
$result = checkRules("register", $key, $value);
$pass = $result['pass'];
$errormessage = $result['errormessage'];
or change checkRules to return an indexed array (not my preference -- indexed arrays should only be used for uniform data).
You could also write:
list($pass, $errormessage) = array_values(checkRules("register", $key, $value));
but I think it's generally poor practice to depend on the order of elements in an associative array.

You're returning an array with string keys. You need to return an array with numeric indices instead, as list expects.
checkRules should be returning [1, ""], not array("pass" => 1, "errormessage" => "").

Change line like this:
list($pass, $errormessage) = array_values(checkRules("register", $key, $value));
list can't work with associative arrays :)

Related

Return doesn't work but print works

I'm trying to search in multidimensional array and get the value but return doesn't work.
function search($arr,$q){
foreach($arr as $key => $val){
if(trim($key) == $q) return $arr;
else if(is_array($val)) search($val,$q);
}
}
But echo and print work.
Where is the problem?
Not sure you've solved it already, but this is a solution that might help you on your way:
<?php
$arr = ['firstname' => 'John', 'lastname' => 'Dough', 'jobs' => ['job1' => 'writer', 'job2' => 'dad']];
$result = null; // the container for the search result
$key = 'job1'; // the key we are looking for
findKey($arr, $key, $result); // invoke the search function
/** print the result of our search - if key not found, outputs 'NULL' */
echo '<pre>';
var_dump($result); // output: string(6) "writer"
echo '<pre>';
/**
* #param array $arr the multidimensional array we are searching
* #param string $key the key we are looking for
* #param $result passed by reference - in case the key is found, this variable 'stores' the corresponding value.
*/
function findKey($arr = [], $key = '', &$result)
{
foreach ($arr as $key0 => $value0) {
if($key0 == $key) {
$result = $value0;
}
if(is_array($value0)) {
findKey($value0, $key, $result);
}
}
return false;
}

How to access object property of indeterminate depth

I have an object with data stored at multiple levels ( a JSON-decoded document ) like this:
$db = (object) array(
'simple_property' => 'value',
'complex_property' => (object) array(
'key' => 'value',
'nested' => (object) array(
'key' => 'value'
)
)
);
I want to be able to access and update data at any depth via reference. Example:
$db->{ $key } = $new_value
If $key is equal to 'simple_property', that works. But if $key is equal to 'complex_property->nested->key', it doesn't. Is there a way to accomplish what I want to, or am I looking at it incorrectly?
I don't think you can get it to work that way. You'll have to create a function (or class method) to do something like that. As an example:
function getRecursiveProperty($object, $path)
{
$array = explode('->', $path);
if (empty($array))
{
return NULL;
}
foreach ($array as $property)
{
if (!isset($object->$property))
{
return NULL;
}
if (!is_object($object->$property))
{
return $object->$property;
}
$object = $object->$property;
}
return $object->$property;
}
function setRecursiveProperty($object, $path, $value)
{
foreach (explode('->', $path) as $property)
{
if (!isset($object->$property))
{
return FALSE;
}
if (!is_object($object->$property))
{
$object->$property = $value;
return TRUE;
}
$object = $object->$property;
}
return FALSE;
}
$key = 'complex_property->nested->key';
echo getRecursiveProperty($db, $key); // value
setRecursiveProperty($db, $key, 'new_value');
echo getRecursiveProperty($db, $key); // new_value
Why you don't use $db = json_decode($json, true); instead of $db = json_decode($json);?
In this way you return an associative array instead of an object and you will not have these problems anymore.
json_decode ( string $json , bool $assoc)
json
The json string being decoded.
This function only works with UTF-8 encoded strings.
assoc
When TRUE, returned objects will be converted into
associative arrays.
More info: http://php.net/manual/en/function.json-decode.php

PHP how to extract info from json string wtihout knowing the keys

I am querying a service if a person have phone number(s) (also maybe not). I have a json string (as return value) like the following:
$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';
I convert this string to json_decode() function.
$jd = json_decode($json);
Then I want to get the phone numbers only into an array without keys.
if($jd->found) {
$o2a = get_object_vars($json);
}
var_dump($o2a);
When I want to see what $o2a holds with var_dump() function, I get the following:
array (size=2)
'data' =>
array (size=2)
0 =>
object(stdClass)[2]
public 'tel1' => string '1219' (length=4)
1 =>
object(stdClass)[3]
public 'tel2' => string '2710' (length=4)
'found' => boolean true
I want to get only the phone numbers into an array at the end like:
$phones = array('1219', '2710');
What makes me stop doing this is that I do not know how many phone numbers one can have. Json array could consist of more or less elements.
$possibleJson1 = '{"data":[],"found":false}'; //no phone number found
$possibleJson2 = '{"data":[{"tel1":"1102"},{"tel2":"3220"},{"tel3":"1112"},{"tel4":"3230"}],"found":true}'; //4 phone numbers found
It may vary 0-to-n, so if it was a constant number I could create that array within a loop.
Some functions without any code :)
$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';
$vals = array_values(array_reduce(json_decode($json, true)['data'], 'array_merge',[]));
var_dump($vals);
Convert it into an array and then you should be able to iterate it easily
$jd = json_decode($json, true);
$phones = array();
if(isset($jd['data']) && $jd['found']) {
foreach($jd['data'] as $key => $val) $phones[] = $val;
}
Instead of handling with an object, use the second parameter of the json_decode function so it would returned an array.
Check if the data and found keys exist.
Since you don't know what are the keys names, you can use array_values
Demo
.
$jd = json_decode($json, true);
if(isset($jd['data']) && isset($jd['found'])){
$telArr = $jd['data'];
$phones = array();
foreach($telArr as $tel){
$value = array_values($tel);
$phones[] = $value[0];
}
var_dump($phones);
}
Output:
array(2) {
[0]=>
string(4) "1102"
[1]=>
string(4) "3220"
}
Well, I would try something like that:
$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';
$jd = json_decode($json);
$phones = [];
if ($jd->found && count($jd->data)) {
foreach ($jd->data as $key -> $value) {
$phones[] = $value;
}
}
Try as using in_array and simple foreach loop
$json = '{"data":[{"tel1":"1102"},{"tel2":"3220"}],"found":true}';
$arr = json_decode($json, true);
$result = array();
if (in_array(true, $arr)) {
foreach ($arr['data'] as $key => $value) {
foreach($value as $k => $v)
$result[] = $v;
}
}
print_r($result);
Fiddle

Compare and replace values in array

I need compare 2 arrays , the first array have one order and can´t change , in the other array i have different values , the first array must compare his id with the id of the other array , and if the id it´s the same , take the value and replace for show all in the same order
For Example :
$array_1=array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2=array("4a-walking","2a-cats");
The Result in this case i want get it´s this :
"1a-dogs","2a-cats","3a-birds","4a-walking"
If the id in this case 4a it´s the same , that entry must be modificate and put the value of other array and stay all in the same order
I do this but no get work me :
for($fte=0;$fte<count($array_1);$fte++)
{
$exp_id_tmp=explode("-",$array_1[$fte]);
$cr_temp[]="".$exp_id_tmp[0]."";
}
for($ftt=0;$ftt<count($array_2);$ftt++)
{
$exp_id_targ=explode("-",$array_2[$ftt]);
$cr_target[]="".$exp_id_targ[0]."";
}
/// Here I tried use array_diff and others but no can get the results as i want
How i can do this for get this results ?
Maybe you could use the array_udiff_assoc() function with a callback
Here you go. It's not the cleanest code I've ever written.
Runnable example: http://3v4l.org/kUC3r
<?php
$array_1=array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2=array("4a-walking","2a-cats");
function getKeyStartingWith($array, $startVal){
foreach($array as $key => $val){
if(strpos($val, $startVal) === 0){
return $key;
}
}
return false;
}
function getMergedArray($array_1, $array_2){
$array_3 = array();
foreach($array_1 as $key => $val){
$startVal = substr($val, 0, 2);
$array_2_key = getKeyStartingWith($array_2, $startVal);
if($array_2_key !== false){
$array_3[$key] = $array_2[$array_2_key];
} else {
$array_3[$key] = $val;
}
}
return $array_3;
}
$array_1 = getMergedArray($array_1, $array_2);
print_r($array_1);
First split the 2 arrays into proper key and value pairs (key = 1a and value = dogs). Then try looping through the first array and for each of its keys check to see if it exists in the second array. If it does, replace the value from the second array in the first. And at the end your first array will contain the result you want.
Like so:
$array_1 = array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2 = array("4a-walking","2a-cats");
function splitArray ($arrayInput)
{
$arrayOutput = array();
foreach ($arrayInput as $element) {
$tempArray = explode('-', $element);
$arrayOutput[$tempArray[0]] = $tempArray[1];
}
return $arrayOutput;
}
$arraySplit1 = splitArray($array_1);
$arraySplit2 = splitArray($array_2);
foreach ($arraySplit1 as $key1 => $value1) {
if (array_key_exists($key1, $arraySplit2)) {
$arraySplit1[$key1] = $arraySplit2[$key1];
}
}
print_r($arraySplit1);
See it working here:
http://3v4l.org/2BrVI
$array_1=array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2=array("4a-walking","2a-cats");
function merge_array($arr1, $arr2) {
$arr_tmp1 = array();
foreach($arr1 as $val) {
list($key, $val) = explode('-', $val);
$arr_tmp1[$key] = $val;
}
foreach($arr2 as $val) {
list($key, $val) = explode('-', $val);
if(array_key_exists($key, $arr_tmp1))
$arr_tmp1[$key] = $val;
}
return $arr_tmp1;
}
$result = merge_array($array_1, $array_2);
echo '<pre>';
print_r($result);
echo '</pre>';
This short code works properly, you'll get this result:
Array
(
[1a] => dogs
[2a] => cats
[3a] => birds
[4a] => walking
)

converting array containing keys and values into string

I have an array it contains key and value. i would like to converting into a string.
array(
[business_type]=>'cafe'
[business_type_plural] => 'cafes'
[sample_tag]=>'couch'
[business_name]=>'couch cafe'
)
Expected Output:
business_type,cafe|business_type_plural,cafes|sample_tag,couch|business_name,couch cafe
NOTE:
I was searching in StackOverflow and found the below question and it has answer. I want exactly reverse one.
converting string containing keys and values into array
Try
$data = array();
foreach($arr as $key=>$value) {
$data[] = $key.','.$value;
}
echo implode('|',$data);
Another Solution:
function test_alter(&$item1, $key, $delimiter)
{
$item1 = "$key".$delimiter."$item1";
}
array_walk($arr, 'test_alter',',');
echo implode('|',$arr);
Use the foreach() function to go through the array and string the keys/values together...
Assuming your array is called $array
$result = "";
foreach($array as $key => $value){
$result .= $key . "," . $value . "|";
}
It's as simple as that.
EDIT - Thanks Nelson
After that, lost the last |
$result = rtrim($result, "|");
try this
$pieces=array();
foreach(array('key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3') as $k=>$v)
{
$pieces[]=$k.','.$v;
}
echo implode('|', $pieces);

Categories