SarahB12 Posted January 19, 2014 Share Posted January 19, 2014 Hey guys, I'm having the hardest time trying to access data within my foreach loop. Here is the var_dump right before the foreach. array(19) { [0]=> string( "Thursday" [1]=> string( "Saturday" [2]=> string(6) "Monday" [3]=> string(6) "Friday" [4]=> string(6) "Monday" [5]=> string(9) "Wednesday" [6]=> string(7) "Tuesday" [7]=> string(7) "Tuesday" [8]=> string( "Thursday" [9]=> string(6) "Sunday" [10]=> string(7) "Tuesday" [11]=> string(7) "Tuesday" [12]=> string(6) "Monday" [13]=> string(6) "Friday" [14]=> string(9) "Wednesday" [15]=> string(6) "Monday" [16]=> string(9) "Wednesday" [17]=> string(6) "Sunday" [18]=> string(6) "Monday" } array(19) { [0]=> string(90) "http://www.mylink.com/1" [1]=> string(90) "http://www.mylink.com/2" [2]=> string(90) "http://www.mylink.com/3" [3]=> string(90) "http://www.mylink.com/4" [4]=> string(90) "http://www.mylink.com/5" [5]=> string(90) "http://www.mylink.com/6" [6]=> string(90) "http://www.mylink.com/7" [7]=> string(90) "http://www.mylink.com/8" [8]=> string(90) "http://www.mylink.com/9" [9]=> string(90) "http://www.mylink.com/10" [10]=> string(90) "http://www.mylink.com/11" [11]=> string(90) "http://www.mylink.com/12" [12]=> string(90) "http://www.mylink.com/13" [13]=> string(90) "http://www.mylink.com/14" [14]=> string(90) "http://www.mylink.com/15" [15]=> string(90) "http://www.mylink.com/16" [16]=> string(90) "http://www.mylink.com/17" [17]=> string(0) "" [18]=> string(0) "" } array(19) { [0]=> string(6) "715.00" [1]=> string(6) "912.00" [2]=> string(6) "715.00" [3]=> string(6) "715.00" [4]=> string(6) "715.00" [5]=> string(6) "715.00" [6]=> string(6) "715.00" [7]=> string(6) "912.00" [8]=> string(6) "715.00" [9]=> string(6) "715.00" [10]=> string(6) "715.00" [11]=> string(6) "715.00" [12]=> string(6) "715.00" [13]=> string(6) "912.00" [14]=> string(6) "912.00" [15]=> string(6) "715.00" [16]=> string(6) "715.00" [17]=> string(6) "715.00" [18]=> string(6) "912.00" } array(19) { [0]=> string(6) "330.00" [1]=> string(6) "400.00" [2]=> string(6) "330.00" [3]=> string(6) "330.00" [4]=> string(6) "330.00" [5]=> string(6) "330.00" [6]=> string(6) "330.00" [7]=> string(6) "400.00" [8]=> string(6) "330.00" [9]=> string(6) "330.00" [10]=> string(6) "330.00" [11]=> string(6) "330.00" [12]=> string(6) "330.00" [13]=> string(6) "400.00" [14]=> string(6) "400.00" [15]=> string(6) "330.00" [16]=> string(6) "330.00" [17]=> string(6) "330.00" [18]=> string(6) "400.00" } array(19) { [0]=> string(6) "220.00" [1]=> string(6) "275.00" [2]=> string(6) "220.00" [3]=> string(6) "220.00" [4]=> string(6) "220.00" [5]=> string(6) "220.00" [6]=> string(6) "220.00" [7]=> string(6) "275.00" [8]=> string(6) "220.00" [9]=> string(6) "220.00" [10]=> string(6) "220.00" [11]=> string(6) "220.00" [12]=> string(6) "220.00" [13]=> string(6) "275.00" [14]=> string(6) "275.00" [15]=> string(6) "220.00" [16]=> string(6) "220.00" [17]=> string(6) "220.00" [18]=> string(6) "275.00" } array(19) { [0]=> string(6) "187.00" [1]=> string(6) "225.00" [2]=> string(6) "187.00" [3]=> string(6) "187.00" [4]=> string(6) "187.00" [5]=> string(6) "187.00" [6]=> string(6) "187.00" [7]=> string(6) "225.00" [8]=> string(6) "187.00" [9]=> string(6) "187.00" [10]=> string(6) "187.00" [11]=> string(6) "187.00" [12]=> string(6) "187.00" [13]=> string(6) "225.00" [14]=> string(6) "225.00" [15]=> string(6) "187.00" [16]=> string(6) "187.00" [17]=> string(6) "187.00" [18]=> string(6) "225.00" } array(19) { [0]=> string(6) "157.00" [1]=> string(6) "192.00" [2]=> string(6) "157.00" [3]=> string(6) "157.00" [4]=> string(6) "157.00" [5]=> string(6) "157.00" [6]=> string(6) "157.00" [7]=> string(6) "192.00" [8]=> string(6) "157.00" [9]=> string(6) "157.00" [10]=> string(6) "157.00" [11]=> string(6) "157.00" [12]=> string(6) "157.00" [13]=> string(6) "192.00" [14]=> string(6) "192.00" [15]=> string(6) "157.00" [16]=> string(6) "157.00" [17]=> string(6) "157.00" [18]=> string(6) "192.00" } array(19) { [0]=> string(6) "138.00" [1]=> string(6) "165.00" [2]=> string(6) "138.00" [3]=> string(6) "138.00" [4]=> string(6) "138.00" [5]=> string(6) "138.00" [6]=> string(6) "138.00" [7]=> string(6) "165.00" [8]=> string(6) "138.00" [9]=> string(6) "138.00" [10]=> string(6) "138.00" [11]=> string(6) "138.00" [12]=> string(6) "138.00" [13]=> string(6) "165.00" [14]=> string(6) "165.00" [15]=> string(6) "138.00" [16]=> string(6) "138.00" [17]=> string(6) "138.00" [18]=> string(6) "165.00" } array(19) { [0]=> string(5) "89.00" [1]=> string(6) "125.00" [2]=> string(5) "75.00" [3]=> string(5) "95.00" [4]=> string(5) "86.00" [5]=> string(5) "86.00" [6]=> string(5) "75.00" [7]=> string(6) "150.00" [8]=> string(5) "79.00" [9]=> string(5) "77.00" [10]=> string(6) "103.00" [11]=> string(6) "103.00" [12]=> string(5) "75.00" [13]=> string(6) "118.00" [14]=> string(6) "125.00" [15]=> string(5) "89.00" [16]=> string(5) "75.00" [17]=> string(5) "86.00" [18]=> string(6) "135.00" } array(19) { [0]=> string(5) "97.00" [1]=> string(6) "115.00" [2]=> string(5) "97.00" [3]=> string(5) "97.00" [4]=> string(5) "97.00" [5]=> string(5) "97.00" [6]=> string(5) "97.00" [7]=> string(6) "115.00" [8]=> string(5) "97.00" [9]=> string(5) "97.00" [10]=> string(5) "97.00" [11]=> string(5) "97.00" [12]=> string(5) "97.00" [13]=> string(6) "115.00" [14]=> string(6) "115.00" [15]=> string(5) "97.00" [16]=> string(5) "97.00" [17]=> string(5) "97.00" [18]=> string(6) "115.00" } array(19) { [0]=> string(5) "81.00" [1]=> string(6) "105.00" [2]=> string(5) "60.00" [3]=> string(5) "81.00" [4]=> string(5) "71.00" [5]=> string(5) "71.00" [6]=> string(5) "61.00" [7]=> string(6) "125.00" [8]=> string(5) "69.00" [9]=> string(5) "69.00" [10]=> string(5) "81.00" [11]=> string(5) "81.00" [12]=> string(5) "60.00" [13]=> string(5) "96.00" [14]=> string(6) "105.00" [15]=> string(5) "75.00" [16]=> string(5) "60.00" [17]=> string(5) "71.00" [18]=> string(6) "115.00" } array(19) { [0]=> string(5) "78.00" [1]=> string(5) "90.00" [2]=> string(5) "78.00" [3]=> string(5) "78.00" [4]=> string(5) "78.00" [5]=> string(5) "78.00" [6]=> string(5) "78.00" [7]=> string(5) "90.00" [8]=> string(5) "78.00" [9]=> string(5) "78.00" [10]=> string(5) "78.00" [11]=> string(5) "78.00" [12]=> string(5) "78.00" [13]=> string(5) "90.00" [14]=> string(5) "90.00" [15]=> string(5) "78.00" [16]=> string(5) "78.00" [17]=> string(5) "78.00" [18]=> string(5) "90.00" } array(19) { [0]=> string(5) "50.00" [1]=> string(5) "60.00" [2]=> string(5) "50.00" [3]=> string(5) "50.00" [4]=> string(5) "50.00" [5]=> string(5) "50.00" [6]=> string(5) "50.00" [7]=> string(5) "60.00" [8]=> string(5) "50.00" [9]=> string(5) "50.00" [10]=> string(5) "50.00" [11]=> string(5) "50.00" [12]=> string(5) "50.00" [13]=> string(5) "60.00" [14]=> string(5) "60.00" [15]=> string(5) "50.00" [16]=> string(5) "50.00" [17]=> string(5) "50.00" [18]=> string(5) "60.00" } array(19) { [0]=> string(5) "28.00" [1]=> string(5) "35.00" [2]=> string(5) "28.00" [3]=> string(5) "28.00" [4]=> string(5) "28.00" [5]=> string(5) "28.00" [6]=> string(5) "28.00" [7]=> string(5) "35.00" [8]=> string(5) "28.00" [9]=> string(5) "28.00" [10]=> string(5) "28.00" [11]=> string(5) "28.00" [12]=> string(5) "28.00" [13]=> string(5) "35.00" [14]=> string(5) "35.00" [15]=> string(5) "28.00" [16]=> string(5) "28.00" [17]=> string(5) "28.00" [18]=> string(5) "35.00" } array(19) { [0]=> string(5) "13.00" [1]=> string(5) "25.00" [2]=> string(5) "13.00" [3]=> string(5) "13.00" [4]=> string(5) "13.00" [5]=> string(5) "13.00" [6]=> string(5) "13.00" [7]=> string(5) "25.00" [8]=> string(5) "13.00" [9]=> string(5) "13.00" [10]=> string(5) "13.00" [11]=> string(5) "13.00" [12]=> string(5) "13.00" [13]=> string(5) "25.00" [14]=> string(5) "25.00" [15]=> string(5) "13.00" [16]=> string(5) "13.00" [17]=> string(5) "13.00" [18]=> string(5) "25.00" } Here is the foreach: foreach ($values as $cell) { echo "<a href=\"".$link."\" class=\"".$class."\">" . $cell . "</a>"; } $class is supposed to be the first iteration of the array: Thursday, Saturday, Monday, etc.$link is supposed the be the second iteration of the array: http://www.mylink.com/... I can get the $cell data, but I cannot access the $link or $class values. Thank you so much for any help you can provide. ~SarahB Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted January 19, 2014 Share Posted January 19, 2014 Where did you get the variables $class and $link from? Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted January 19, 2014 Author Share Posted January 19, 2014 Sorry - I forgot to explain that. $class and $link are not defined yet - right now these are placeholders more than anything else. I'm having issues defining these within the foreach loop. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted January 19, 2014 Share Posted January 19, 2014 So $values contains 14 arrays, each with 19 values? Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted January 19, 2014 Author Share Posted January 19, 2014 $values contains 15 arrays with 19 values. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted January 19, 2014 Share Posted January 19, 2014 You would need to iterate over the values in each $cell. Since $cell is an array I don't know what you want to echo with it since you cannot echo an array. foreach ($values as $cell) { $links = $cell[0]; $classes = $cell[1]; foreach ($links as $key => $link) { echo $link; echo $classes[$key]; } } Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted January 19, 2014 Author Share Posted January 19, 2014 Here is what I'm looking to do. I want to loop through all of the $cell values which are the numbers but $link and $class need to go to the next entry after the values have ended on the array. Here's an example: <a href="http://www.mylink.com/1" class="Thursday">715.00 </a> <a href="http://www.mylink.com/1" class="Thursday">330.00 </a> <a href="http://www.mylink.com/1" class="Thursday">220.00 </a> <a href="http://www.mylink.com/1" class="Thursday">187.00 </a> <a href="http://www.mylink.com/1" class="Thursday">157.00 </a> <a href="http://www.mylink.com/1" class="Thursday">138.00 </a> <a href="http://www.mylink.com/1" class="Thursday">89.00 </a> <a href="http://www.mylink.com/1" class="Thursday">97.00 </a> <a href="http://www.mylink.com/1" class="Thursday">81.00 </a> <a href="http://www.mylink.com/1" class="Thursday">78.00 </a> <a href="http://www.mylink.com/1" class="Thursday">50.00 </a> <a href="http://www.mylink.com/1" class="Thursday">28.00 </a> <a href="http://www.mylink.com/1" class="Thursday">13.00 </a> <a href="http://www.mylink.com/2" class="Saturday">912.00 </a> <a href="http://www.mylink.com/2" class="Saturday">400.00 </a> etc.... Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 19, 2014 Share Posted January 19, 2014 $classes = array_shift($values); // get the 1st array of class values $links = array_shift($values); // get the 2nd array of links // $values is just the remaining arrays of values at this point and can be repetitively looped over foreach($classes as $key=>$class){ foreach($values as $arr){ echo "<a href='$links[$key]' class='$class'>$arr[$key]</a><br>\n"; } } Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted January 19, 2014 Author Share Posted January 19, 2014 The array_shift is taking the first value out of each of the arrays. Here is the result for $classes: Thursday http://www.mylink.com/1 715.00 330.00 220.00 187.00 157.00 138.00 89.00 97.00 81.00 78.00 50.00 28.00 13.00 and here is the result for $links: Saturday http://www.mylink.com/2 912.00 400.00 275.00 225.00 192.00 165.00 125.00 115.00 105.00 90.00 60.00 35.00 25.00 Is there a way to assign the first array to $classes and the second array to $links, and then iterate through the remaining arrays? $classes = array_shift($values); // get the 1st array of class values $links = array_shift($values); // get the 2nd array of links // $values is just the remaining arrays of values at this point and can be repetitively looped over foreach($classes as $key=>$class){ foreach($values as $arr){ echo "<a href='$links[$key]' class='$class'>$arr[$key]</a><br>\n"; } } Thank you Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 19, 2014 Share Posted January 19, 2014 Is there a way to assign the first array to $classes and the second array to $links, and then iterate through the remaining arrays? that's exactly what the code i posted does. if the result you got from that code isn't correct, that would mean that your posted data definition is inaccurate and it's impossible to write code without accurate information. Quote Link to comment Share on other sites More sharing options...
SarahB12 Posted January 19, 2014 Author Share Posted January 19, 2014 (edited) Here are the results for $values: Array ( [0] => Thursday [1] => Saturday [2] => Monday [3] => Friday [4] => Monday [5] => Wednesday [6] => Tuesday [7] => Tuesday [8] => Thursday [9] => Sunday [10] => Tuesday [11] => Tuesday [12] => Monday [13] => Friday [14] => Wednesday [15] => Monday [16] => Wednesday [17] => Sunday [18] => Monday ) Array ( [0] => http://www.mylink.com/1 [1] => http://www.mylink.com/2 [2] => http://www.mylink.com/3 [3] => http://www.mylink.com/4 [4] => http://www.mylink.com/5 [5] => http://www.mylink.com/6 [6] => http://www.mylink.com/7 [7] => http://www.mylink.com/8 [8] => http://www.mylink.com/9 [9] => http://www.mylink.com/10 [10] => http://www.mylink.com/11 [11] => http://www.mylink.com/12 [12] => http://www.mylink.com/13 [13] => http://www.mylink.com/14 [14] => http://www.mylink.com/15 [15] => http://www.mylink.com/16 [16] => http://www.mylink.com/17 [17] => http://www.mylink.com/18 [18] => http://www.mylink.com/19 ) Array ( [0] => 715.00 [1] => 912.00 [2] => 715.00 [3] => 715.00 [4] => 715.00 [5] => 715.00 [6] => 715.00 [7] => 912.00 [8] => 715.00 [9] => 715.00 [10] => 715.00 [11] => 715.00 [12] => 715.00 [13] => 912.00 [14] => 912.00 [15] => 715.00 [16] => 715.00 [17] => 715.00 [18] => 912.00 ) Array ( [0] => 330.00 [1] => 400.00 [2] => 330.00 [3] => 330.00 [4] => 330.00 [5] => 330.00 [6] => 330.00 [7] => 400.00 [8] => 330.00 [9] => 330.00 [10] => 330.00 [11] => 330.00 [12] => 330.00 [13] => 400.00 [14] => 400.00 [15] => 330.00 [16] => 330.00 [17] => 330.00 [18] => 400.00 ) Array ( [0] => 220.00 [1] => 275.00 [2] => 220.00 [3] => 220.00 [4] => 220.00 [5] => 220.00 [6] => 220.00 [7] => 275.00 [8] => 220.00 [9] => 220.00 [10] => 220.00 [11] => 220.00 [12] => 220.00 [13] => 275.00 [14] => 275.00 [15] => 220.00 [16] => 220.00 [17] => 220.00 [18] => 275.00 ) Array ( [0] => 187.00 [1] => 225.00 [2] => 187.00 [3] => 187.00 [4] => 187.00 [5] => 187.00 [6] => 187.00 [7] => 225.00 [8] => 187.00 [9] => 187.00 [10] => 187.00 [11] => 187.00 [12] => 187.00 [13] => 225.00 [14] => 225.00 [15] => 187.00 [16] => 187.00 [17] => 187.00 [18] => 225.00 ) Array ( [0] => 157.00 [1] => 192.00 [2] => 157.00 [3] => 157.00 [4] => 157.00 [5] => 157.00 [6] => 157.00 [7] => 192.00 [8] => 157.00 [9] => 157.00 [10] => 157.00 [11] => 157.00 [12] => 157.00 [13] => 192.00 [14] => 192.00 [15] => 157.00 [16] => 157.00 [17] => 157.00 [18] => 192.00 ) Array ( [0] => 138.00 [1] => 165.00 [2] => 138.00 [3] => 138.00 [4] => 138.00 [5] => 138.00 [6] => 138.00 [7] => 165.00 [8] => 138.00 [9] => 138.00 [10] => 138.00 [11] => 138.00 [12] => 138.00 [13] => 165.00 [14] => 165.00 [15] => 138.00 [16] => 138.00 [17] => 138.00 [18] => 165.00 ) Array ( [0] => 89.00 [1] => 125.00 [2] => 75.00 [3] => 95.00 [4] => 86.00 [5] => 86.00 [6] => 75.00 [7] => 150.00 [8] => 79.00 [9] => 77.00 [10] => 103.00 [11] => 103.00 [12] => 75.00 [13] => 118.00 [14] => 125.00 [15] => 89.00 [16] => 75.00 [17] => 86.00 [18] => 135.00 ) Array ( [0] => 97.00 [1] => 115.00 [2] => 97.00 [3] => 97.00 [4] => 97.00 [5] => 97.00 [6] => 97.00 [7] => 115.00 [8] => 97.00 [9] => 97.00 [10] => 97.00 [11] => 97.00 [12] => 97.00 [13] => 115.00 [14] => 115.00 [15] => 97.00 [16] => 97.00 [17] => 97.00 [18] => 115.00 ) Array ( [0] => 81.00 [1] => 105.00 [2] => 60.00 [3] => 81.00 [4] => 71.00 [5] => 71.00 [6] => 61.00 [7] => 125.00 [8] => 69.00 [9] => 69.00 [10] => 81.00 [11] => 81.00 [12] => 60.00 [13] => 96.00 [14] => 105.00 [15] => 75.00 [16] => 60.00 [17] => 71.00 [18] => 115.00 ) Array ( [0] => 78.00 [1] => 90.00 [2] => 78.00 [3] => 78.00 [4] => 78.00 [5] => 78.00 [6] => 78.00 [7] => 90.00 [8] => 78.00 [9] => 78.00 [10] => 78.00 [11] => 78.00 [12] => 78.00 [13] => 90.00 [14] => 90.00 [15] => 78.00 [16] => 78.00 [17] => 78.00 [18] => 90.00 ) Array ( [0] => 50.00 [1] => 60.00 [2] => 50.00 [3] => 50.00 [4] => 50.00 [5] => 50.00 [6] => 50.00 [7] => 60.00 [8] => 50.00 [9] => 50.00 [10] => 50.00 [11] => 50.00 [12] => 50.00 [13] => 60.00 [14] => 60.00 [15] => 50.00 [16] => 50.00 [17] => 50.00 [18] => 60.00 ) Array ( [0] => 28.00 [1] => 35.00 [2] => 28.00 [3] => 28.00 [4] => 28.00 [5] => 28.00 [6] => 28.00 [7] => 35.00 [8] => 28.00 [9] => 28.00 [10] => 28.00 [11] => 28.00 [12] => 28.00 [13] => 35.00 [14] => 35.00 [15] => 28.00 [16] => 28.00 [17] => 28.00 [18] => 35.00 ) Array ( [0] => 13.00 [1] => 25.00 [2] => 13.00 [3] => 13.00 [4] => 13.00 [5] => 13.00 [6] => 13.00 [7] => 25.00 [8] => 13.00 [9] => 13.00 [10] => 13.00 [11] => 13.00 [12] => 13.00 [13] => 25.00 [14] => 25.00 [15] => 13.00 [16] => 13.00 [17] => 13.00 [18] => 25.00 ) $classes = array_shift($values); // get the 1st array of class values Here are the results for $classes: Thursday http://www.mylink.com/1 715.00 330.00 220.00 187.00 157.00 138.00 89.00 97.00 81.00 78.00 50.00 28.00 13.00 $links = array_shift($values); // get the 2nd array of links Here are the results for $links: Saturday http://www.mylink.com/2 912.00 400.00 275.00 225.00 192.00 165.00 125.00 115.00 105.00 90.00 60.00 35.00 25.00 Edited January 19, 2014 by SarahB12 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 20, 2014 Share Posted January 20, 2014 there's no way the given code, which i tested before posting, can produce the result you are listing. Quote Link to comment 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.