开发者

I don't know how to "sort" arrays

开发者 https://www.devze.com 2023-04-06 14:41 出处:网络
I have this array : array(2) { [1]=> array(4) { [\"name\"]=> string(14) \"Les Contenants\" [\"ordre\"]=>

I have this array :

array(2) {
  [1]=>
  array(4) {
    ["name"]=>
    string(14) "Les Contenants"
    ["ordre"]=>
    string(1) "3"
    [9]=>
    array(1) {
      ["name"]=>
      string(20) "Corbeilles unitaires"
    }
    [10]=>
    array(1) {
      ["name"]=>
      string(6) "Mannes"
    }
  }
  [6]=>
  array(3) {
    ["name"]=>
    string(7) "L'utile"
    ["ordre"]=>
    string(1) "1"
    [133]=>
    array(3) {
      ["name"]=>
      string(7) "Paniers"
      [192]=>
      array(1) {
        ["name"]=>
        string(13) "à provisions"
      }
      [193]=>
      array(2) {
        ["name"]=>
        string(13) "anses mobiles"
        [201]=>
        array(1) {
          ["name"]=>
          string(19)开发者_如何学C "non doublés tissus"
        }
      }
    }
  }
}

I need to sort this array on this key : array[$i]['ordre'] on an ascending order.

The results must be :

array(2) {
  [6]=>
  array(3) {
    ["name"]=>
    string(7) "L'utile"
    ["ordre"]=>
    string(1) "1"
    [133]=>
    array(3) {
      ["name"]=>
      string(7) "Paniers"
      [192]=>
      array(1) {
        ["name"]=>
        string(13) "à provisions"
      }
      [193]=>
      array(2) {
        ["name"]=>
        string(13) "anses mobiles"
        [201]=>
        array(1) {
          ["name"]=>
          string(19) "non doublés tissus"
        }
      }
    }
  }
  [1]=>
  array(4) {
    ["name"]=>
    string(14) "Les Contenants"
    ["ordre"]=>
    string(1) "3"
    [9]=>
    array(1) {
      ["name"]=>
      string(20) "Corbeilles unitaires"
    }
    [10]=>
    array(1) {
      ["name"]=>
      string(6) "Mannes"
    }
  }
}

Have you an idea to do it ?


usort($array,function($a,$b) {return $a['ordre']-$b['ordre'];});

Or, if your version of PHP doesn't support lambda-functions:

usort($array,create_function('$a,$b','return $a["ordre"]-$b["ordre"];'));
0

精彩评论

暂无评论...
验证码 换一张
取 消