2016-05-16 12 views
2

Ich habe ein Problem mit einer PHP-Funktion. Was ich gerne bekommen würde ist: Ordnen Sie ein Array je nachdem, ob eine Bedingung oder nicht.Ein Array neu anordnen und Element verschieben

meine Arrays Format ist:

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

Wie Sie in jedem Bild sehen können, gibt einen Tag „default genannt wird, kann dieser Tag WAHR oder FALSCH sein Nur ein Foto, um das TRUE-Attribut kann mir.. ist interessiert:

1) Blätter durch das Array und sehen, ob es ein Bild mit TRUE Attribute im Standardfeld ist;

2), wenn das Array nicht vorhanden verlassen, wie gefunden;

3) Wenn ein Bild mit TRUE-Attribut vorhanden ist, muss dieses Foto an der ersten Position im Array platziert werden;

Wie kann ich das tun?

Danke Steve

+0

Mit [usort] (http://php.net/usort) wird wahrscheinlich helfen. –

+0

kann für einige Einträge wahr sein? – splash58

+0

Können Sie Ihr Array neu formatieren, ist es schwierig, die Struktur zu erhalten. – Jacob

Antwort

0

Ich habe etwas versucht, wie unten array_splice mit Bezug auf diese Verwendung: 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 Sie so, wie Sie herausfinden möchten, helfen ..!

+0

Es funktioniert perfekt !!!! Danke, ich werde diese Funktion studieren! –

0

schreibe ich hatte keine Ursache‘Codes ich weiß nicht, wie ich das tun kann ..

Im Moment habe ich nur Tests wie die Überprüfung der getan haben die Anzahl der Bilder in der Anordnung enthalten ist, weil ich über das tun eine for-Schleife denke:

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

Vielleicht so etwas wie folgt aus:

<?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

Ich benutze Ihren Code, aber das Array ist nicht neu geordnet ... auf den ersten Blick denke ich, es ist, weil die IF-Bedingung nicht den Wert "Standard" finden ... zu sehen Standardeigenschaft ich verwende $ data ['images'] ['default'] ... ist das das Problem? –

0

Try this:

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

Es Array sortiert [ 'Bilder'] von Ihrem Weg.

+0

es funktioniert nur das erste Mal! Ich versuche zu erklären: das erste Mal, dass ich es verwendet habe funktioniert perfekt! Wenn ich das Standardbild ändere, ändert Ihr Code das Array nicht ... Array bleibt das gleiche wie die erste Neuordnung. –

+0

was meinst du das Standardbild ändern? – James

+0

im Backend kann ich die Standardbilder ändern, die von einem X-Bild wählen, das im Array vorhanden ist ... wenn ich will, kann ich das Standardbild und das vorherige Standardbild ändern, das FALSE-Attribut in 'Standard' erhalten. Aber mit Ihrer Funktion mein Array nicht neu anordnen ebery Zeit ich das Def-Image ändern .. –

0

Wenn ich richtig rate, verwenden Sie diese "Standard" Sachen, um nur das Standardbild einzustellen. Warum nicht den Schlüssel [default] zum Haupt-Array hinzufügen, in dem Sie den Index des Standardbildes aus dem Array [images] speichern können. Wenn kein Standardbild festgelegt ist, behalten Sie einfach null dort.

$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' => '', 
     ), 
    ) 
); 

Auf diese Weise müssen Sie Array überhaupt nicht filtern oder sortieren.

0

Sie können usort() verwenden, um PHP-Array wie folgt zu sortieren ...

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

erhalten Sie folgende Ausgabe:

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

dies Ihr Array sortieren wird.

Verwandte Themen