1

i have this array

    Array
(
    [disc-art] => Array
        (
            [original] => upload/c/4/c4eea45ce0f5e8d5698f5ff2d18b4566.jpg
            [preview] => Array
                (
                    [dvd-disc] => upload/c/4/d_disc_c4eea45ce0f5e8d5698f5ff2d18b4566.jpg
                )

        )

    [cover-art] => Array
        (
            [original] => upload/7/2/72bb5a03708c99c822a792c76c00e8d1.jpg
            [preview] => Array
                (
                    [dvd-cover] => upload/7/2/d_cover_72bb5a03708c99c822a792c76c00e8d1.jpg
                    [dvd-spine] => upload/7/2/d_spine_72bb5a03708c99c822a792c76c00e8d1.jpg
                    [dvd-back] => upload/7/2/d_back_72bb5a03708c99c822a792c76c00e8d1.jpg
                )

        )

    [insert-art] => Array
        (
            [original] => upload/b/1/b1f8e49d77121c01011acaa90cabc8ee.jpg
            [preview] => Array
                (
                    [dvd-insert] => upload/b/1/d_insert_b1f8e49d77121c01011acaa90cabc8ee.jpg
                )

        )

    [boxshot] => Array
        (
            [preview] => Array
                (
                    [boxshot] => upload/7/2/d_boxshot_72bb5a03708c99c822a792c76c00e8d1.jpg
                )

        )

)

I want to return an array that accesses each preview and gets the contents

array('dvd-disc' => '...path...', 'dvd-cover' => '...path...', etc...)

Please help, it has been a long day and I want to go home! :)

EDIT:

Correction - I want my final array to look like this...

array(
    'dvd-disc' => '<img src="'.$path.'" alt="'.$type.'" />',
    'dvd-cover' => '<img src="'.$path.'" alt="'.$type.'" />', 
    etc...
);

where $path is the value of each preview's sub-value and $type is the value of each preview's sub-key

3
  • "I want to go home!": You could still post what you have tried, or what your particular problem is ;) Don't you know how to access the elements? Or how to combine them? Commented Jul 2, 2011 at 0:24
  • @Felix Kling Sorry, I have tried to accomplish the problem to no avail with foreach loops and array_push, but I can't seem to wrestle this one down. Commented Jul 2, 2011 at 0:28
  • What are you looking for for $type? Commented Jul 2, 2011 at 0:31

2 Answers 2

3
$new_array = array();
foreach($main_array as $sub_array)
{
   if(isset($sub_array['preview']))
   {
       foreach($sub_array['preview'] as $type => $image_url)
       {
           $new_array[$type] = '<img src="'.$image_url.'" alt="'.$type.'" />';
       }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Tim - you answered my original question, but my brain is fried and that actually wasn't what I was looking for. Should I ask a new question, or can you edit yours here?
@Dave: That should do it now.
2
// (the array you are bring in is $mainArray)

// New array we'll be creating
$previewPaths = array()

foreach ($mainArray as $item) {

    foreach ($item['preview'] as $previewName => $previewPath) {

        $previewPaths[$previewName] = '<img src="'.$previewPath.'" alt="'.$previewName.'" />',;

    }

}

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.