E
E
Eugene2018-06-24 01:18:12
PHP
Eugene, 2018-06-24 01:18:12

How to sort multidimensional php array by string values?

There is an array:

$product_option_value_data[] = array(
                            'name'                    => $option_value['name'],
                            'product_option_value_id' => $option_value['product_option_value_id'],
              'option_value_id'         => $option_value['option_value_id'],
              'image'                   => $this->model_tool_image->resize($option_value['image'], 50, 50),
              'price'                   => $price,
              'price_prefix'            => $option_value['price_prefix']
            );

I sort it like this:
arsort($product_option_value_data);
The problem is that the option_value['name'] field does not store numeric data, but string data: "0.95 kg", "11.5 kg", "3.2 kg" and therefore the array looks like this:
Array
(
    [0] => Array
        (
            [name] => 0,95 кг
            [product_option_value_id] => 535415
            [option_value_id] => 365
            [image] => 
            [price] => 479.00P
            [price_prefix] => =
        )
    [2] => Array
        (
            [name] => 11,5 кг
            [product_option_value_id] => 535416
            [option_value_id] => 447
            [image] => 
            [price] => 4960.00P
            [price_prefix] => =
        )
    [1] => Array
        (
            [name] => 3,2 кг
            [product_option_value_id] => 535417
            [option_value_id] => 520
            [image] => 
            [price] => 1500.00P
            [price_prefix] => =
        )
)

That is, the array is sorted by the values ​​in the [name] key, namely by the first characters of the values ​​"0", "1", "3".
How to make sure that the sorting is still correct, i.e. the output should be like this:
Array
(
    [0] => Array
        (
            [name] => 0,95 кг
            [product_option_value_id] => 535415
            [option_value_id] => 365
            [image] => 
            [price] => 479.00P
            [price_prefix] => =
        )
    [1] => Array
        (
            [name] => 3,2 кг
            [product_option_value_id] => 535417
            [option_value_id] => 520
            [image] => 
            [price] => 1500.00P
            [price_prefix] => =
        )
    [2] => Array
        (
            [name] => 11,5 кг
            [product_option_value_id] => 535416
            [option_value_id] => 447
            [image] => 
            [price] => 4960.00P
            [price_prefix] => =
        )
)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question