phppup Posted January 27 Share Posted January 27 (edited) I'm trying to include an array inside of a different array, but having trouble reaching the data. $ingredients = ["sugar", "flour", "water"]; $spec_recipe = ["apple", "cinnamon", $ingredients]; echo $spec_recipe[2]; RESULT: Array echo $spec_recipe[2][0]; RESULT: A (assuming first letter of Array) How do I move this info correctly so that it's accessible? Edited January 27 by phppup Forgot item Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/ Share on other sites More sharing options...
gw1500se Posted January 27 Share Posted January 27 Not sure how you want it but you can use array_merge. $ingredients = ["sugar", "flour", "water"]; $spec_recipe = array_merge(["apple", "cinnamon"], $ingredients); Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/#findComment-1648599 Share on other sites More sharing options...
phppup Posted January 27 Author Share Posted January 27 @gw1500se No luck. In not sure if it matters that this variable is stuck into a sub array. This started out working well with single words or phrases. But when I incorporated an array to provide those words and phrases, it went bonkers. I felt that the array structure was getting cluttered and that variables would help organize it. Was I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/#findComment-1648602 Share on other sites More sharing options...
Solution mac_gyver Posted January 27 Solution Share Posted January 27 your example works as expected for me. what php version are you using? what does using - echo '<pre>'; print_r($spec_recipe); echo '</pre>'; show for the data? Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/#findComment-1648603 Share on other sites More sharing options...
phppup Posted January 27 Author Share Posted January 27 (edited) @mac_gyver Save as my initial info in this question. Both direct access efforts and print_r: [6] => Array The data is not getting pulled by the variable, yet it knows there's an array present. Not sure if exact version, but current. Edited January 27 by phppup Forgot item Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/#findComment-1648607 Share on other sites More sharing options...
phppup Posted January 27 Author Share Posted January 27 ** Recent development ** I modified my approach and got a response //At first $ingredients = ["sugar", "flour", "water"]; $spec_recipe = ["apple", "cinnamon", $ingredients]; echo $spec_recipe[2]; RESULT: Array echo $spec_recipe[2][0]; RESULT: A (assuming first letter of Array) Now I altered the variable to specify an element $ingredients = ["sugar", "flour", "water"]; $spec_recipe = ["apple", "cinnamon", $ingredients[0] ]; echo $spec_recipe[2]; RESULT: sugar I suppose this is something, but I want the array to become available through this variable so that I can address the full array contents. What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/#findComment-1648608 Share on other sites More sharing options...
phppup Posted January 27 Author Share Posted January 27 Used the suggested tools and discovered my problem. The array variable was inside a string. Apparrently this negates the array tendencies once they are lost. Quote Link to comment https://forums.phpfreaks.com/topic/326661-getting-array-data-in-variable/#findComment-1648614 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.