doucai4274 2015-06-04 18:02
浏览 38
已采纳

使用特定要求重新排序阵列

I have an array that I need no reorder like this:

  • If this have "casa-cocina-mesas-" and "casa-cocina-mesas-somethingelse-", the value with more values must keep it, the other must be deleted.

  • But if we have this "casa-cocina-mesas-somethingelse-" and this "casa-anotherstuff-mesas-somethingelse-", the both must be saved.

  • And if an value have another differtent value like "electronic-tools" and "electronic-computer-accessory". bot array must be saved.

  • And i have some values as null too but this value doesn't matter.

  • Finally all the other must be removed.

    <?php
    $category = array("casa-cocina-",null,"casa-","electronic-computer-accessory","electronic-",null,"casa-cocina-mesas-",
    "casa-cocina-","electronic-","electronic-tools");
    ?>
    
  • 写回答

1条回答 默认 最新

  • duanfei8149 2015-06-04 18:39
    关注

    For each value, you want to know if the string is contained in another value already present in the array. If it is, we can disregard the value. The following should do the trick:

    <?php
    $categories = array("casa-cocina-",null,"casa-","electronic-computer-accessory","electronic-",null,"casa-cocina-mesas-","casa-cocina-","electronic-","electronic-tools");
    
    // Remove empty values from your array
    $categories = array_filter($categories);
    
    $temp = [];
    
    $element = array_shift($categories);
    
    while ($element != NULL) {
        $found = false;
        // Check the remaining elements in the array
        foreach ($categories as $category) {
            if (strpos($category, $element) !== FALSE) {
                // We found this as a substring
                $found = true;
                break;
            }
        }
    
        if (!$found) {
            // Check the elements in the temp array to prevent duplication.
            foreach ($temp as $category) {
                if (strpos($category, $element) !== FALSE) {
                    // We found this as a substring
                    $found = true;
                    break;
                }
            }
        }
    
        // If we didn't find the value as a substring, add it to $temp
        if (!$found) {
            $temp[] = $element;
        }
    
        // Pull the next element from the array
        $element = array_shift($categories);
    }
    
    // Store the processed array
    $categories = $temp;
    
    echo var_dump($categories);
    
    // array(3) {
    //   [0]=> string(29) "electronic-computer-accessory"
    //   [1]=> string(18) "casa-cocina-mesas-"
    //   [2]=> string(16) "electronic-tools"
    // }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?