2016-05-16 19 views
2

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

あなたはすべての画像に「デフォルトというタグがある見ることができるように、このタグは、TRUEまたはFALSEできる唯一の写真はTRUE属性を持つことができ I。アレイを通してスクロール)

1とデフォルトのフィールドにTRUE属性を持つ画像があるかどうか、

2)見られるように、アレイを離れる存在しない場合、

:「に興味メートル

3)TRUEの画像がある場合、この写真は配列の最初の位置に配置する必要があります。

どうすればいいですか?

はあなたに スティーブ

ありがとう
+0

[usort](http://php.net/usort)を使用すると、おそらく役に立ちます。一部のエントリで –

+0

がtrueになることがありますか? – splash58

+0

配列を再フォーマットしても構いません。 – Jacob

答えて

0

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

     ) 

) 

が、これはあなたが望む方法を見つけるのに役立ちます願っています...!

+0

それは完璧に動作します!!!!ありがとう、私はその機能を勉強します! –

0

私はそれを行うことができますかわからない..私はだけなの検証として、テストを行ってきた瞬間に

任意のコード」の原因を書いていません配列に含まれる画像の数、iのループのためにやって考えているので:

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

たぶん、このような何か:

<?php 
// Check if there is a "falses" and "true"; or only "falses" 
// (assuming there's never more than one default = true) 
if(count(array_unique(array_column($your_array['images'],'default'))) > 1) 
{ 
    // Copies the default true 
    $first_img = array_filter($your_array['images'],function($image){return !$image['default'];}); 

    // Build final $images array 
    $final_images_array = array_merge(
     // Add the first image (default = true) 
     $first_img, 
     // Add all the other images. 
     array_filter($your_array['images'],function($image){return $image['default'];}) 
    ); 
} 

// Leaves the array unchanged otherwise 
+0

私はあなたのコードを使用しますが、配列は並べ替えられません...最初の見方では、IF条件が値 "default"私は$ data ['images'] ['default'] ...これは問題ですか? –

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; 
}); 

それはあなたの方法により、[ '画像']の配列をソートします。私はこれを参照してarray_splice を使用して、以下のようなものを試してみました

+0

これは初回のみ動作します!私は説明しようとします: 私はそれが完全に動作するように使用した初めて!私がデフォルトのイメージを変更すると、あなたのコードは配列の順序を変更しません。配列は最初の並べ替えと同じままです。 –

+0

デフォルトイメージを変更するとはどういう意味ですか? – James

+0

バックエンドでは、配列内に存在するXイメージから選択したデフォルトイメージを変更できます...デフォルトイメージを変更することができ、過去のデフォルトイメージはデフォルトのFALSE属性を取得します。しかし、あなたの関数を使用して私の配列は、自分自身を並べ替えていない私はdef画像を変更する時間。 –

0

私が正しく思うのであれば、このデフォルトのものを使ってデフォルト画像を設定しています。メインアレイに[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' => '', 
     ), 
    ) 
); 

このようにして、配列をフィルタリングまたは並べ替える必要はありません。

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

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'); 

これはあなたの配列をソートします。