what does this construct mean in PHP ? It is storing a variable called "function" with his String value in an array ?
array('function' => 'theme_select_as_checkboxes')
thanks
its just an associative array and unless some context is given, doesnt mean anything special!
Seems like declaring an associative array. With sucha an array, you can retrieve the content of the array this way :
$myArray = array('function' => 'theme_select_as_checkboxes');
echo $myArray['function']; // Prints 'theme_select_as_checkboxes
No magic in here ! ;)
Appears to be a function/method name that can be called at a later time. Other than the data, it looks to be just a standard array.
You may be interested in this question as well: How to call PHP function from string stored in a Variable.
this is not a special structure. http://en.wikipedia.org/wiki/Associative_array http://www.bellaonline.com/articles/art31316.asp
Related
I have an array of 'servers' that I'm storing in a JSON file.
The JSON file looks like this:
{"1":{"available":1,"players":0,"maxplayers":4}}
I retrieve this value with this:
$servers = (array) json_decode(file_get_contents("activeservers.json"));
However, when I try to access the array with $server = $servers[$id], $server is null. I noticed that the key is in quotes, so I also tried casting $id to a string, and surrounding it with quotes (") which didn't work.
Something to note, is that this code is returning "NULL":
foreach(array_keys($servers) as $key){
var_dump($servers[$key]);
}
Your code is wrong. Also you don't need to type cast when doing a json_decode, you can instead set the second parameter to true more info here.
Also you don't need to use the array_keys function in your foreach loop,
try this.
$json = '{"1":{"available":1,"players":0,"maxplayers":4}}';
$servers = json_decode($json, true);
foreach($servers as $key => $value) {
print $value["available"];
}
Do a print_r($value) to get all the array keys available to use. Also you could take advantage of the $key variable to print out the array key of the parent array.
Thanks, #Rizier123 (who solved the question).
Apparently passing TRUE as the second parameter to my json_decode function fixes the issue.
After checking the PHP documentation for json_decode() (PHP: json_decode), it seems that passing this parameter means that the resulting decoded array is automatically converted into an associative array (and this is recurring, meaning that this automatically happens for sub-arrays).
Edit: #Rizier123 also says that "you might want to read: stackoverflow.com/a/10333200 to understand a bit better why it is so "weird" and your method didn't work properly."
I am trying to write a soap parameter in REALbasic.
I need to add an array within another array similar to this in php:
$params = array(array(
'sku' => 'some sku'
));
so I can pass this:
$result = $client->call($session, 'catalog_product.list', $params);
I have
dim aArgs (0,1) as String
dim aParmas (0,1) as String
aArgs(0,0)="sku"
aArgs(0,1)="some sku"
aParmas(0,1)= aArgs
But receive a "Type mismatch error. Expected String, but got String(,)"
How can I do this.
Thanks
First off, the line
aParmas(0,1)= aArgs
is wrong because you assign an array (which is in aArgs) to a single element of aParmas. And since those single elements hold a String, you try to assign an array to a single string here, hence the error message.
But I think you're looking at this from the wrong end. You need to start with figuring out what parameters you need to send to the session function you want to call.
That means: You need to find the REALbasic function for $client->call. Once you know which function that is, look at the parameters that function expects. I doubt it expects a two-dimensional array for the "params". Once you know what to pass here, let us know if you still cannot figure out how to get it working.
An explanation of multidimensional arrays in REALbasic is here
The short answer is that you can't have a PHP-like array of arrays. You need to wrap your array in a class and make the class behave like an array.
Any reason you're using REALbasic? If it's cross-platform you're after, python is ALWAYS a better choice
I am trying to write this using php and json_encode
{"patient": {"demographics": {}}
}
This is the Array I am using with json encode.
Array("patient" => Array("demographics" => Array()))
When I echo the output, this is what I get:
{"patient":{"demographics":[]}}
I really think this is a stupid mistake ob my part. All help is appreciated.
try
json_encode($your_array, JSON_FORCE_OBJECT)
as per the docs: http://php.net/json_encode
By default, php arrays stay arrays ([]) when json'd, unless there's a single non-numeric key, in which case it'll be an object
You can also try this:
json_encode(Array("patient" => Array("demographics" => new stdClass)));
This way you can have empty arrays and empty objects in the same JSON, which would not be possible using the other answer.
Im having a little trouble in referencing indexes in arrays in Smarty. I believe that it is because the variable I am using as the index is a string. How may I cast this string as a integer within the template?
Thanks.
If I correctly understand the question, {$variable|intval}
The documentation shows many usage examples, especially with regards to accessing array elements.
{$foo} <-- displaying a simple variable (non array/object)
{$foo[4]} <-- display the 5th element of a zero-indexed array
{$foo.bar} <-- display the "bar" key value of an array, similar to PHP $foo['bar']
{$foo.$bar} <-- display variable key value of an array, similar to PHP $foo[$bar]
{$foo[bar]} <-- syntax only valid in a section loop, see {section}
I believe that it is because the
variable I am using as the index is a
string
I don't agree with that belief:
$arr = array('a');
$i = '0';
echo $arr[$i]; // echos a
I think the problem lies elsewhere. If you have further questions, you should include some of your code.
If it's already assigned to a variable, say $var, you can set the type of a variable like this:
{$converted = settype ($var, 'integer')}
You don't have to use the $converted value, but if you don't assign it, the bool will show up in your page.
since i am not allowed to comment (doh), i will just say that #webbiedave code is correct in php, but in smarty it doesnt work. i have just spent too much time trying to figure it out why i am not accessing data from array in template, and i found out that i had array with integer keys and the parameter which i used for key in smarty was string, so it wasnt working as expected. i solved it like that:
<!--{debug says}
{$item}=> Array (2)
name=> "lalala"
id => "123"
...
{$arrays} => Array (7)
123=> Array (3)
other_part_i_care=> "bebebe"
...
-->
{$arrays[$item.id].other_part_i_care} <!--this doesnt return anything-->
{assign var='item_id' value=$item.id} <!--my guess here it gets interpreted as int -->
{$arrays[$item_id].other_part_i_care} <!--this return expected outcome-->
i have an simple array:
array
0 => string 'Kum' (length=3)
1 => string 'Kumpel' (length=6)
when I encode the array using json_encode(), i get following:
["Kum","Kumpel"]
My question is, what is the reason to get ["Kum","Kumpel"] instead of { "0" : "Kum", "1" : "Kumpel" }?
"{}" brackets specify an object and "[]" are used for arrays according to JSON specification. Arrays don't have enumeration, if you look at it from memory allocation perspective. It's just data followed by more data, objects from other hand have properties with names and the data is assigned to the properties, therefore to encode such object you must also pass the correct property names. But for array you don't need to specify the indexes, because they always will be 0..n, where n is the length of the array - 1, the only thing that matters is the order of data.
$array = array("a","b","c");
json_encode($array); // ["a","b","c"]
json_encode($array, JSON_FORCE_OBJECT); // {"0":"a", "1":"b","2":"c"}
The reason why JSON_FORCE_OBJECT foces it to use "0,1,2" is because to assign data to obeject you must assign it to a property, since no property names are given by developer (only the data) the encoder uses array indexes as property names, because those are the only names which would make sense.
Note: according to PHP manual the options parameters are only available from PHP 5.3.
For older PHP versions refer to chelmertz's answer for a way to make json_encode to use indexes.
As Gumbo said, on the JS-side it won't matter. To force PHP into it, try this:
$a = new stdClass();
$a->{0} = "Kum";
$a->{1} = "Kumpel";
echo json_encode($a);
Not that usable, I'd stick with the array notation.
Just cast as an object and it will work fine...the JSON_FORCE_OBJECT parameter does exactly the same thing.
json_encode((object)$array);
Don't forget to convert it back into a php array so you can access its values in php:
$array = (object)$array;
$array = (array)$array;
json_encode($array);
Since you’re having a PHP array with just numeric keys, there is no need to use a JavaScript object. But if you need one, try Maiku Mori’s suggestion.
I personally think this is a bug that needs to be fixed in PHP. JSON_FORCE_OBJECT is absolutely not an answer. If you try to do any sort of generic programming you get tripped up constantly. For example, the following is valid PHP:
array("0" => array(0,1,2,3), "1" => array(4,5,6,7));
And should be converted to
{"0": [0,1,2,3], "1": [4,5,6,7]}
Yet PHP expects me to either accept
[[0,1,2,3],[4,5,6,7]]
or
{"0":{"0":1,"1":1,"2":2,"3":3},"1":{"0":4,"1":5,"2":6,"3":7}}
Neither of which are right at all. How can I possibly decode an object like that? What possible reason is there to ever change something that is clearly using strings as indexes? It's like PHP was trying to be clever to help out idiotic people who can't differentiate strings from ints, but in the process messed up anyone legitimately using strings as indexes, regardless of what the value COULD be turned into.