首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用动态数组创建数组密钥名

用动态数组创建数组密钥名
EN

Stack Overflow用户
提问于 2014-09-29 17:39:24
回答 2查看 74关注 0票数 2

我试图让下面的示例为每个维度都有键名,这样看起来会更像这样:

代码语言:javascript
复制
Array
(
    [Mobile] => Array
        (
            [0] => Array
                (
                    [product_id] => 007
                    [product_name] => Blackberry R-900 Mobile
                    [product_price] => £450
                    [product_status] => 1
                    [product_category] => Mobile
                )

与下面的示例不同,我还包括了用于将数组创建为分类数组的类及其使用情况。

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

谢谢你的帮助。

示例:

代码语言:javascript
复制
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
                )

        )

)

用法:

代码语言:javascript
复制
<?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类:

代码语言:javascript
复制
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);
    }
}
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-29 18:12:30

应该更简单:

代码语言:javascript
复制
public function makeTree()
{
    foreach($this->arrOriginal as $value) {
        $this->arrDummy[$value[$this->strKey]][] = $value;
    }
    return $this->arrDummy;
}
票数 0
EN

Stack Overflow用户

发布于 2014-09-29 17:59:47

我认为这将有效地映射数组以获取所需的键,然后使用array_filter只获取该类别中的项:

代码语言:javascript
复制
//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; 
                    }));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26105682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档