Jump to content

Declare variable from array! Please help!


mikebarbaro

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.