【问题标题】:Array - Select the sub children id's from an arrayArray - 从数组中选择子子 ID
【发布时间】:2018-10-01 22:29:03
【问题描述】:

我从一个函数构建了这个数组,该函数将允许或有效的 url slug 及其 id 排序到另一个数组中。我可以通过将 url slug 传递给类别来调用当前类别 ID。现在我需要获取子类别 ID(如果有),以便我可以从数据库中调用适当的项目以进行显示。

这是数组的一个例子:

Array (
    [radios] => 1
    [radios/motorola] => 2
    [radios/motorola/handheld] => 3
    [radios/motorola/mobile] => 4
    [radios/icom] => 5
    [radios/icom/handheld] => 6
    [radios/icom/mobile] => 7
    [radios/mics-and-speakers] => 8
    [radios/mounts] => 9
    [radios/radio-other] => 10
    [misc] => 11
    [misc/led] => 12
    [phones] => 13
    [phones/samsung] => 14
    [phones/lg] => 15
    [phones/motorola] => 16
    [phones/huawei] => 17
    [phones/blackberry] => 18
    [phones/flip] => 19
    [boosters] => 20
    [boosters/standalone] => 21
    [boosters/indoor-antenna] => 22
    [boosters/outdoor-antenna] => 23
    [boosters/connections] => 24
    [accessories] => 25
    [accessories/cases] => 26
    [accessories/other] => 27
    [internet] => 28
    [internet/fusion] => 29
    [internet/point-to-point] => 30
    [internet/hotspots] => 31
    [internet/gateways] => 32
    [internet/switches] => 33
    [cameras] => 34
    [cameras/complete-kits] => 35
    [cameras/additional-cameras] => 36
    [cameras/other] => 37
);

如您所见,每个结果都指向该组的类别 ID。如果我访问以下网址:

http://example.com/store/cameras

我可以打印出正确的THE PATH CURRENTLY IS: 34。由于它下面有孩子,我还需要他们的 ID 和他们任何孩子的 ID,等等。这样在 radios 上,我可以显示所有子类别项目,而在 radios/motorola 上我只显示基于摩托罗拉的产品及其子产品,等等。

如果有一种简单的方法,使用我现在拥有的这个数组,将子项(如果有的话)一直向下排序并只取回他们的 id(最好是在一个新数组中)以显示数据库项?

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    你可能想创建一个这样的函数来过滤你的数组,

    function filterArray($array, $term) {   
        $pattern = "/\b" . str_replace($term, '/', '\/') . "\b/i";
        foreach($array as $key => $value) {
            if(preg_match($pattern, $key)) {
                /* Following condition makes sure that your search
                will match starting from the beginning. */          
                if (stripos(trim($key), $term) === 0){
                    $filtred[] = $value;
                }
            }
        }
     }
    

    然后使用$array 和您的搜索$term 调用上述函数。

    filterArray($array, 'radios') 会给你这个,

    数组 ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )

    filterArray($array, 'radios/motorola') 会给你这个,

    数组([0] => 2 [1] => 3 [2] => 4)

    等等..我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      你可以试试双层数组,这样你就可以像这样存储值,

      Array (
      [name] => John
      [surname] => Smith
      [contact] => Array ( 
                          [address] => 18 Maple Street
                          [number] => 555 477 77 77
                          )
      )
      

      这样,如果您需要从“联系人”打印某些内容,您可以使用循环并打印联系人字段的所有子项。

      【讨论】:

      • ..什么?这如何帮助从数组分支获取子 ID?我已经有一个包含我需要的 id 的数组,我只需要将孩子的 id 链接起来,这样我就可以从他们的 id 中获取产品
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多