0

I have an array like this

array
  0 => 
    array
      'title' => string 'Last Name' (length=9)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 1
  1 => 
    array
      'title' => string 'Title 1' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  2 => 
    array
      'title' => string 'Title 10' (length=7)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  3 => 
    array
      'title' => string 'Title 11' (length=7)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  4 => 
    array
       'title' => string 'Title 12' (length=7)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  5 => 
    array
       'title' => string 'Title 2' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  6 => 
    array
        'title' => string 'Title 3' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  7 => 
    array
       'title' => string 'Title 4' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  8 => 
    array
      'title' => string 'Title 5' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  9 => 
    array
      'title' => string 'Title 6' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  10 => 
    array
      'title' => string 'Title 7' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  11 => 
    array
       'title' => string 'Title 8' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0
  12 => 
    array
        'title' => string 'Title 9' (length=6)
      'price' => string '0.0000' (length=6)
      'price_type' => string 'fixed' (length=5)
      'is_required' => int 0

So the issue is that in the array I have title. When I do foreach loop the title shows up as

LAST NAME:
Title 1:
Title 10:
Title 11:
Title 12:
Title 2:
Title 3:
Title 4:
Title 5:
Title 6:
Title 7:
Title 8:
Title 9:

As you can notice that Title 10 shows up right after 1 and its not in correct numeric order. How can I fix that in php. thanks

TRIED $titles = array(); foreach ($product->custom_options as $key => $row) { $titles[$key] = $row['title']; } var_dump(array_multisort($titles, SORT_DESC, $product->custom_options));

hakre
  • 193,403
  • 52
  • 435
  • 836
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

4 Answers4

2

Try combining usort with strnatcmp. Something like this:

usort($array, function($lhs, $rhs){ 
    return strnatcmp($lhs['title'], $rhs['title']);
});
complex857
  • 20,425
  • 6
  • 51
  • 54
0

You never sort the array, it is being displayed in that order because you inserted the items in that way, you first need to order the array but not in an alphanumerical way. Try using another field with an Integer value.

jtello
  • 451
  • 4
  • 7
0

The issue is most likely with the generation of the array rather than fixing it as is now. From what I see you have a header as the first element and from then on you have the elements which are not 'sorted' properly.

Without knowing the exact requirements, and based on what you posted, a way to 'sort' this would be:

// $data array is the existing array
$new_array = array();
foreach ($data as $item) {
    if ($item['title'] == 'Last Name') {
        $new_array[0] = $item;
    } 
    else
    {
        $key = intval(trim(str_replace("Title ", "", $item['title'])));
        $new_array[$key] = $item;
    }
}

ksort($new_array);

The resulting array has the element with key 0 as the header and everything else depends on the "Title XX" where XX is a number (taken from the title element

HTH

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
0

You can directly use the php defined natsort function that sort an array using a "natural order" algorithm.

Simply write the following code and it is done.

natsort($array);
Arun Jain
  • 5,476
  • 2
  • 31
  • 52