I'm trying to create a very simple database-driven block builder, for some fun Christmas coding. At the moment I get all the blocks in the correct order from the database and then run a foreach to loop through them:
function get_blocks() {
global $db;
$GLOBALS['current_page_id'] = get_page_id();
$stm = $db->prepare("SELECT * FROM page_blocks WHERE page_id = :id ORDER BY `block_order` ASC");
$stm->execute(array(':id' => 1));
$res = $stm->fetchAll();
return $res;
}
$blocks = get_blocks();
foreach($blocks as $block) {
if($block['block_name'] == 'block-type-1') {
//code to execute
}
}
It works, but I need to be able to apply a function to each block similar to (and yes, I know this won't work):
foreach($blocks as have_block($block))
Is there a way to loop through the blocks from the database in order and then apply a function to the result?
I think that array_walk
is what you need: http://php.net/manual/en/function.array-walk.php
array_walk($blocks, 'some_function');