function Assignment Parameter Problem

Source: Internet
Author: User
Such as:
PHP Built-in sprintf () function
Cases:
sprintf ($xml, $a, $b);
I want to insert the parameter $c now, $d
But $c, $d is the data taken out of the array.
$array =array (
Array (contents of ' C '),
Array (contents of ' d '),
)
If I loop out the $array, and then put the parameters is not feasible, what good method?
$str = ";
foreach ($array as $key = = $val) {
$str. = $val [0]. ', ';
}
sprintf ($xml, $a, $b, $str);


Reply to discussion (solution)

There's something wrong with your approach.
For sprintf ($xml, $a, $b);
There are at most 2%s (or other format characters) in the $MXL, otherwise a syntax error will occur
When writing sprintf ($xml, $a, $b, $c, $d); When
4 format characters are required in the $MXL
So, you just think about how to pass a value.

I know the format requirements for sprintf ().
I'm just taking this function out to do an example.
When a function encounters multiple arguments, can there be a way to take a value out of an array, pass it to a function, and pass it as multiple arguments, not one.

Call_user_func_array

Boss Xu, still do not understand my meaning.
I mean:
There is one way:
function Test () {
}
Test ($a, $b);//There may be more parameters later, but this is dynamic and there may be 5 or 6, not necessarily.

So I want to iterate through an array of source arrays:
$array =array (1,2,3,4,5);//I now need to pass the values in the array to test (), which is 5, but it could be 6, 7.
such as: Test ($a, $b, 1,2,3,4,5);
So I'm going to try to pass through the array to the function?
$args = ";
foreach ($array as $key = = $val) {
$args. = $val. ', ';
}
substr ($args, 0,-1);
Test ($a, $b, $args);
But I'm sorry the idea is wrong, because when it passes, it's a parameter, so what do we need to do in the middle?

Call_user_func_array
This method limits too much to death.

I don't know what you want to do, at least you have a problem with the example!
Can you give me a proper example?

I don't know what you want to do, at least you have a problem with the example!
Can you give me a proper example?
There is one way:
Test ($a, $b);//But I want to add more parameters in the back, possibly multiple.
But for the moment, I'm using
$args = "A,b,c";
Test ($a, $b, $args);//This is not feasible because there are only 3 parameters at this time.
The result I want is:
Test ($a, $b, a,b,c);//This is normal, there are 5 parameters, which is what I want.

function test ($a, $b) {  echo ' passed in: '. Join (', ', Func_get_args ());} Test (from); Incoming: 1,2test (1,2,3,4,5);//incoming: 1,2,3,4,5$ar = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f '); Call_user_func_array (' Test ', $ar);//Incoming : a,b,c,d,e,f


You still don't have an accurate description of what you mean
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.