php数组堆栈排序

发布于 2024-10-18 21:54:04 字数 723 浏览 1 评论 0原文

我正在尝试在 php 中对这个数组进行排序:

Array
(
    [Levi Self] => Array
        (
            [0] => Portraits
            [1] => Abstract
        )

    [Portraits] => Array
        (
            [0] => Megan
        )

    [Abstract] => Array
        (
            [0] => Locks
        )

)

看起来像这样:

Array
(
    [Levi Self] => Array
        (
            [Portraits] => Array
               (
                 [0] => Megan
               )

            [Abstract] => Array
               (
                 [0] => Locks
               )
        )
)

基本上,它删除了重复的项目“肖像”和“摘要”,因为它们已经是数组开始的键,并且是第一把钥匙,“利维·塞尔夫”。有什么办法可以做到这一点吗?谢谢,利维·塞尔夫

I am trying to sort this array in php:

Array
(
    [Levi Self] => Array
        (
            [0] => Portraits
            [1] => Abstract
        )

    [Portraits] => Array
        (
            [0] => Megan
        )

    [Abstract] => Array
        (
            [0] => Locks
        )

)

To look like this:

Array
(
    [Levi Self] => Array
        (
            [Portraits] => Array
               (
                 [0] => Megan
               )

            [Abstract] => Array
               (
                 [0] => Locks
               )
        )
)

Which, basically, removes the duplicate items "Portraits" and "Abstract", as they are already the keys for the array to begin with, along with being items under the first key, "Levi Self". Is there any way to do this? Thanks, Levi Self

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-10-25 21:54:04

$priKey = "Levi Self";
$arr = array(
    "Levi Self" => array("Portraits","Abstract"),
    "Portraits" => array("Megan"),
    "Abstract" => array("Locks")
);

function rearrangeData($primaryKey,$myArr){
    foreach($myArr[$primaryKey] as $key => $value){
        $myArr[$primaryKey][$value] = $myArr[$value];
        unset($myArr[$primaryKey][$key]);
        unset($myArr[$value]);
    }

    return $myArr;
}

$arr2 = rearrangeData($priKey, $arr);

print_r($arr); //what you started with
print_r($arr2); //what you want


$priKey = "Levi Self";
$arr = array(
    "Levi Self" => array("Portraits","Abstract"),
    "Portraits" => array("Megan"),
    "Abstract" => array("Locks")
);

function rearrangeData($primaryKey,$myArr){
    foreach($myArr[$primaryKey] as $key => $value){
        $myArr[$primaryKey][$value] = $myArr[$value];
        unset($myArr[$primaryKey][$key]);
        unset($myArr[$value]);
    }

    return $myArr;
}

$arr2 = rearrangeData($priKey, $arr);

print_r($arr); //what you started with
print_r($arr2); //what you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文