RYU5
RYU5

Reputation: 51

Adding String to String in an array in php

table overview

I am using a database to get the prices as displayed as "Betrag". The actual value of "Betrag" is "18981", but i am converting it by:

$tabledata[] = array('category' => $beschriftung, 'value' => number_format($zeile['summe'], 0, ',', '.').'€', 'id' => $i);

My problem is that i want to get the marked text "Anzahl: 413" below the price, but using the number_format, it doesn't work. I was trying something like $tabledata += or just adding it after the conversion but it didn't get my expected output. It all works with a while loop iterating over the id.

So the actual question is: Is it possible to add a String to an array without deleting the value which is alredy in it? Feel free to ask questions or give comments.

Upvotes: 0

Views: 95

Answers (2)

C4pt4inC4nn4bis
C4pt4inC4nn4bis

Reputation: 598

it is possible to add a string to a value which already is in an array - for example

<?php $einArray = array('text1','text2');
$einArray[1] .= "test";
echo $einArray[1];
?>

Will output "text2test".

Is this the answer to your problem?

Upvotes: 0

Philipp
Philipp

Reputation: 15639

Thats the wrong place to fix your "issue". $tabledata is only an array, printed somewhere else and at this point, you have to move the output of your "Betrag". That means of couse, you have to add it to your $tabledata array.

$tabledata[] = array(
    'category' => $beschriftung,
    'value' => number_format($zeile['summe'], 0, ',', '.').'&euro;',
    'id' => $i,
    'count' => $betrag
);

and later, you have to print it right after $value. (Something like this..)

foreach ($tabledata as $row) {
    // ...
    echo $row['value'];
    echo "<br />";
    echo "Anzahl:" . $row['count'];
    // ...
}

But thats only an example and depends on how you build your response (maybe inside an template engine, ...)

Of couse, you could also do the fast and bad way by just append the "Betrag" to the value with a <br /> delimeter.

$tabledata[] = array(
    'category' => $beschriftung,
    'value' => number_format($zeile['summe'], 0, ',', '.').'&euro;' . '<br />Anzahl: ' . $betrag,
    'id' => $i
);

Upvotes: 1

Related Questions