Jump to content

How to print a string that also contains an array?


E_Leeder

Recommended Posts

If I wanted to print a string that contains a variable that also contains an array, how would I do so? I want to put the resulting echo into a log file.

Example: “These are the books: $userArray and these are the pages: $pagesArray”;

With just an array, I can use print_r($array, true) to get an array formatted for use in a string (which works well for my purposes) but this doesn’t work with two arrays unless I do this manually. (Such as setting the $user_array = print_r($userArray). But I want to write a function that will take a string, potentially containing arrays  (as variables), and interpolate/make them legible for  humans. (Some of the arrays are also multidimensional.)

But it looks like I can't even concatenate a string and an array. $string = $string . $array. gives an error, array to string conversion.


I don’t understand PHP very well so please explain what is going on here. and the best way to record something like the Example above.
 

Link to comment
Share on other sites

Thanks, I should have thought of that. :sweat:

 

But I guess there is no way to have a variable that contains an array in a string because of the string to array conversion error?

 

So a function like this is impossible?

$variable = “These are the books: $userArray and these are the pages: $pagesArray”; // (double or single quoted)

function usePrint_rOnAllArraysInString($variable) {

// do the magic print_r on all arrays in the variable passed
return $variable // all arrays in the string flattened into something like print_r does

}
Edited by E_Leeder
Link to comment
Share on other sites

Yes, very close to something like that, thanks. Except that I'd like to prepend a string to each array print out, and have the ability to put multiple string/array combinations in the function.

 

I have a better idea of what would be necessary now, thanks to you example.

 

(The data doesn't make sense, just using it as an example.)

$string1 = 'There are the books: ';
$array1 = ('book1', 'book2', 'book3');

$string2 = 'There are the pages: ';
$array2 = ('page1', 'page2', 'page3');



function printArray($string, $array, $string2, $array2) {
// do the work

return $string;
}

But how would I make a function take an indefinite number of arguments? I googled it and came up and func_get_args().

 

So would there be any problems with this?

function printArray($args) {
  $args = func_get_args()
  $string = '';
  foreach ($args as $arg) {
   if (is_array($arg) {
    $string .= print_r($arg, true);
   }
   else {
   $string .= $arg;
   }
  }
return $string;
}

any problems with this code or ways to make it better?

Edited by E_Leeder
Link to comment
Share on other sites

IMHO print_r() is a debugging function and not suitable for output to a web page for user consumption.

 

You might find it useful to have a couple of functions in your arsenal, such as

function bulletList($arr)
{
    return "<ul><li>" . join('</li><li>', $arr) . "</li></ul>\n";
}

function commaList($arr)
{
    $last = array_pop($arr);
    return join(', ', $arr) . " and $last";
}

Then can

$books = array (
        'Treasure Island',
        'Harry Potter and the Philosopher\'s Stone',
        'A Tale of Two Cities'
    );
$pages = array(23,45,135,247,248);
    
$sentence = "These are the books " . bulletList($books) . "and these are the pages " . commaList($pages);

and echoing $sentence gives

 

 

These are the books

  • Treasure Island
  • Harry Potter and the Philosopher's Stone
  • A Tale of Two Cities

and these are the pages 23, 45, 135, 247 and 248

 

Edited by Barand
Link to comment
Share on other sites

Thanks for sharing, Barand. The string output I want doesn't need to be THAT legible for humans because it is just going in a log, and the log will be parsed. But if I do need to look at an individual log entry, I should be able to make sense of it (even if it takes a lot of effort).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.