2

I always have trouble wrapping my head around these foreach array things, and SO has been an incredibly value resource in getting me there, so I hope you guys can help with this one.

public function progress_bar()
{

    $items = array(
        array(
            'step-name' => 'Setup',
            'url' => '/projects/new/setup/',
        ),
        array(
            'step-name' => 'Artwork',
            'url' => '/projects/new/artwork/',
        ),
        array(
            'step-name' => 'Review',
            'url' => '/projects/new/review/',
        ),
        array(
            'step-name' => 'Shipping',
            'url' => '/projects/new/shipping-info/',
        ),
        array(
            'step-name' => 'Billing',
            'url' => '/projects/new/billing/',
        ),
        array(
            'step-name' => 'Receipt',
            'url' => '/projects/new/receipt/',
        ),
    );

    // Status can be active, editing, or complete.

    foreach ($this->progress as $step => $status)
    {
        foreach ($items as $item)
        {
            $item['step-name'] == ucfirst($step) ? $item['status'] = $status : '';
        }
    }

    return $items;
}

$this->progress contains an array of statuses ('setup' => 'active', 'artwork' => 'editing')

I want to add to the $items array the status of each matching item in $this->progress

$items = array(
    array(
        'step-name' => 'Setup',
        'url' => '/projects/new/setup',
        'status' => 'active',
    ),
    etc...
);

3 Answers 3

1

If I understand your question correctly, the problem is that you are trying to add an array element to $items, but what you're actually doing is adding the element to a temporary variable ($item), which does not reference the original $items variable.

I'd suggest approaching it like this:

foreach ($this->progress as $step => $status)
{
    // Having the $key here allows us to reference the
    // original $items variable.
    foreach ($items as $key => $item)
    {
        if ($item['step-name'] == ucfirst($step) )
        {
            $items[$key]['status'] = $status;
        }
    }
}

return $items;
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you did understand correctly, and yes I recognized it was temporary but didn't know how to make it permanent, and yes, you rule and saved the day!!
Excellent on all fronts then!
0

Are you locked into using an array to store $items? If so, you're going to be stuck doing a nested loop ("For each element in $this->progress, check each element in $items. If match, update $items" or something like that). If you have some flexibility, I would use a hash for $items (associative array in php-speak), where the index is the step name. So $items['Setup'] would contain 'url' => ... and 'status' => ... etc. Does that make sense? Then your algorithm breaks down to "For each element in $this->progress, get the element in $items by name ($items[$step_name]) and update it's info."

Comments

0

I would change how you have the $items array keyed and do it like this. Stops you from having the nested loops.

public function progress_bar()
{

    $items = array(
        'Setup' => array(
                'url' => '/projects/new/setup/',
               ),
        'Artwork' => array(
                    'url' => '/projects/new/artwork/',
                ),
        'Review' => array(
                    'url' => '/projects/new/review/',
                ),
        'Shipping' => array(
                    'url' => '/projects/new/shipping-info/',
                ),
        'Billing' => array(
                    'url' => '/projects/new/billing/',
                ),
        'Receipt' => array(
                    'url' => '/projects/new/receipt/',
                )
    );

    // Status can be active, editing, or complete.

    foreach ($this->progress as $step => $status)
    {
        $item[ucfirst($step)]['status'] = $status;
    }

    return $items;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.