Jump to content

Recommended Posts

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']);

Link to comment
https://forums.phpfreaks.com/topic/189915-declare-variable-from-array-please-help/
Share on other sites

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.

 

 

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)

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.

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.