Prepare a custom array based on existing array

I have an array with the following structure:

<?php
$images = array(
    0 => array(
        'step'          => 1,
        'image'         => 'main-image-1.jpg',
        //...
        'extra_images'  => array(
            array (
                'image' => 'extra-image-1.1.jpg',
                //...
            ),
            array (
                'image' => 'extra-image-1.2.jpg',
                //...
            ),
            //...
        )
    ),
    1 => array(
        'step'          => 2,
        'image'         => 'main-image-2.jpg',
        //...
        'extra_images'  => array(
            array (
                'image' => 'extra-image-2.1.jpg',
                //...
            ),
            array (
                'image' => 'extra-image-2.2.jpg',
                //...
            ),
            //...
        )
    ),
    //...
    9 => array(
        'step'          => 10,
        'image'         => 'main-image-10.jpg',
        //...
        'extra_images'  => array(
            array (
                'image' => 'extra-image-10.1.jpg',
                //...
            ),
            array (
                'image' => 'extra-image-10.2.jpg',
                //...
            ),
            //...
        )
    ),

);

Now I want a custom array of only images from both main & extra images but in custom order. For example, the final array that I would like to have:

$finalImages = array(
    // custom ordered images from $images
    // I need these images to appear first from $images array
    array (
        'image' => 'main-image-3.jpg',
        //...
    ),
    array (
        'image' => 'main-image-5.jpg',
        //...
    ),
    array (
        'image' => 'extra-image-5.1.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-5.2.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-1.1.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-1.2.jpg',
        //...
    ),
    array(
        'image' => 'main-image-1.jpg',
        //...
    ),
    // and then rest of the images should follow from the sequence with main image as first followed by extra images:
    array(
        'image' => 'main-image-2.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-2.1.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-2.2.jpg',
        //...
    ),
    // there wont tbe 'main-image-3.jpg' as it's already there in custom order
    array(
        'image' => 'extra-image-3.1.jpg', //
        //...
    ),
    array(
        'image' => 'extra-image-3.2.jpg',
        //...
    ),
    //...
    array(
        'image' => 'main-image-9.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-9.1.jpg',
        //...
    ),
    array(
        'image' => 'extra-image-9.2.jpg',
        //...
    ),
);

How can such array $finalImages be prepared from existing array $images ?

(Note that first images can be main image or extra images or both from certain step. In our example, if you see we have only main image from step 3 but both main & extra images from step 5, rest should follow thereafter)

If step is what you want to order on, then how about reindexing a new array with step as the index and rearrange the new array with ksort()?

http://php.net/manual/en/function.ksort.php

Scott

I suppose you could loop through your $images array a few times to build it…


$finalImages = array();
foreach($images as $arr):
    $finalImages[]['image'] = $arr['image'];
endforeach;

foreach($images as $arr):
    foreach($arr['extra_images'] as $arr2):
        $finalImages[]['image'] = $arr2['image'];
    endforeach;
endforeach;    

echo "<pre>";
print_r($finalImages);
echo "</pre>";        

Oh wait… I reread your sample array and I see not ALL main images are first then extra images… Then a single loop should work.

             
$finalImages = array();
foreach($images as $arr):       
    $finalImages[]['image'] = $arr['image'];
    foreach($arr['extra_images'] as $arr2):
        $finalImages[]['image'] = $arr2['image'];
    endforeach;
endforeach;    

echo "<pre>";
print_r($finalImages);
echo "</pre>";        

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.