mikebarbaro Posted January 26, 2010 Share Posted January 26, 2010 Hello, I have the below code integrated into a system I am making for a restaurant to accept orders online. I need to store the array contents in a human-readable fashion as the variable $finalitems. Can anyone please help? function print_ar($array, $count=0) { $i=0; $tab =''; while($i != $count) { $i++; $tab .= " | "; } foreach($array as $key=>$value){ if(is_array($value)){ echo $tab."<br />"; $count++; print_ar($value, $count); $count--; } else{ $tab2 = substr($tab, 0, -12); echo "<strong>$value</strong><br />"; } $k++; } $count--; } $finalitems = print_ar($_SESSION['items']); Quote Link to comment https://forums.phpfreaks.com/topic/189915-declare-variable-from-array-please-help/ Share on other sites More sharing options...
whansen02 Posted January 27, 2010 Share Posted January 27, 2010 if it was me doing this with that code, I would set $finalitems[$key] = $value; or something to that affect within your foreach loop. Lesson being behind the scenes there are old school things called copy constructors that take care of how to copy different variables, int's, strings, arrays, objects - objects in languages like C++ use custom copy constructors written by you to make sure the objects elements are copied to another array instance the way you expect. PHP is C++ based but tries to hide some of those elements from plain site so you don't have to worry about them.. sometimes for the better, sometimes for worse. Quote Link to comment https://forums.phpfreaks.com/topic/189915-declare-variable-from-array-please-help/#findComment-1002145 Share on other sites More sharing options...
mikesta707 Posted January 27, 2010 Share Posted January 27, 2010 PHP is written in C, not C++ (i'm nitpicking, I know), and technically, if your object doesn't need a deep copy, there is no need to define a copy constructor (or the other two of the big 3) as the default one that the compiler gives you would work just fine. Since PHP has no real pointer type, deep copy's wouldn't be very useful anyways. I'm not even entirely sure what copy constructors have to do with what OP is doing, but anyways... have you looked into print_r you can pass true into the optional second parameter to have the function return a string instead of printing something out. It seems this would do exactly what you want. $str = print_r($array, true); echo $str; //or just //print_r($array) Quote Link to comment https://forums.phpfreaks.com/topic/189915-declare-variable-from-array-please-help/#findComment-1002156 Share on other sites More sharing options...
mikebarbaro Posted January 28, 2010 Author Share Posted January 28, 2010 PHP is written in C, not C++ (i'm nitpicking, I know), and technically, if your object doesn't need a deep copy, there is no need to define a copy constructor (or the other two of the big 3) as the default one that the compiler gives you would work just fine. Since PHP has no real pointer type, deep copy's wouldn't be very useful anyways. I'm not even entirely sure what copy constructors have to do with what OP is doing, but anyways... have you looked into print_r you can pass true into the optional second parameter to have the function return a string instead of printing something out. It seems this would do exactly what you want. $str = print_r($array, true); echo $str; //or just //print_r($array) This makes the array but I need it to be in a more readable format. Instead of Array (1 => 2 =>).... It needs to be more like this: Value 1 Value 2 Value 3 My original code would echo it like that perfectly but what I need to do is save it as a variable so it can eventually later on be sent out as an email or fax. Quote Link to comment https://forums.phpfreaks.com/topic/189915-declare-variable-from-array-please-help/#findComment-1003005 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.