strongbad Posted March 29, 2011 Share Posted March 29, 2011 I am fairly new to PHP, coming over from JAVA. In JAVA, you can select an element of an array and set it as a separate variable, such as: String name="": name = array[4]; But in PHP I am trying this: $ban = $banners[$selected_banner]; Where $selected_banner is a random number. Unfortunately, I am gettting the following error... Catchable fatal error: Object of class stdClass could not be converted to string Are arrays in PHP able to be used in such a way? What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/ Share on other sites More sharing options...
litebearer Posted March 29, 2011 Share Posted March 29, 2011 Simple answer = yes. ie... <?PHP $myarray = array("red", "white", "blue"); $some_number = 2; $color = $myarray[$some_number]; echo $color; /* would show blue */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/#findComment-1193495 Share on other sites More sharing options...
strongbad Posted March 29, 2011 Author Share Posted March 29, 2011 Simple answer = yes. ie... <?PHP $myarray = array("red", "white", "blue"); $some_number = 2; $color = $myarray[$some_number]; echo $color; /* would show blue */ ?> This is pretty much what I have going on. If I use: echo $banners[$selected_banner]; It prints out the propper URL. But if I try to assign it, it throws that error. Is the problem because it's a URL? What am I missing here? Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/#findComment-1193527 Share on other sites More sharing options...
kenrbnsn Posted March 29, 2011 Share Posted March 29, 2011 Please show us your code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/#findComment-1193536 Share on other sites More sharing options...
litebearer Posted March 29, 2011 Share Posted March 29, 2011 try doing a print_r of the array Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/#findComment-1193584 Share on other sites More sharing options...
strongbad Posted March 29, 2011 Author Share Posted March 29, 2011 try doing a print_r of the array Thanks Litebearer. I printed it out with print_r and realized it was a multi-dimensional array. Thanks for the suggestion! Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/#findComment-1193707 Share on other sites More sharing options...
Adam Posted March 29, 2011 Share Posted March 29, 2011 Are you aware of the array_rand() function by the way? Sounds like it may be of use to you here. Quote Link to comment https://forums.phpfreaks.com/topic/232000-newbie-array-question/#findComment-1193746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.