DBHelper::verticalSlice PHP Method

verticalSlice() public static method

* verticalSlice 1. For an array of assoc rays, return an array of values for a particular key 2. if $keyfield is given, same as above but use that hash key as the key in new array
public static verticalSlice ( $array, $field, $keyfield = null )
    public static function verticalSlice($array, $field, $keyfield = null)
    {
        $array = (array) $array;
        $R = array();
        foreach ($array as $obj) {
            if (!array_key_exists($field, $obj)) {
                die("verticalSlice: array doesn't have requested field\n");
            }
            if ($keyfield) {
                if (!array_key_exists($keyfield, $obj)) {
                    die("verticalSlice: array doesn't have requested field\n");
                }
                $R[$obj[$keyfield]] = $obj[$field];
            } else {
                $R[] = $obj[$field];
            }
        }
        return $R;
    }

Usage Example

Example #1
0
function displayAllMainPageCategoryItems()
{
    $category_ids = DBHelper::verticalSlice(Category::getCategories(), "id");
    $html = "";
    for ($i = 0; $i < count($category_ids); $i++) {
        $html .= sprintf('<div data-tab="%d" class="category-items hidden">', $i + 1);
        $html .= displayMainPageCategoryItems($category_ids[$i]);
        $html .= '</div>';
    }
    return $html;
}
All Usage Examples Of DBHelper::verticalSlice