jjc180 Posted March 18, 2009 Share Posted March 18, 2009 Hey, I am trying to use a variable in an array key with no success. here is a snippet of code: $row_i = 1; foreach($enrolled_array as $enrolled_client){ $cell_1 = "topmostSubform[0].Page1[0].SMART_ID___Row_".$row_i."[0]"; $fields[$cell_1]->setValue($enrolled_client['clientid']); $row_i++; } I get the following error: [19-Mar-2009 08:20:50] PHP Fatal error: Call to a member function setValue() on a non-object in /home/prime09/public_html/autoforms/broker_invoice.php on line 139 If I manually put in the following it will work without any issues but of course what I am trying to do here is have the array key change each time the loop passes through. Works: $fields['topmostSubform[0].Page1[0].SMART_ID___Row_1[0]']->setValue($enrolled_client['clientid']); Any tips appreciated! I am sure its something obvious. Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/ Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 Basically, it is saying that $fields[$cell_1] is not an object. Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-788073 Share on other sites More sharing options...
tracivia Posted March 18, 2009 Share Posted March 18, 2009 well it seems $fields[$cell_1] is not the object with the methods and properties ou expect. You can use key in foreach loop like foreach($arr as $k=>$v){...} $k represents the key. Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-788077 Share on other sites More sharing options...
jjc180 Posted March 18, 2009 Author Share Posted March 18, 2009 ok, but $fields['topmostSubform[0].Page1[0].SMART_ID___Row_1[0]'] is an object and $cell_1 is equal to topmostSubform[0].Page1[0].SMART_ID___Row_1[0]. So why does $fields[$cell_1] not work? How can I overcome this? Thanks. Also the following works fine: ($row_i is replace with "1") $row_i = 1; foreach($enrolled_array as $enrolled_client){ $cell_1 = "topmostSubform[0].Page1[0].SMART_ID___Row_1[0]"; $fields[$cell_1]->setValue($enrolled_client['clientid']); $row_i++; } Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-788083 Share on other sites More sharing options...
samshel Posted March 18, 2009 Share Posted March 18, 2009 $row_i = 1; foreach($enrolled_array as $enrolled_client){ echo $cell_1 = "topmostSubform[0].Page1[0].SMART_ID___Row_1[0]"; echo "<br/>"; echo $cell_1 = "topmostSubform[0].Page1[0].SMART_ID___Row_".$row_i."[0]"; $fields[$cell_1]->setValue($enrolled_client['clientid']); $row_i++; } check closely if they are exactly same. Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-788105 Share on other sites More sharing options...
jjc180 Posted March 19, 2009 Author Share Posted March 19, 2009 $row_i = 1; foreach($enrolled_array as $enrolled_client){ echo $cell_1 = "topmostSubform[0].Page1[0].SMART_ID___Row_1[0]"; echo "<br/>"; echo $cell_1 = "topmostSubform[0].Page1[0].SMART_ID___Row_".$row_i."[0]"; echo "<br/>"; $fields[$cell_1]->setValue($enrolled_client['clientid']); $row_i++; } check closely if they are exactly same. They look exactly the same. Is this a bug in PHP? Output: topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_2[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_3[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_4[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_5[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_6[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_7[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_8[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_9[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_10[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_11[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_12[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_13[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_14[0] topmostSubform[0].Page1[0].SMART_ID___Row_1[0] topmostSubform[0].Page1[0].SMART_ID___Row_15[0] Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-788135 Share on other sites More sharing options...
tracivia Posted March 20, 2009 Share Posted March 20, 2009 try to paste the part you get $cell_1. You can also try to redefine it's type like (int)$cell_1 etc Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-789282 Share on other sites More sharing options...
sasa Posted March 20, 2009 Share Posted March 20, 2009 tray to print_r(array_keys($fields)); and look which one miss Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-789415 Share on other sites More sharing options...
jjc180 Posted March 24, 2009 Author Share Posted March 24, 2009 tray to print_r(array_keys($fields)); and look which one miss Output: Array ( [0] => topmostSubform[0].Page1[0].ica_name[0] [1] => topmostSubform[0].Page1[0].ica_bank[0] [2] => topmostSubform[0].Page1[0].ica_bsb[0] [3] => topmostSubform[0].Page1[0].ica_account_no[0] [4] => topmostSubform[0].Page1[0].SMART_ID___Row_1[0] [5] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_1[0] [6] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_1[0] [7] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_1[0] [8] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_1[0] [9] => topmostSubform[0].Page1[0].TOTAL__Row_1[0] [10] => topmostSubform[0].Page1[0].SMART_ID___Row_2[0] [11] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_2[0] [12] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_2[0] [13] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_2[0] [14] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_2[0] [15] => topmostSubform[0].Page1[0].TOTAL__Row_2[0] [16] => topmostSubform[0].Page1[0].SMART_ID___Row_3[0] [17] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_3[0] [18] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_3[0] [19] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_3[0] [20] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_3[0] [21] => topmostSubform[0].Page1[0].TOTAL__Row_3[0] [22] => topmostSubform[0].Page1[0].SMART_ID___Row_4[0] [23] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_4[0] [24] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_4[0] [25] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_4[0] [26] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_4[0] [27] => topmostSubform[0].Page1[0].TOTAL__Row_4[0] [28] => topmostSubform[0].Page1[0].SMART_ID___Row_5[0] [29] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_5[0] [30] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_5[0] [31] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_5[0] [32] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_5[0] [33] => topmostSubform[0].Page1[0].TOTAL__Row_5[0] [34] => topmostSubform[0].Page1[0].SMART_ID___Row_6[0] [35] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_6[0] [36] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_6[0] [37] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_6[0] [38] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_6[0] [39] => topmostSubform[0].Page1[0].TOTAL__Row_6[0] [40] => topmostSubform[0].Page1[0].SMART_ID___Row_7[0] [41] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_7[0] [42] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_7[0] [43] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_7[0] [44] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_7[0] [45] => topmostSubform[0].Page1[0].TOTAL__Row_7[0] [46] => topmostSubform[0].Page1[0].SMART_ID___Row_8[0] [47] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_8[0] [48] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_8[0] [49] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_8[0] [50] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_8[0] [51] => topmostSubform[0].Page1[0].TOTAL__Row_8[0] [52] => topmostSubform[0].Page1[0].SMART_ID___Row_9[0] [53] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_9[0] [54] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_9[0] [55] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_9[0] [56] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_9[0] [57] => topmostSubform[0].Page1[0].TOTAL__Row_9[0] [58] => topmostSubform[0].Page1[0].SMART_ID___Row_10[0] [59] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_10[0] [60] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_10[0] [61] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_10[0] [62] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_10[0] [63] => topmostSubform[0].Page1[0].TOTAL__Row_10[0] [64] => topmostSubform[0].Page1[0].SMART_ID___Row_11[0] [65] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_11[0] [66] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_11[0] [67] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_11[0] [68] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_11[0] [69] => topmostSubform[0].Page1[0].TOTAL__Row_11[0] [70] => topmostSubform[0].Page1[0].SMART_ID___Row_12[0] [71] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_12[0] [72] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_12[0] [73] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_12[0] [74] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_12[0] [75] => topmostSubform[0].Page1[0].TOTAL__Row_12[0] [76] => topmostSubform[0].Page1[0].SMART_ID___Row_13[0] [77] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_13[0] [78] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_13[0] [79] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_13[0] [80] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_13[0] [81] => topmostSubform[0].Page1[0].TOTAL__Row_13[0] [82] => topmostSubform[0].Page1[0].SMART_ID___Row_14[0] [83] => topmostSubform[0].Page1[0].CLIENT_NAME__Row_14[0] [84] => topmostSubform[0].Page1[0].BUSINESS_NAME__Row_14[0] [85] => topmostSubform[0].Page1[0].INITIAL_PAYMENT__Row_14[0] [86] => topmostSubform[0].Page1[0].TRAILING_FEE__Row_14[0] [87] => topmostSubform[0].Page1[0].TOTAL__Row_14[0] [88] => topmostSubform[0].Page1[0].subtotal[0] [89] => topmostSubform[0].Page1[0].gst[0] [90] => topmostSubform[0].Page1[0].total_due[0] [91] => topmostSubform[0].Page1[0].ica_name[1] [92] => topmostSubform[0].Page1[0].ica_address[0] [93] => topmostSubform[0].Page1[0].ica_phone[0] [94] => topmostSubform[0].Page1[0].comments[0] [95] => topmostSubform[0].Page1[0].invoice_number[0] [96] => topmostSubform[0].Page1[0].invoice_date[0] [97] => topmostSubform[0].Page1[0].date_range[0] ) I still can't solve this problem. It seems like what i have done SHOULD work. Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-793167 Share on other sites More sharing options...
sasa Posted March 25, 2009 Share Posted March 25, 2009 last one (row15) doesn0t exist Link to comment https://forums.phpfreaks.com/topic/150059-variable-in-array-key/#findComment-793339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.