多维数组将数组内部转换为字符串

时间:2016-06-10 21:13:06

标签: php arrays

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6111
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6122
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6131
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6151
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6162
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6211
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6223
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6231
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6241
                    [rank] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6114
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6123
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6131
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6211
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6223
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6231
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6241
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6251
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6262
                    [rank] => 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6114
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6122
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6131
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6312
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6322
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6331
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6343
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6351
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6362
                    [rank] => 1
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6111
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6122
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6131
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6141
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6151
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6162
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6312
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6322
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6331
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6343
                    [rank] => 1
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6114
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6123
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6134
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6311
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6322
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6332
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6342
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6351
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6362
                    [rank] => 1
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6111
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6122
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6134
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6211
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6223
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6231
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6241
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6251
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6261
                    [rank] => 1
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6111
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6121
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6131
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6211
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6223
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6231
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6241
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6251
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6262
                    [rank] => 1
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6211
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6221
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6231
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6241
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6312
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6323
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6331
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6343
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6351
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6361
                    [rank] => 1
                )

        )

    [8] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6111
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6122
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6131
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6142
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6151
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6161
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6312
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6322
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6331
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6343
                    [rank] => 1
                )

        )

    [9] => Array
        (
            [0] => Array
                (
                    [masteryId] => 6211
                    [rank] => 5
                )

            [1] => Array
                (
                    [masteryId] => 6223
                    [rank] => 1
                )

            [2] => Array
                (
                    [masteryId] => 6231
                    [rank] => 5
                )

            [3] => Array
                (
                    [masteryId] => 6241
                    [rank] => 1
                )

            [4] => Array
                (
                    [masteryId] => 6312
                    [rank] => 5
                )

            [5] => Array
                (
                    [masteryId] => 6322
                    [rank] => 1
                )

            [6] => Array
                (
                    [masteryId] => 6332
                    [rank] => 5
                )

            [7] => Array
                (
                    [masteryId] => 6343
                    [rank] => 1
                )

            [8] => Array
                (
                    [masteryId] => 6352
                    [rank] => 5
                )

            [9] => Array
                (
                    [masteryId] => 6363
                    [rank] => 1
                )

        )

)

如何将上面的数组转换为下面的数组。

上面的数组里面有9个数组。而且他们掌握了并且在内部排名。我想让它们成为一个字符串。 masteryid xx rank ++ masteryid xx rank ....

    Array
(  
    [0] => 6111 xx 5 ++ 6122 xx 1 ++ 6131 xx 5 ++ 6142 xx 1 ....
    [1] => 6114 xx 5 ++ 6123 xx 1 ++ 6131 xx 5 ++ 6142 xx 1 ....
    ....
    [9] => 6221 xx 5 ++ 6223 xx 1 ++ 6231 xx 5 ++ ....
)

我坚持使用这个代码,我试着这个约5个小时,我无法做到这一点,所以我来到这里。

2 个答案:

答案 0 :(得分:4)

试试这个:

$output = array();

foreach($array as $value)
{
   $s = "";
   foreach($value as $v)
       $s .= $v['masteryId'].' xx '.$v['rank'].' ++ ';

   $output[] = rtrim($s,' ++ ');
}

我希望这会对你有帮助。

答案 1 :(得分:3)

Magic one-liner:

array_walk($array,function(&$a){$a=array_reduce($a,function($b,$c){return($b?"$b ++ ":"")."$c[masteryId] xx $c[rank]";},"");});

这为外部数组中的每个元素调用一个匿名函数。由于&之前$a传递了对元素的引用,我们可以在函数内修改它。

然后,该函数通过将"$c[masteryId] xx $c[rank]"附加到前一个减少的结果,将每个元素依次减少为一个字符串。之前的输出,后跟" ++ "仅在它存在时才被添加(即它不是子数组中的第一个项)。

这里有一些更清晰,有一些空格和一些可能更清晰的变量名称:)

array_walk($array, function(&$subarray){
    $subarray = array_reduce($subarray, function($carry, $item){
        return ($carry ? "$carry ++ " : "") . "$item[masteryId] xx $item[rank]";
    }, "");
});