Steve Lactress Steve Lactress - 6 months ago 13
PHP Question

Reorder an array and move element

I have a problem with a php function.
What I'd like to get is: reorder an array according to whether or not a condition.

my array's format is:

Array (
[id] => 3
[idCategory] => 1
.... => **
[images] => Array (
[0] => Array (
[path] => http://ssdds.jpg
[type] => logo
[default] => false
[alt] =>
)
[1] => Array (
[path] => http://saasdsd.jpg
[type] => photo
[default] => true
[alt] =>
)
[2] => Array (
[path] => http://saddadsasd.jpg
[type] => photo
[default] => false
[alt] =>
)
)
**....


As you can see in every image there is a tag called "default, This tag can be TRUE or FALSE. Only a photo can have the TRUE attribute.
I'm interested in:

1) Scroll through the array and see if there is a picture with TRUE attribute in the DEFAULT field;

2) if not exist leave the array as found;

3) If there is a picture with TRUE attribute this photo must be placed at the first position in the array;

How can i do that?

Thank You
Steve

Answer

I have tried something like below using array_splice with reference to this: Move an array element to a new index in PHP

$arr['images'] = array ( 
        '0' => array ( 
            'path' => 'http://img1.jpg',
            'type' => 'logo',
            'default' => false,
            'alt' => ''
            ),
        '1' => array ( 
            'path' => 'http://img2.jpg',
            'type' => 'photo',
            'default' => true,
            'alt' => ''
        ),
        '2' => array ( 
            'path' => 'http://img3.jpg',
            'type' => 'photo',
            'default' => false,
            'alt' => ''
        ),
        '3' => array ( 
            'path' => 'http://img4.jpg',
            'type' => 'photo',
            'default' => true,
            'alt' => ''
        ));

        foreach($arr['images'] as $key => $img)
        {
            if($img['default'] == true)
            {
                $out = array_splice($arr['images'], $key, 1);
                array_splice($arr['images'], 0, 0, $out);
            }
        }

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

OUTPUT:

Array
(
    [images] => Array
        (
            [0] => Array
                (
                    [path] => http://img4.jpg
                    [type] => photo
                    [default] => 1
                    [alt] => 
                )

            [1] => Array
                (
                    [path] => http://img2.jpg
                    [type] => photo
                    [default] => 1
                    [alt] => 
                )

            [2] => Array
                (
                    [path] => http://img1.jpg
                    [type] => logo
                    [default] => 
                    [alt] => 
                )

            [3] => Array
                (
                    [path] => http://img3.jpg
                    [type] => photo
                    [default] => 
                    [alt] => 
                )

        )

)

Hope this will help you to find out the way you want..!

Comments