E_Leeder Posted October 25, 2014 Share Posted October 25, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/ Share on other sites More sharing options...
jcbones Posted October 25, 2014 Share Posted October 25, 2014 (edited) Use the second argument to print array. echo 'These are the books: ' . print_r($userArray,true) . ' and these are the pages: ' . print_r($pagesArray,true); Edited October 25, 2014 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494653 Share on other sites More sharing options...
E_Leeder Posted October 25, 2014 Author Share Posted October 25, 2014 (edited) Thanks, I should have thought of that. 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 October 25, 2014 by E_Leeder Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494666 Share on other sites More sharing options...
Knoxxy Posted October 25, 2014 Share Posted October 25, 2014 Id use implode to add all the values to your string. http://php.net/manual/en/function.implode.php Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494667 Share on other sites More sharing options...
jcbones Posted October 25, 2014 Share Posted October 25, 2014 Do you mean like: function printArray(array $arr) { $op = null; foreach($arr as $key=>$val) { if(is_array($val)) { $op .= printArray($val); } else { $op .= $key . ': ' . $val . '<br />'; } return $op; } Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494687 Share on other sites More sharing options...
E_Leeder Posted October 26, 2014 Author Share Posted October 26, 2014 (edited) 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 October 26, 2014 by E_Leeder Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494781 Share on other sites More sharing options...
Barand Posted October 26, 2014 Share Posted October 26, 2014 (edited) 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 October 26, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494786 Share on other sites More sharing options...
E_Leeder Posted October 26, 2014 Author Share Posted October 26, 2014 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). Quote Link to comment https://forums.phpfreaks.com/topic/292046-how-to-print-a-string-that-also-contains-an-array/#findComment-1494804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.