Joss Posted March 20, 2015 Share Posted March 20, 2015 I am trying to use array_chunk to split a pile of results from a table field into two columns This is my code: $testimonials = $pages->get("/")->testimonials; $testcount = $testimonials->count(); $resultnum = ceil($testcount / 2); echo $resultnum; foreach(array_chunk($testimonials, $resultnum) as $list){ echo "<div class='block block12 '>"; echo "<p>hello column</p>"; foreach($list as $test){ echo "<p class='quote'>{$test->text}helo</p>"; echo "<p class='from'>{$test->from}</p>"; } //end inner foreach echo "</div>"; } The problem, according to debug, is that it says that: Warning: array_chunk() expects parameter 1 to be array, object given in /home/justright/public_html/site/templates/testimonials.php on line 30 Parameter 1 is $testimonials. I am echoing out $resultnum, and that is giving the right value and I have used my selector without array_chunk and it is definitely outputting the content of the table field. So why does it not work in the above case? Joss Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 20, 2015 Share Posted March 20, 2015 While WireArray objects of ProcessWire are countable they are still not real arrays, so you need to use getArray() on the object to get the array of a WireArray object. Then you can use all the array functions. 5 Link to comment Share on other sites More sharing options...
Joss Posted March 20, 2015 Author Share Posted March 20, 2015 Solved - I needed to tell it what it was: $testimonials = $pages->get("/")->testimonials; $testcount = $testimonials->count(); $resultnum = ceil($testcount / 2); echo $resultnum; foreach(array_chunk($testimonials->getArray(), $resultnum) as $list){ echo "<div class='block block12 '>"; echo "<p>hello column</p>"; foreach($list as $test){ echo "<p class='quote'>{$test->text}helo</p>"; echo "<p class='from'>{$test->from}</p>"; } //end inner foreach echo "</div>"; } So, added getArray Thanks to this little post as a clue from Netcarver https://processwire.com/talk/topic/7803-implement-array-chunk/?p=75573 Aww! You beat me to it, LostKobrakai 3 Link to comment Share on other sites More sharing options...
kongondo Posted March 20, 2015 Share Posted March 20, 2015 Hey, he liveth! 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now