Passing an array as a parameter in a custom function - php

Hey,
I was wondering if it was possible to pass an associative array as a parameter in a custom function. This is my scenario:
In the php file I set the array:
$dataArr = array('one'=>'1','two'=>'2','three'=>'3');
$tpl->assign('dataArr',$dataArr);
This is my custom function dulled down
function smarty_function_drawChart($params, &$smarty){
print_r($params);
}
This is my function call in the template
{drawChart data={$dataArr} title='Title of the Chart'}
The problem I am having is that if you notice where I print_r($params), that shows:
Array
(
[data] => Array
[title] => Title of the Chart
)
It seems to be passing the string 'Array' rather than the actual array. I have done debugging right before passing the $dataArr that shows {$dataArr.one} has a value. Once inside my custom function $params['data'].one does not exist.
Any ideas on what I am doing wrong?
Thanks
Levi

I am still not 100% sure why my code above didn't work. My thought is that the brackets work just as an 'echo' would do in php, which is why the string 'Array' was being passed into the function. I was able to get it to work by simple removing the brackets around the $dataArr variable.
This was my original call:
{drawChart data={$dataArr} title='Title of the Chart'}
This is my new call that works
{drawChart data=$dataArr title='Title of the Chart'}

Related

PHP http_build_query() generate incorrect url for array get parameter

I probably discovered a bug of a PHP function http_build_query().
I am developing a search engine with dynamic form and some of these parameters are array.
I simply get the query url from the current $_GET with http_build_query().
But all of these array parameters are automatically changed from "arrayName%5B%5D" to "arrayName%5B0%5D" in the new query generated.
$queryStr = http_build_query($_GET);
original url
&arrayName%5B%5D=
new query string got from http_build_query():
&arrayName%5B0%5D=
What is the reason for this? How to fix this?
It's not a bug to http_build_query() function.
When you pass the get parameter via URL like: "arrayName[]="
print_R($_GET);
will return
Array
(
[arrayName] => Array
(
[0] =>
)
)
and http_build_query generates the url encoded string from array, the result is:
&arrayName%5B0%5D=
and decoded this looks like:
arrayName[0]=
Now you can see where the 0 came from :)
There is no need to fix this, you can change your code to pass keys for the arrayName or still use it as is.
Suppose we are using multiple checkboxes with ASC/DESC orders.
For that previously provided solution not working.
$queryStr = http_build_query($_GET);
$queryStr = urldecode($queryStr);
$queryStr = preg_replace('/[[0-9]+]/', '[]', $queryStr);
This solution works fine for me.
Forex. https://example.com?s=LorumText&post_type[]=blog
// Now your string won't be converted into hashcode(%5B0%5D=), It remains with []
Current Result: https://example.com?s=LorumText&post_type %5B0%5D=blog
Updated Result: https://example.com?s=LorumText&post_type[]=blog

REALbasic array in an array

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

PHP - Function name from variable

Okay, I've found a possible solution for this, but for some reason, I can't make it work in my application. Apparently, if I have a variable which contains a name function, I could use
<?php echo $variable(); ?>
to output the function with the same name.
I'm using Codeigniter. It has a function in its Form helper to output a text field, which is
<?php form_input(); ?>
I have a variable
<?php $instance['taxon_field'] = 'form_input'; ?>
If I echo out this variable, I do get the needed value, 'form_input'. However, as soon as I try to echo
$instance['taxon_field']()
I get a warning:
Message: Illegal string offset 'taxon_field'
and a fatal error:
Call to undefined function p()
I am really clueless here, because echoing only the variable gives 'form_input', but echoing $variable() only gives 'p'.
Where am I doing wrong?
The actual problem here is that $instance is not an array, but a string. Judging from the error message, it's a string whose value starts with p.
The syntax $var[$key] is used not only to access array elements but also to index into strings, where $var[0] would be the first character (actually, byte) of $var etc. If $instance is a string and you write $instance['taxon_field'] then PHP will try to convert 'taxon_field' to an integer in order to index into the string. This results in 0 as per the usual conversion rules, so the whole expression gets you the first letter of the string.
Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name.
Use call_user_func()
call_user_func($instance['taxon_field']);
The confusion created is actually my own fault because I failed to provide some aditional information which I thought was not important, but turned out to be crutial. My $instance[] array is actually a result of a foreach loop (two of them, to be precise) and is a part of a bigger multidimensional array. The actual code is more complicated, but I'll try to represent it right:
<?php
$bigger_array = array(
0 => array(
'field_one' => 'value_one',
'field_two' => 'value_two',
'field_three' => 'new_function'
),
1 => array(
'field_one' => 'new_value_one',
'field_two' => 'new_value_two',
'field_three' => 'echo'
)
);
function new_function()
{
echo 'New function called.';
}
foreach($bigger_array as $instance)
{
$name = $instance['field_three'];
$name('Hello World!');
}
?>
This will output the following:
New function called.
Fatal error: Call to undefined function echo() in /opt/lampp/htdocs/bla.php on line 69
In other words, the newly defined function works fine, but the built-in 'echo' doesn't.
This is actually not my original problem, this is something that I've encountered while trying to debug the initial issue. And the original problem is that creating a function from a single-dimensional array works okay. whereas creating a function from a multi-dimensional array within a foreach loop transforms the array into a string with the value of its last member.
Now, I'm still not really able to fully answer my question, but I think information I'm giving could lead to a solution. In the simplified example that I gave here, why am I getting the message that echo() function is not defined, while the new function works fine?

array values do not work as expected

I've a soap function which expects 3 parameters that should be passed as strings with quotes.
function('id','username','password');
and in another hand i've an array which contains :
[0] = > "'id','username','password'"
[1] = > "'id','username','password'"
....
when i echo $array[0] out put is 'id','username','password' and when i use function('id','username','password'); there is no problem but when i use
function($array[0]); it won't work.
i tested my array with echo, die, print_r ... the output is the same as the function expects!!!!
any help ?
thanks ; )
Simply because it can't work. If you have a function that needs 3 parameters, you can't pass a single parameter. Also if is an array that contains the 3 parameters you need, the function still want and need 3 parameters. Thus, if you give the function an array, it will use just the array as the first one (so you'll have an unexpected behavior) and take the second and the thirs as NULL.
It's true that php is a little bit magic, but can't do miracles.
You need to change the signature of your function.
function('id','username','password');
is a function with three parameters.
function($array[0]);
is a function with only one parameter.

PHP: array('function' => 'theme_select_as_checkboxes')

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

Categories