kdes70
@kdes70
Web developer

Сортировка массива, в под массив php?

Возникла проблема при написание скрипта, буду весьма благодарен если кто поможет.
Суть проблемы вот в чём:
Есть вот такой массив, мне нужно выделить в под массив, все холодные закуски и салаты.
Это своего рода массив категорий и нужно выделить под категории.
Dump => array(48) {
  [0] => array(8) {
    ["id"] => string(1) "1"
    ["name"] => string(10) "АКЦИЯ"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [1] => array(8) {
    ["id"] => string(1) "4"
    ["name"] => string(10) "ОБЕДЫ"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [2] => array(8) {
    ["id"] => string(2) "10"
    ["name"] => string(68) "БЛЮДА ИЗ РЫБЫ СОБСТВЕННОГО ХОЗЯЙСТВА"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [3] => array(8) {
    ["id"] => string(2) "11"
    ["name"] => string(25) "РОЛЛЫ (СЫМАКИ)"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [4] => array(8) {
    ["id"] => string(2) "12"
    ["name"] => string(45) "ХОЛОДНЫЕ ЗАКУСКИ  мясные"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [5] => array(8) {
    ["id"] => string(2) "13"
    ["name"] => string(46) "ХОЛОДНЫЕ ЗАКУСКИ овощные"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [6] => array(8) {
    ["id"] => string(2) "14"
    ["name"] => string(45) "ХОЛОДНЫЕ ЗАКУСКИ  рыбные"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [7] => array(8) {
    ["id"] => string(2) "15"
    ["name"] => string(43) "САЛАТЫ мясные с курицей"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [8] => array(8) {
    ["id"] => string(2) "16"
    ["name"] => string(47) "САЛАТЫ мясные со свининой"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [9] => array(8) {
    ["id"] => string(2) "17"
    ["name"] => string(47) "САЛАТЫ мясные с телятиной"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }
  [10] => array(8) {
    ["id"] => string(2) "18"
    ["name"] => string(44) "САЛАТЫ мясные Азиатские"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
  }


результат должен получиться
Dump => array(48) {
    [0] => array(8) {
    ["id"] => string(1) "1"
    ["name"] => string(10) "АКЦИЯ"
    ["discount"] => string(1) "0"
    ["img"] => string(0) ""
    ["made"] => string(1) "5"
    ["dop1"] => string(0) ""
    ["dop2"] => string(0) ""
    ["x"] => string(0) ""
    }
    [1] => array(8) {
        ["id"] => string(1) "4"
        ["name"] => string(10) "ОБЕДЫ"
        ["discount"] => string(1) "0"
        ["img"] => string(0) ""
        ["made"] => string(1) "5"
        ["dop1"] => string(0) ""
        ["dop2"] => string(0) ""
        ["x"] => string(0) ""
  }
  [2] => array(8) {
        ["id"] => string(2) "10"
        ["name"] => string(68) "БЛЮДА ИЗ РЫБЫ СОБСТВЕННОГО ХОЗЯЙСТВА"
        ["discount"] => string(1) "0"
        ["img"] => string(0) ""
        ["made"] => string(1) "5"
        ["dop1"] => string(0) ""
        ["dop2"] => string(0) ""
        ["x"] => string(0) ""
  }
  [3] => array(8) {
        ["id"] => string(2) "11"
        ["name"] => string(25) "РОЛЛЫ (СЫМАКИ)"
        ["discount"] => string(1) "0"
        ["img"] => string(0) ""
        ["made"] => string(1) "5"
        ["dop1"] => string(0) ""
        ["dop2"] => string(0) ""
        ["x"] => string(0) ""
  }
  [4] => array(8) {
        ["id"] => string(2) "12"
        ["name"] => string(45) "ХОЛОДНЫЕ ЗАКУСКИ"
        ["made"] => string(1) "5"
        ["sub"] => array(3) {
            [0] => array(8) {
                ["id"] => string(2) "12"
                ["name"] => string(45) "ХОЛОДНЫЕ ЗАКУСКИ  мясные"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
            [1] => array(8) {
                ["id"] => string(2) "13"
                ["name"] => string(46) "ХОЛОДНЫЕ ЗАКУСКИ овощные"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
            [2] => array(8) {
                ["id"] => string(2) "14"
                ["name"] => string(45) "ХОЛОДНЫЕ ЗАКУСКИ  рыбные"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
        }
  }
   [5] => array(8) {
        ["id"] => string(2) "12"
        ["name"] => string(45) "САЛАТЫ"
        ["made"] => string(1) "5"
        ["sub"] => array(3){
            [0] => array(8) {
                ["id"] => string(2) "15"
                ["name"] => string(43) "САЛАТЫ мясные с курицей"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
            [1] => array(8) {
                ["id"] => string(2) "16"
                ["name"] => string(47) "САЛАТЫ мясные со свининой"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
            [9] => array(8) {
                ["id"] => string(2) "17"
                ["name"] => string(47) "САЛАТЫ мясные с телятиной"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
            [10] => array(8) {
                ["id"] => string(2) "18"
                ["name"] => string(44) "САЛАТЫ мясные Азиатские"
                ["discount"] => string(1) "0"
                ["img"] => string(0) ""
                ["made"] => string(1) "5"
                ["dop1"] => string(0) ""
                ["dop2"] => string(0) ""
                ["x"] => string(0) ""
            }
  
        }
}
  • Вопрос задан
  • 107 просмотров
Решения вопроса 1
coderisimo
@coderisimo
Общая идея.

$allFood = []; // сюда поместите массив со всей жратвой
        $coldFoodArray = ['name'=>'ХОЛОДНЫЕ ЗАКУСКИ' , 'sub'=>[]]; // 'sub'=>[] здесь будут холодные закуски
        $saladsArray = ['name'=>'САЛАТЫ' , 'sub'=>[] ];//  'sub'=>[] здесь будут модификации оливье

        foreach ($allFood as $k => $v) {
            if (strpos($v['name'], 'ХОЛОДНЫЕ ЗАКУСКИ')) {
                $coldFoodArray['sub'][] = $v;
                unset($allFood[$k]);
            }
            if (strpos($v['name'], 'САЛАТЫ')) {
                $saladsArray['sub'][] = $v;
                unset($allFood[$k]);
            }
        }

        $allFood[] = $coldFoodArray;
        $allFood[] = $saladsArray;
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы