开发者

php array stack ordering

开发者 https://www.devze.com 2023-02-12 10:35 出处:网络
I am trying to sort this array in php: Array ( [Levi Self] => Array ( [0] => Portraits [1] => Abstract

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, basi开发者_运维技巧cally, 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



$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

0

精彩评论

暂无评论...
验证码 换一张
取 消