-2

I'm trying to figure how to convert an array into a string but don't know how to do it. Keep saying Array to string conversion in my function table_header or function labelheader_cell. I'm trying to understand how to convert an array into string keep giving me an error.

Here is my code:

function table_header($labels, $params='')
{
    start_row();
    foreach ($labels as $label)
        labelheader_cell($label, $params);
    end_row();
}


    function labelheader_cell($label, $params="")
    {
        echo "<td class='tableheader' $params>$label</td>\n";
    }

    function view($trans)
    {
        return get_view($trans["user_no"]);
    }

    $th = array(_("Period"), _("Amount") => array('fun'=>'view'), _("Last Year"), array('insert'=>true, 'fun'=>'edit_link'), array('insert'=>true, 'fun'=>'edit'));
        table_header($th);
2
  • several possibilities: you can implode() the array with a delimiter like implode(',', $array), you could loop through the array (for($i=0; $i<count($array), $i++) { }) and do some output in there,.. - what do you want your output to look/be like? Commented Mar 17, 2019 at 2:02
  • where should i put my implode() i'm new to this array i'm still figure out array. do i need to put it at function table_header ? or labelheader_cell function?, it would display the table Period which is date that is inserted and amount with link with view and last year date which is date updated and insert array which would edit the link. Commented Mar 17, 2019 at 2:31

1 Answer 1

2

Use the implode function:

http://php.net/manual/es/function.implode.php

you can glue each element of the array in one string.

For Example:

for the array

$a = array('p1','p2','p3','p4');

To convert it to string

$s = implode('; ',$a);

You will get:

p1; p2; p3; p4
Sign up to request clarification or add additional context in comments.

1 Comment

Although that link could answer the question, an example of usage is always better suited in SO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.