0

I have a query that returns a row from database.

I would like to convert resulting array to strings, where each string is named as the dabase header, so I can use those strings in code.

I am currently doing it like below, but that doesn't seem like a good practice (or?)?

foreach($news_array as $position_in_main_array => $inner_array_member)
{           
    echo $inner_array_member["Date"];
    echo $inner_array_member["Published"];
    echo $inner_array_member["Title"];
    echo $inner_array_member["Text"];
}

Thanks

5
  • 1
    Why don't you use variables as: $mydate = $inner_array_member["Date"]; $mymember = $inner_array_member["Published"]; ? Commented Jan 13, 2016 at 8:38
  • 3
    That looks just fine to me...!? Commented Jan 13, 2016 at 8:43
  • Do you want any separator between string ? Commented Jan 13, 2016 at 8:44
  • Simple concatenate array and form a string eg : str = array1 + array2 .... Commented Jan 13, 2016 at 8:45
  • You should use foreach($news_array as $position_in_main_array){foreach($position_in_main_array =>$inner_array_member){echo $inner_array_member['Date'];}} Commented Jan 13, 2016 at 8:45

3 Answers 3

2

you can use implode function

$headarray = array($inner_array_member["Date"],$inner_array_member["Published"],$inner_array_member["Title"],$inner_array_member["Text"]);

echo implode(" ",$headarray);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use

complete code

foreach($news_array as $position_in_main_array => $inner_array_member)
{           
    $headarray[] = $inner_array_member["Date"];
    $headarray[] = $inner_array_member["Published"];
    $headarray[] = $inner_array_member["Title"];
    $headarray[] = $inner_array_member["Text"];
}

echo implode(" ",$headarray);

Thanks Amit

Comments

0
foreach($news_array as $position_in_main_array => $inner_array_member)
{           
    foreach($inner_array_member as $key =>  $value)
    {           
        echo $key.": ".$value." ";
    }
    //new line echo "\n";
    echo "<br>";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.