【问题标题】:Creating Array Key Name with Dynamic Array使用动态数组创建数组键名
【发布时间】:2014-09-29 17:39:24
【问题描述】:

我试图让下面的示例具有每个维度的键名,因此它看起来更像这样:

Array
(
    [Mobile] => Array
        (
            [0] => Array
                (
                    [product_id] => 007
                    [product_name] => Blackberry R-900 Mobile
                    [product_price] => £450
                    [product_status] => 1
                    [product_category] => Mobile
                )

代替下面的例子, 我已将用于创建数组的类包含在分类数组中以及它的用法。

array2arraytree 中的函数 appendNode 是算法实际为每个类别创建新数组的地方。

感谢您的帮助。

例子:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [product_id] => 007
                    [product_name] => Blackberry R-900 Mobile
                    [product_price] => £450
                    [product_status] => 1
                    [product_category] => Mobile
                )

            [1] => Array
                (
                    [product_id] => 001
                    [product_name] => Wespro Multi-SIM & Touch-screen Mobile
                    [product_price] => £150
                    [product_status] => 1
                    [product_category] => Mobile
                )

            [2] => Array
                (
                    [product_id] => 004
                    [product_name] => Sigmatel MP4/MP3 + Camera Mobile
                    [product_price] => £150
                    [product_status] => 1
                    [product_category] => Mobile
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [product_id] => 033
                    [product_name] => 8 GB Pendrive
                    [product_price] => £14.99
                    [product_status] => 0
                    [product_category] => Computers
                )

            [1] => Array
                (
                    [product_id] => 334
                    [product_name] => 250 GB Portable Hard Drive
                    [product_price] => £79.99
                    [product_status] => 1
                    [product_category] => Computers
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [product_id] => 033
                    [product_name] => The White Tiger – Aravind Adiga
                    [product_price] => £29.99
                    [product_status] => 1
                    [product_category] => Books
                )

            [1] => Array
                (
                    [product_id] => 4501
                    [product_name] => The Final Reckoning - Sam Bourne
                    [product_price] => £19.99
                    [product_status] => 0
                    [product_category] => Books
                )

            [2] => Array
                (
                    [product_id] => 034
                    [product_name] => The Final Reckoning - Sam Bourne
                    [product_price] => £15.79
                    [product_status] => 0
                    [product_category] => Books
                )

        )

)

用法:

<?php
require_once("array2arraytree.php");
$arrProducts=array(
    array(
        "product_id"            => "007",
        "product_name"          => "Blackberry R-900 Mobile",
        "product_price"         => "£450",
        "product_status"        =>"1",
        "product_category"      =>"Mobile"
    ),
    array(
        "product_id"            => "033",
        "product_name"          => "8 GB Pendrive",
        "product_price"         => "£14.99",
        "product_status"        => "0",
        "product_category"      => "Computers"
    ),
    array(
        "product_id"            => "033",
        "product_name"          => "The White Tiger – Aravind Adiga",
        "product_price"         => "£29.99",
        "product_status"        => "1",
        "product_category"      => "Books"
    ),
    array(
        "product_id"            => "4501",
        "product_name"          => "The Final Reckoning - Sam Bourne",
        "product_price"         => "£19.99",
        "product_status"        => "0",
        "product_category"      => "Books"
    ),
    array(
        "product_id"            => "001",
        "product_name"          => "Wespro Multi-SIM & Touch-screen Mobile",
        "product_price"         => "£150",
        "product_status"        => "1",
        "product_category"      => "Mobile"
    ),
    array(
        "product_id"            => "004",
        "product_name"          => "Sigmatel MP4/MP3 + Camera Mobile",
        "product_price"         => "£150",
        "product_status"        => "1",
        "product_category"      => "Mobile"
    ),
    array(
        "product_id"            => "034",
        "product_name"          => "The Final Reckoning - Sam Bourne",
        "product_price"         => "£15.79",
        "product_status"        => "0",
        "product_category"      => "Books"
    ),
    array(
        "product_id"            => "334",
        "product_name"          => "250 GB Portable Hard Drive",
        "product_price"         => "£79.99",
        "product_status"        => "1",
        "product_category"      => "Computers"
    )
);

$objTree=new Array2ArrayTree($arrProducts,"product_category");
$arrTree=$objTree->makeTree();
print("<pre>");
print_r($arrTree);
print("</pre>");
?>

类 Array2ArrayTree:

class Array2ArrayTree
{
    public $arrOriginal = array();
    public $arrDummy = array();
    public $strKey = "";

    public function __construct($arrData, $arrKey)
    {
        $this->arrOriginal = $arrData;
        $this->strKey = $arrKey;
        $this->arrDummy = array();
    }

    public function makeTree()
    {
        for ($i = 0; $i <= sizeof($this->arrOriginal) - 1; $i++) {
            $keyPosition = $this->searchKey($this->arrOriginal[$i][$this->strKey]);
            if ($keyPosition == -1) {
                $this->addNode($this->arrOriginal[$i]);
            } else {
                $this->appendNode($this->arrOriginal[$i], $keyPosition);
            }
        }
        return $this->arrDummy;
    }

    function searchKey($strCurrentValue)
    {
        for ($i = 0; $i <= sizeof($this->arrDummy) - 1; $i++) {
            if ($this->arrDummy[$i][0][$this->strKey] == $strCurrentValue) {
                return $i;
            }
        }
        return - 1;
    }

    function addNode($arrNode)
    {
        $this->arrDummy[sizeof($this->arrDummy)][0] = $arrNode;
    }

    function appendNode($arrNode, $keyPosition)
    {
        array_push($this->arrDummy[$keyPosition], $arrNode);
    }
}
?>

【问题讨论】:

    标签: php arrays algorithm sorting categorical-data


    【解决方案1】:

    应该更简单:

    public function makeTree()
    {
        foreach($this->arrOriginal as $value) {
            $this->arrDummy[$value[$this->strKey]][] = $value;
        }
        return $this->arrDummy;
    }
    

    【讨论】:

    • 你是神...谢谢.. :D
    【解决方案2】:

    我认为这可行 - 映射数组以获取您想要的键,然后使用 array_filter 仅获取该类别中的项目:

    //get the keys you want:
    $keys = array_unique(array_map(function($item) { return $item['product_category']; }, $array));
    $return = []; 
    foreach($keys as $key) {
        //filter the array to the one's in the current category, and reindex them to 0:
        $return[$key] = array_values(array_filter($array, function($item) use ($key) { 
                            return $item['product_category'] == $key; 
                        }));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2017-12-10
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      相关资源
      最近更新 更多