2

Sorry for not using the right terminology, but given how basic my PHP level is, perhaps you will not mind so much.

A function for a plugin I am using allows arguments in order to selectively echo content:

<?php phpquote(argumentstring); ?>

An example of the argument string is e.g. auto_refresh=0&tags=a,b,c

I had always assumed it would be possible to use an array to build up a 'string' e.g. something like

<?php $arg=array( 'auto_refresh' => '0', 'tags' => 'a,b,c' );
phpquote($arg); ?>

And was therefore thinking I could bring in other content from another field e.g.

<?php 
$fieldcontent = get_field("field_content");
$arg=array( 'auto_refresh' => '0', 'tags' => $fieldcontent );
phpquote($arg); ?>

However, this does not seem to work and so my two questions are:

  1. Is it possible to use an array to build such an argument string? Or does this depend on the plugin?
  2. If so, is it possible to bring in content from elsewhere as the example above?

Thanks in advance. I appreciate this may seem like a really basic question with incorrect terminology (sorry!) so hope it makes sense to the experts.

1
  • What's the input? And what should be the output? Commented Apr 26, 2014 at 10:35

4 Answers 4

2

You can use implode() and array_map() PHP functions to make an array to become an argument string:

<?php

    $args = array( 'auto_refresh' => '0', 'tags' => 'a,b,c' );

    function rb_map( $a, $b ) {
        return $a . '=' . $b;
    }

    $arg_string = implode('&', array_map( 'rb_map', array_keys($args), $args ) );
    //var_dump($arg_string);
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Loop through the array and use the implode() function for glueing pieces together. manual

Or you can do something like this, too:

$argumentString= "";
foreach($arg as $k=>$v){
    $argumentString.= $k.'='.$v.'&';
}
$argumentString = substr($argumentString,0,-1); //removes the last &

3 Comments

i like your answer, though I guess it would be nice if you could write up the code in a way that it fits the code used by the OP...
what do you think about this? http://3v4l.org/BThWI... feel free to use it in your answer ;)
To: RápliAndrás and @webeno: Thank you both so much for your replies :) Although I did end up using the implode and array_map method suggested by rambu, I can confirm that this works as well.
2
<?php
$arg=array( 'auto_refresh' => '0', 'tags' => 'a,b,c' );
echo urldecode(http_build_query($arg)); //output: auto_refresh=0&tags=a,b,c
?>

Is this what you want?

Check http_build_query for more information

1 Comment

Thanks very much Dikesh for your help. I found this worked really well for me too, even though I ended up using one of the other methods in the end. My final one was $completearg = urldecode(http_build_query($arg)); and then phpquote($completearg); I have upvoted it for others to read through as it seems like a really useful function!
1

Whether or not you can use an array as the argument of a function depends on how that is defined / used in the function itself.

Consider this for example:

function test($var) {
    print_r($var);
}

$bla = array(1 => "test me", 2 => "balbla");

test($bla);

(You can play with it here)

You can pass either a string or an array as an argument of the test function as print_r is able to handle both. If the function had echo for example, and you passed an array to the function as argument, it would fail.

1 Comment

Thanks webeno :) your explanation really helped here even though did end up using code above I have upvoted your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.