简体   繁体   中英

why does the php code only fetch one digit from my two-digits-ids in the database?

Let's say my database's table has 11 entries, so 11 ids from 1 to 11. The following could would echo 12345678911 - it cuts off the last digit of 10 and 11 . I wonder why and more than anything: How can I fix this?

foreach($resultholeIdVonAutorenWerke as $row) {
    $autor_werk_id = $row['id'];

    $test .= "$autor_werk_id[0]";
}

echo $test

Because you specifically only access the first character. $autor_werk_id is string, not an array. When you use array syntax on a string it will return the character you specify from that string. In your case the first character because you use a key of zero.

Here's an example :

$string = '1234567890';
echo $string[5]; // Outputs 6

Remove that portion of code and it will work just fine:

foreach($resultholeIdVonAutorenWerke as $row) {
   $autor_werk_id = $row['id'];

   $test .= $autor_werk_id; 
}

echo $test;

Instead of looping you can implode the result to a string.

$ids = array_column($resultholeIdVonAutorenWerke, "id");
echo implode("", $ids); //1234567891011

Array_column grabs one column from the array and implode glues together the array items with the glue (in this case "", nothing)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM