وحید
2 سال پیش توسط وحید مطرح شد
1 پاسخ

مرتب کردن آرایه دو بعدی

سلام به همگی
من این آرایه دو بعدی رو دارم

array(8) {
  [0]=>
  array(9) {
    ["filter"]=>
    string(1) "*"
    ["label"]=>
    string(3) "All"
    ["description"]=>
    bool(false)
    ["count"]=>
    bool(false)
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(true)
    ["url"]=>
    string(16) "/elahe/drawings/"
    ["class"]=>
    string(38) "vp-filter__item vp-filter__item-active"
  }
  [1]=>
  array(10) {
    ["filter"]=>
    string(4) "2003"
    ["label"]=>
    string(4) "2003"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(7)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=2003"
    ["class"]=>
    string(15) "vp-filter__item"
  }
  [2]=>
  array(10) {
    ["filter"]=>
    string(4) "2005"
    ["label"]=>
    string(4) "2005"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(6)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=2005"
    ["class"]=>
    string(15) "vp-filter__item"
  }
  [3]=>
  array(10) {
    ["filter"]=>
    string(4) "2018"
    ["label"]=>
    string(4) "2018"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(5)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=2018"
    ["class"]=>
    string(15) "vp-filter__item"
  }
  [4]=>
  array(10) {
    ["filter"]=>
    string(4) "1995"
    ["label"]=>
    string(4) "1995"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(2)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=1995"
    ["class"]=>
    string(15) "vp-filter__item"
  }
  [5]=>
  array(10) {
    ["filter"]=>
    string(4) "2008"
    ["label"]=>
    string(4) "2008"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(4)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=2008"
    ["class"]=>
    string(15) "vp-filter__item"
  }
  [6]=>
  array(10) {
    ["filter"]=>
    string(4) "2007"
    ["label"]=>
    string(4) "2007"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(6)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=2007"
    ["class"]=>
    string(15) "vp-filter__item"
  }
  [7]=>
  array(10) {
    ["filter"]=>
    string(4) "2017"
    ["label"]=>
    string(4) "2017"
    ["description"]=>
    string(0) ""
    ["count"]=>
    int(2)
    ["taxonomy"]=>
    string(8) "category"
    ["id"]=>
    int(0)
    ["parent"]=>
    int(0)
    ["active"]=>
    bool(false)
    ["url"]=>
    string(31) "/elahe/drawings/?vp_filter=2017"
    ["class"]=>
    string(15) "vp-filter__item"
  }
}

که خروجیش یه همچین چیزی میشه
 تصویر

حالا من نیاز دارم که بر اساس کلید لایه دوم filter اینهارو سورت کنم که سالهایی که توی تصویر هست به ترتیب مثلا از سال ۱۹۹۵ تا ۲۰۱۸ نمایش داده بشه

خوشحال میشم کسی بتونه کمکم بکنه


ثبت پرسش جدید
متین طیبی نیا
تخصص : nodejs
@MatinTayebi 2 سال پیش آپدیت شد
0

سلام میتونید از تابع usort استفاده کنید من قبلا به سوال برخورد کردم ویک فاکنش آماده طراحی کردم

function sorting($data, $sortByColumn, $orderBy = "asc"): array
{
    $orderBy = strtolower($orderBy);

    usort($data, function ($a, $b) use ($sortByColumn, $orderBy) {
        $valueOne = strtolower($a[$sortByColumn]);
        $valueTwo = strtolower($b[$sortByColumn]);
        if ($orderBy === "asc") {
            return $valueOne > $valueTwo;
        } else if ($orderBy === "desc") {
            return $valueOne < $valueTwo;
        }
    });

    return $data;
}

ارگمان اول : ارایه مد نظر
ارگمان دوم : اسم کلید
ارگمان سوم : مرتب سازی از بزرگ به کوچیک یا برعکس


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام