aliento Posted October 13, 2011 Share Posted October 13, 2011 Hello , I have a table like $data[$var][$i] $data is an string, $var is a string and $i is an index int. I am tired to write for($i=1;,$i<=count($$data[$var],$i++) { echo $data['id'][$i] ... }oouuuff How i can make it with foreach ? I tried foreach ($data -> $field as $value) echo $value; but i doesnt work. Any advise ? Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/ Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 I haven't tried this but would this work: foreach ($data[$var] -> $field as $value) Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278870 Share on other sites More sharing options...
aliento Posted October 13, 2011 Author Share Posted October 13, 2011 No it does not work returns Warning: Invalid argument supplied for foreach() Thank you. Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278875 Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 Can you try nesting foreach statements? foreach ($data -> $field as $value) { foreach($value -> $field2 as $value2) { } } Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278877 Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 $data needs to be an array, not a string. Also, you shouldn't use count() in a loop like that. Just store it in a variable before the loop. Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278878 Share on other sites More sharing options...
aliento Posted October 13, 2011 Author Share Posted October 13, 2011 @titan21 No again it returns the same. @Kira $data is an array and $var is an array but not integers. The $i is int i mean. Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278882 Share on other sites More sharing options...
titan21 Posted October 13, 2011 Share Posted October 13, 2011 Can you send us the output from print_r($data) Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278884 Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 @titan21 No again it returns the same. @Kira $data is an array and $var is an array but not integers. The $i is int i mean. $var should be a string as you're using it as a key. Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278886 Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 Is this what you're trying to do? $var = 'key'; $data['key'] = array( 'item0', 'item1', 'item2' ); foreach ($data[$var] as $item) { echo "{$item}<br />"; } Will output: item0 item1 item2 Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278888 Share on other sites More sharing options...
aliento Posted October 13, 2011 Author Share Posted October 13, 2011 This is the array : $data = array( /* ("field")*/ array(1, 2 , 3)), array(array(1, 2 , 3)), array(array(1, 2 , 3)) ); Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278892 Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 Okay, so there's no key to call on, therefore I'm assuming you want to loop through them all. What I don't understand is why you would use an array within an array making the inner array the first item of the outer array, which is pointless unless there's multiple arrays in it. A more sensible array would be something like: $data = array( 'key1' => array(1, 2, 3), 'key2' => array(1, 2, 3), 'key3' => array(1, 2, 3) ); Then you could call on it by the key of choice (as it looks like you were trying to do by the previous posts). $data = array( 'key1' => array(1, 2, 3), 'key2' => array(4, 5, 6), 'key3' => array(7, 8, 9) ); $var = 'key2'; foreach ($data[$var] as $item) { echo "{$item}<br />"; } Outputs 4 5 6 Hopefully I'm correct on what you're trying to do. Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278895 Share on other sites More sharing options...
aliento Posted October 13, 2011 Author Share Posted October 13, 2011 It works ! Thank you Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278896 Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 It works ! Thank you Yup Link to comment https://forums.phpfreaks.com/topic/249016-foreach-and-datavari-arrays/#findComment-1278897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.