I'm trying to receive an array via a cURL POST request, and I'm having an extrange issue.
Long story short: This is an api controller we're in, and when I json_encode a response...
echo json_encode($_POST['keys']); return;
The result (at the calling end of the api communication):
array(361) {
[0]=>
string(26) "some+urlencoded+string"
[1]=>
string(14) "and+some+other"
...
}
and so on. So the api receives my argument array no problem.
Then in the api I try a foreach loop to urldecode each string, to have those values in some other array:
$myArray = array();
foreach ($_POST['keys'] as $key => $value)
{
$myArray[$key] = urldecode($value);
// Or $myArray[] = urldecode($value); -- same result
}
echo json_encode($myArray); return;
And this is the result:
NULL
What am I doing wrong?
Thanks in advance :)
==================================================================================
EDIT: The problem seems to be that, in the api controller (that lives in a yii application) urledecode does not work. Neither does utf8_decode, nor base64_decode. At least they do not work inside a foreach loop, anyway. Why don't they work? Beats me. I'm still stuck.
==================================================================================
EDIT 2: I've made some progress in the isolation of the problem, asked another Q here at SO. Sorry for this, might as well be closed.
PHP (CI) cURL passed multidimensional array does not behave as one (Can't loop it)
I don't think your problem is here... are you sure your $_POST['keys'] is ok when you foreach it ? you should try to echo something in your loop, or print_r($_POST['keys']) just before.
I tried this :
$_POST['keys'] = Array('some+urlencoded+string','and+some+other') ;
$myArray = array();
foreach ($_POST['keys'] as $key => $value)
$myArray[$key] = urldecode($value);
echo '<pre>' ;
var_dump($_POST['keys']) ;
echo json_encode($_POST['keys']);
echo "\n" ;
echo json_encode($myArray);
echo '</pre>' ;
wich render this, looked pretty correct :
array(2) {
[0]=>
string(22) "some+urlencoded+string"
[1]=>
string(14) "and+some+other"
}
["some+urlencoded+string","and+some+other"]
["some urlencoded string","and some other"]
Make print_r on $_POST['keys'] and on $myArray, i think your loop is just not working.
Related
i used kirki drag and drop wordpress plugin for my site to create a sortable list when dragable is enabled.
i was able to create a setting that outputs this array, when i did var_dump for the settings here was what i got
array(3) { [0]=> string(10) "Big Grid 1" [1]=> string(10) "Big Grid 2" [2]=> string(10) "Big Grid 3" }
when in var_dump it sorts correctly when i drag and drop any element, they take there place as sorted in the var_dump array, to me the array given is completely useless till i set values for them .
so the question is how do i output them in php to get sorted just as they where in the array.
i tried switch case but its not working.
here is my code
foreach ($array[0] as $key => $value) {
switch ($key) {
case 'Big grid 1' :
// do something
break ;
case 'Big grid 2' :
// do something
break ;
case 'Big grid 3' :
// do something
break ;
}
}
please i need help on this one.
hope my question was clear.
i am no PHP expert, thus a complex answer would be well appreciated.
not sure what you are trying to do with using switch statement and I'm not really sure what you are trying to accomplish with that code you have,
but as I understand about your question, you just want to output the value of an array,
then you can do something like
$items = array("Big Grid 1","Big Grid 2","Big Grid 3");
$output = '';
foreach ($items as $item ) {
$output .= $item .'<br/>';
}
echo $output; // or return $output if you need a return value
I got a problem, I got a array from a sql query and I want to associate each value of this array to an index value.
array(16) { ["noCommande"]=> string(5) "49083" ["dateCommande"]=> string(19) "2007-02-21 18:24:04" ...
So here I just want to get back each value one per one. The array[i] doesn't work so I am a bit in trouble.
Thanks you for your support.
It's associative array, values are in
$array['noCommande'];
$array['dateCommande'];
etc.
If you want to ake a loop over array and write all values,
foreach ($array as $key => $value) {
echo $key . ': ' . $value; // echoes 'noCommande: 49083', etc.
}
You can iterate like below:-
foreach($your_array as $key=>$value){
echo $key.'-'.$value;
echo "<br/>";//for new line to show in a good manner
}
It will output like :- noCommande - 49083 and etc.
I´m learning to work with files in php, first i made this function for writing to the file, it´s quite simple:
function zapis_do_suboru($zapisovany_subor, $obsah_suboru)
{
$pracovny_subor = fopen($zapisovany_subor,"w") or die("Chyba pri otvarani suboru");
fwrite($pracovny_subor, $obsah_suboru) or die("Nejde zapisovat do suboru");
fclose($pracovny_subor);
echo "Zapis $zapisovany_subor prebehol uspesne.";
}
Then I made function for reading from the file, but I was little stuck here, because my book is explaining how to read just one row, but then i googled little bit and found some solution and made this function for reading from file:
function citanie_zo_suboru($citany_subor)
{
$pracovny_subor = fopen($citany_subor,"r") or die("Chyba pri otvarani suboru");
$j = 0;
while(!feof($pracovny_subor))
{
$pole[$j] = array(fgets($pracovny_subor, 4096));
$j++;
}
fclose($pracovny_subor);
return $pole;
}
Then i wanted to test it, so i create two variables:
$subor = "textsubor.txt";
$text = <<<_END
Riadok1 blabla
Riadok2 blabla
Riadok3 meno suboru: $subor
_END;
And this calling of functions:
zapis_do_suboru($subor, $text);
echo "<br />";
foreach (citanie_zo_suboru($subor) as $index =>$popis)
echo $popis."<br />";
But the problem is, that function citanie_zo_suboru is creating two-dimension array, so the output was only "array array array array". With print_r and little bit of trying i found out, that when i change:
foreach (citanie_zo_suboru($subor) as $index =>$popis)
echo $popis."<br />";
to:
foreach (citanie_zo_suboru($subor) as $index =>$popis)
echo $popis[0]."<br />";
it's doing exactly what i wanted. Can someone tell me why my function is creating two-dimension and not just classic one-dimension array? I would be really grateful if you could. Thanks
You are storing array in array key:
$pole[$j] = array(fgets($pracovny_subor, 4096));
So, $pole[0] should further contain array.
To simply write a file you can use file_put_contens() : http://php.net/manual/fr/function.file-put-contents.php
To read a file you have the same file_get_contents() : http://php.net/manual/en/function.file-get-contents.php
To read a file and store each line as value in an array use file() : http://www.php.net/manual/en/function.file.php
I hope this 3 function can help you in your current issue.
i've just reworked my recursion detection algorithm in my pet project dump_r()
https://github.com/leeoniya/dump_r.php
detecting object recursion is not too difficult - you use spl_object_hash() to get the unique internal id of the object instance, store it in a dict and compare against it while dumping other nodes.
for array recursion detection, i'm a bit puzzled, i have not found anything helpful. php itself is able to identify recursion, though it seems to do it one cycle too late. EDIT: nvm, it occurs where it needs to :)
$arr = array();
$arr[] = array(&$arr);
print_r($arr);
does it have to resort to keeping track of everything in the recursion stack and do shallow comparisons against every other array element?
any help would be appreciated,
thanks!
Because of PHP's call-by-value mechanism, the only solution I see here is to iterate the array by reference, and set an arbitrary value in it, which you later check if it exists to find out if you were there before:
function iterate_array(&$arr){
if(!is_array($arr)){
print $arr;
return;
}
// if this key is present, it means you already walked this array
if(isset($arr['__been_here'])){
print 'RECURSION';
return;
}
$arr['__been_here'] = true;
foreach($arr as $key => &$value){
// print your values here, or do your stuff
if($key !== '__been_here'){
if(is_array($value)){
iterate_array($value);
}
print $value;
}
}
// you need to unset it when done because you're working with a reference...
unset($arr['__been_here']);
}
You could wrap this function into another function that accepts values instead of references, but then you would get the RECURSION notice from the 2nd level on. I think print_r does the same too.
Someone will correct me if I am wrong, but PHP is actually detecting recursion at the right moment. Your assignation simply creates the additional cycle. The example should be:
$arr = array();
$arr = array(&$arr);
Which will result in
array(1) { [0]=> &array(1) { [0]=> *RECURSION* } }
As expected.
Well, I got a bit curious myself how to detect recursion and I started to Google. I found this article http://noteslog.com/post/detecting-recursive-dependencies-in-php-composite-values/ and this solution:
function hasRecursiveDependency($value)
{
//if PHP detects recursion in a $value, then a printed $value
//will contain at least one match for the pattern /\*RECURSION\*/
$printed = print_r($value, true);
$recursionMetaUser = preg_match_all('#\*RECURSION\*#', $printed, $matches);
if ($recursionMetaUser == 0)
{
return false;
}
//if PHP detects recursion in a $value, then a serialized $value
//will contain matches for the pattern /\*RECURSION\*/ never because
//of metadata of the serialized $value, but only because of user data
$serialized = serialize($value);
$recursionUser = preg_match_all('#\*RECURSION\*#', $serialized, $matches);
//all the matches that are user data instead of metadata of the
//printed $value must be ignored
$result = $recursionMetaUser > $recursionUser;
return $result;
}
I was wondering if i could assign values to a variable inside an IF statement. My code is as follows:
<?php
if ((count($newArray) = array("hello", "world")) == 0) {
// do something
}
?>
So basically i want assign the array to the $newArray variable, then count newArray and check to see if it is an empty array.
I know i can do this on several lines but just wondered if i could do it on one line
Try this:
if(count($newArray = array("Hello", "world")) == 0) {
....
}
I'd advice against this though, as it makes your code less readable. And highly illogical too as you know that the array in question contains two values. But perhaps you have something else in mind. :)
Yeah, you can, like so:
if(count($ary = array(1,2,3)))
Doing a var_dump of $ary gives:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
Actually you don't need to use count inside the if statement, because an empty array is considered to be false in PHP. See PHP documentation.
So your code can look like this:
if (!$newArray = array("hello", "world")) {
echo "newArray is empty";
}