0

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.

3
  • it is more an issue of presentation. Show the code that builds that html structure. Also, is the text you want to add always constant or is it depending from the values of the array? Commented Jun 22, 2018 at 7:47
  • It is depending from the values of the array, it is the amount of pieces and the Betrag is the amount in €. Commented Jun 22, 2018 at 7:48
  • why don't you just add a ney key/value pair to the array? Commented Jun 22, 2018 at 7:49

2 Answers 2

1

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, ',', '.').'€',
    '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
);
Sign up to request clarification or add additional context in comments.

1 Comment

I used the "bad way" and it works since i am working with ajax and json. Thank you.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.