0

I have a list that is sorted in alphabetical order. To do this I made sure there were only names in the array by using:

$oned = array_map('current', $productencr);

Then I sorted them all like this:

sort($oned, SORT_STRING | SORT_FLAG_CASE);

My problem:

I also need the description belonging to each name to be shown in a loop. How can I do that and still have my array of names onedimensional and sorted?

<?
//  Product names
    $producten                  = "SELECT producten.naam, producten.id, producten.omschr_kort FROM `producten` WHERE special = ''";
    $productencon               = $conn->query($producten);
    $productencr                = array();
    while ($productencr[] = $productencon->fetch_assoc());
    // Maak van de multidimensionale array een ééndimensionale
    $oned = array_map('current', $productencr);
    $lastChar = '';
    // Sorteer de array op alfabetische volgorde
    sort($oned, SORT_STRING | SORT_FLAG_CASE);

    $i = 1;
    // Loop de array
    foreach($oned as $key => $val){
      //De eerste waarde is leeg dus voor de code pas uit als key nummer is niet 0
      if($key != '0'){
        echo '<li class="tooltipproduct" data-tooltip-content="1">';
        $char = $val[0];
        if($char !== $lastChar){
          if($lastChar !== ''){
            echo '<br>';
          }
          echo '<span style="color:#eb9600;font-size:25px;font-weight:800;">'.strtoupper($char).'</span><br>';
          $lastChar = $char;
        }
        echo '<span class="tooltip1" data-tooltip-content="#tooltip_content'.$i.'" style="cursor:pointer;">'.$val.'</span><br></li>';
        echo '<div class="tooltip_templates" style="background-color:#eb9600;">
                  <span id="tooltip_content'.$i.'" style="min-height:180px;!important">
                      <h2 style="font-size: 20px;color:#fff;font-weight:bold;">'.$val.'</h2>
                      <span>Meer informatie nodig <br> of snel een scherpe offerte?</span>
                      <br>
                      <a style="position:absolute;bottom:20px;font-weight:bold;color:#fff;background-color:#c17c02;padding:10px;border-radius:3px;" href="contact.php">Vraag offerte aan</a>
                      <img style="max-width:90px;position:absolute;bottom:0px;right:12px;" src="images/contact_jess.png" /> <!-- assets/images/team/member-\'.rand(1,10).\'.png -->
                  </span>
              </div>';
        $i++;
      }
    }
?>

$val will always contain a name since the array $oned only contains names (so i can sort them easily). How can I also use another value in my loop? $oned contains ['naam'] but I also need ['omschr_kort'] looped.

Maybe I can first sort the array, then merge the result with another array?

Example of my current array output with everything selected (not only names):

    Array
(
    [0] => Array
        (
            [naam] => Naam1
            [id] => 1
            [omschr_kort] => Lorem ipsum
        )

    [1] => Array
        (
            [naam] => Naam2
            [id] => 2
            [omschr_kort] => Lorem ipsum
        )

    [2] => Array
        (
            [naam] => Naam3
            [id] => 3
            [omschr_kort] => Lorem ipsum
        )
)
twan
  • 2,450
  • 10
  • 32
  • 92

1 Answers1

0

Considering that $productencr is somthing like this:

$productencr = array(
    ['naam'=>'naam2','omschr_kort'=>'oms1'],
    ['naam'=>'naam1','omschr_kort'=>'oms2'],
    ['naam'=>'naam4','omschr_kort'=>'oms3'],
    ['naam'=>'naam3','omschr_kort'=>'oms4'],
);

You can use this script to sort that

$return_fare = [];
foreach ($productencr as $key => $row) {
    $return_fare[$key]  = $row['naam'];
}
$is_sorted = array_multisort($return_fare, SORT_ASC, $productencr);
print_r($productencr); // now $productencr is sorted with [naam] column

full details is in PHP sort array by two field values

Community
  • 1
  • 1
Armin.G
  • 381
  • 2
  • 12