重新排序数组并移动元素

时间:2016-05-16 09:23:43

标签: php arrays

我的php函数有问题。 我想要得到的是:根据条件是否重新排序数组。

我的数组格式为:

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] => 
        ) 
    )
    **....

正如您在每张图片中看到的那样,有一个名为&#34的标签;默认情况下,此标签可以是TRUE或FALSE。只有照片可以具有TRUE属性。 我对:

感兴趣

1)滚动数组并查看DEFAULT字段中是否有TRUE属性的图片;

2)如果不存在则将数组保留为found;

3)如果有一张TRUE属性的图片,这张照片必须放在数组的第一个位置;

我该怎么做?

谢谢 史蒂夫

7 个答案:

答案 0 :(得分:0)

我没有写任何代码,因为我不知道我该怎么做..

目前我只进行了测试,例如验证数组中包含的图像数量,因为我正在考虑进行for循环:

$countimg = count($data['images']);

答案 1 :(得分:0)

也许是这样的:

<myTag:dataList id="test">
    <myTag:options value="#{myBean.myCollection}" var="mySingleObj" itemValue="#{mySingleObj.value}" itemLabel="#{mySingleObj.label}" />
</myTag:dataList>

答案 2 :(得分:0)

试试这个:

usort($array['images'], function($x,$y){ // $array is your Array. 
    if ($x['default']===true) return -1;
    elseif ($x['default']===false) return 1;  
    else return 0; 
});

按您的方式对数组[&#39;图像&#39;]进行排序。

答案 3 :(得分:0)

我使用array_splice尝试了类似下面的内容 参考: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>";

输出:

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] => 
                )

        )

)

希望这能帮助您找到自己想要的方式..!

答案 4 :(得分:0)

如果我猜对了,你正在使用这个&#34;默认&#34;东西只是设置默认图像。为什么不将[default]键添加到主数组中,您可以在其中存储[images]数组中默认图像的索引。如果没有设置默认图像,请将null保留在那里。

$array = array(
    'default_image' => 0,
    'images' => array(
        '0' => array( 
            'path' => 'http://ssdds.jpg' ,
            'type' => 'logo',
            'alt' => '', 
        ), 
        '1' => array( 
            'path' => 'http://saasdsd.jpg', 
            'type' => 'photo', 
            'alt' => '', 
        ), 
        '2' => array( 
            'path' => 'http://saddadsasd.jpg', 
            'type' => 'photo', 
            'alt' => '', 
        ),
    )
);

这样您根本不需要对数组进行过滤或排序。

答案 5 :(得分:0)

您可以使用usort()对php数组进行排序......

<?php 
$dataArray = array( 
                    'id' => 3 ,
                    'idCategory' => 1 ,
                    'images' => array ( 
                            0 => array ( 
                                'path' => 'http://ssdds.jpg' ,
                                'type' => 'logo',
                                'default' => false,
                                ), 
                            1 => array ( 
                                'path' => 'http://saasdsd.jpg' ,
                                'type' => 'photo' ,
                                'default' => true,
                            ) ,
                            2 => array ( 
                                'path' => 'http://saddadsasd.jpg',
                                'type' => 'photo',
                                'default' => false,
                            ),
                        ) 
                 );

foreach($dataArray['images'] as $key => $data)
{
    if(in_array(1,$data))
    {
        usort($dataArray['images'], build_sorter('default'));
        $dataArray['images'] = $dataArray['images'];
    }
}

function build_sorter($key)
{
    return function ($a, $b) use ($key) {
        return strnatcmp($b[$key], $a[$key]);
    };
}

var_dump($dataArray);

这将输出:

array (size=3)
  'id' => int 3
  'idCategory' => int 1
  'images' => 
    array (size=3)
      0 => 
        array (size=3)
          'path' => string 'http://saasdsd.jpg' (length=18)
          'type' => string 'photo' (length=5)
          'default' => boolean true
      1 => 
        array (size=3)
          'path' => string 'http://saddadsasd.jpg' (length=21)
          'type' => string 'photo' (length=5)
          'default' => boolean false
      2 => 
        array (size=3)
          'path' => string 'http://ssdds.jpg' (length=16)
          'type' => string 'logo' (length=4)
          'default' => boolean false

<强> LIVE DEMO

答案 6 :(得分:0)

function sortByOrder($a, $b) {
    return $b['default'];
}

$picture_arr = array("images" => 
  array(
    array("path" => "http://image1.jpg", "type" => "logo", "default" => false),
    array("path" => "http://image2.jpg", "type" => "logo", "default" => false),
    array("path" => "http://image3.jpg", "type" => "logo", "default" => true),
    array("path" => "http://image4.jpg", "type" => "logo", "default" => false),
    array("path" => "http://image5.jpg", "type" => "logo", "default" => true),
  )
);


usort($picture_arr['images'], 'sortByOrder');

这将对您的数组进行排序。

相关问题