php - 字符串连接给出了奇怪的结果

I have built an OpenCart module and tested it on a few shops and it works well. One client just reported to me a problem and after inspecting the html I saw this:

My code in the .tpl template file:

<table class="table table-bordered table-hover" > 
 <tbody> 
   <?php foreach($row['strings'] as $row_string){   

     $key = $row_string['key']; 
     $id = $row['simpleFilePathEscaped'].$key;  ?>

     <tr>      

       <td class="text-center"> 
         <label><?php echo $key; ?> </label> 
       </td> 

       <td class="text-center">
         <div>
           <input  id="text_value_<?php echo $id; ?>" type="text" value="<?php echo $row_string['value']; ?>" placeholder="<?php echo $key; ?>" class="form-control" />
         </div>
       </td> 

       <td class="text-center" >
         <button  id="save_icon_<?php echo $id; ?>" onclick="addLiteralToFile('<?php echo $row['secondary_file_path_escaped'] ?>', '<?php echo $key; ?>', '<?php echo $id;?>');" class="btn btn-primary"><i id="save_inner_icon_<?php echo $id; ?>" class="fa fa-save"></i></button>
         <div id="loading_icon_<?php echo $id; ?>" <div  class="loader"></div> </div>  
       </td> 

     </tr>  
   <?php } ?> 
 </tbody>
</table>

The result for an example row in this client's shop is this:

<tr>                     
    <td class="text-center" > 
     <label>direction</label> 
    </td> 

    <td class="text-center">
     <div>
       <input id="text_value_" type="text" value="ltr" placeholder="direction" class="form-control">
     </div>
    </td> 

    <td class="text-center">
     <button id="save_icon_" onclick="addLiteralToFile('----home----admin----domains----itrend.si----public_html----test----admin----language----english----en-gb.php', 'direction', '');" class="btn btn-primary"><i id="save_inner_icon_" class="fa fa-save"></i></button>
     <div id="loading_icon_" <div="" class="loader"></div>   
    </td> 

</tr>

If you notice you will see that all instances of <?php echo $id; ?> give an empty string. so id="save_icon_<?php echo $id; ?>" becomes id="save_icon_"

That is extremely strange because $id is a concat of $row['simpleFilePathEscaped'] and $key. Even if $row['simpleFilePathEscaped'] is empty I know for sure that $key has a value...Because it is echoed and in this example it is "direction" (in the label tag)

Need some help finding out why this is happening...

$row = {  "strings" => array of  {  "key"=> string,  "value"=> string }, 
          "simpleFilePathEscaped" => string, 
          "secondary_file_path_escaped" => string, 
          "primary_file_path"=> string 
} 

Sorry if i misunderstood the way your $row is build.

if you make an example like

$row['strings'] = ['key' => 10];
$row['strings'] = ['key' => 110];

foreach($row['strings'] as $row_string)
$key = $row_string['key'];

print_r($key); //will give you an empty value

but print_r($row); // will give Array ( [strings] => Array ( ['key'] => 110 ) )`

you need to change your foreach.

foreach($row['strings'] as $row_key => $row_string)
$key = $row_key;

then it will give the 'key' on echo.

or you can do

foreach($row['strings'] as $row_string)
$key = key($row_string);

There is a bug in your code:

Given your array looks like this (as written in your question):

$row = [  "strings" => array of  {  "key"=> string,  "value"=> string }, 
          "simpleFilePathEscaped" => string, 
          "secondary_file_path_escaped" => string, 
          "primary_file_path"=> string 
]

It is not possible for this code:

<?php foreach($row['strings'] as $row_string){   

 $key = $row_string['key']; // <--- THIS is not possible with the array structure you presented
 $id = $row['simpleFilePathEscaped'].$key;  ?>

 <tr>      

   <td class="text-center"> 
     <label><?php echo $key; ?> </label> 
   </td> 

to print this as you suggested in your question:

<tr>                     
<td class="text-center" > 
 <label>direction</label> 
</td> 

Because during the foreach loop, the $row_string variable will hold a string value but you are trying to access it as an array with an associative key.