如何更改下面多维数组的索引?

发布于 2024-11-29 17:16:26 字数 743 浏览 0 评论 0原文

如何将此波纹管数组更改

Array
(
    [0] => Array
    (
        [0] => Array
            (
                [0] => 35.2
            )

        [1] => Array
            (
                [0] => 49.5
            )            

    )

    [1] => Array
    (
        [0] => Array
            (
                [0] => 44
            )

        [1] => Array
            (
                [0] => 38.5
            )

    )
)

Array
(
    [0] => Array
    (
        [0] => 35.2             

        [1] =>49.5                            

    )

   [1] => Array
    (
        [0] =>  44                

        [1] => 38.5                

    )

)

How to change this bellow array

Array
(
    [0] => Array
    (
        [0] => Array
            (
                [0] => 35.2
            )

        [1] => Array
            (
                [0] => 49.5
            )            

    )

    [1] => Array
    (
        [0] => Array
            (
                [0] => 44
            )

        [1] => Array
            (
                [0] => 38.5
            )

    )
)

into

Array
(
    [0] => Array
    (
        [0] => 35.2             

        [1] =>49.5                            

    )

   [1] => Array
    (
        [0] =>  44                

        [1] => 38.5                

    )

)

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

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

发布评论

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

评论(2

情释 2024-12-06 17:16:26

最简单的方法是简单的嵌套 foreach:

// be sure to include the &. Otherwise your edits will do nothing.
foreach( $input as &$level1 )
{
    // level 1 is the array of the arrays which contain your values
    // we need the keys and the arrays which hold the desired values
    foreach( $level1 as $key => $level2 )
    {
       // assign the key to be the value at 0 instead of its current value
       // (which happens to be level2)
       $level1[ $key ] = $level2[ 0 ];
    }
}

Sheepy 建议使用 array_merge 和下面的 call_user_func_array 。这是个好主意,我给了+1。 (我鼓励其他人也这样做)。为了看看哪个更好,我决定对两种解决方案运行基准测试:

结果:

 manual              0.074267864227295
 call_usr_func_array 0.13694596290588
 manual              0.080928087234497
 call_usr_func_array 0.13510608673096

然后我颠倒了测试顺序:

 call_usr_func_array 0.14956903457642
 manual              0.066309928894043
 call_usr_func_array 0.14821600914001
 manual              0.064701080322266

这是基准测试代码:

$st = microtime(1);
$input = array();
for( $i = 0; $i < 10000; $i++ )
    $input[] = array( array( 1 ),array(  2 ),array(  3 ) );

// be sure to include the &. Otherwise your edits will do nothing.
foreach( $input as &$level1 )
{
    // level 1 is the array of the arrays which contain your values
    // we need the keys and the arrays which hold the desired values
    foreach( $level1 as $key => $level2 )
    {
       // assign the key to be the value at 0 instead of its current value
       // (which happens to be level2)
       $level1[ $key ] = $level2[ 0 ];
    }
}
print microtime(1) - $st;
print PHP_EOL;
$st = microtime(1);
$input = array();
for( $i = 0; $i < 10000; $i++ )
    $input[] = array( array( 1 ),array(  2 ),array(  3 ) );

foreach ( $input as $k => $ary ) {
  $input[$k] = call_user_func_array ( 'array_merge' , $ary );
}
print microtime(1) - $st;

The easy, easy way is a simple nested foreach:

// be sure to include the &. Otherwise your edits will do nothing.
foreach( $input as &$level1 )
{
    // level 1 is the array of the arrays which contain your values
    // we need the keys and the arrays which hold the desired values
    foreach( $level1 as $key => $level2 )
    {
       // assign the key to be the value at 0 instead of its current value
       // (which happens to be level2)
       $level1[ $key ] = $level2[ 0 ];
    }
}

Sheepy proposed use of array_merge with call_user_func_array below. That is a good idea and I gave it a +1. (I encourage others to do the same). To see which was better, I decided to run a benchmark on both solutions:

The results:

 manual              0.074267864227295
 call_usr_func_array 0.13694596290588
 manual              0.080928087234497
 call_usr_func_array 0.13510608673096

I then reversed test order:

 call_usr_func_array 0.14956903457642
 manual              0.066309928894043
 call_usr_func_array 0.14821600914001
 manual              0.064701080322266

Here's the benchmark code:

$st = microtime(1);
$input = array();
for( $i = 0; $i < 10000; $i++ )
    $input[] = array( array( 1 ),array(  2 ),array(  3 ) );

// be sure to include the &. Otherwise your edits will do nothing.
foreach( $input as &$level1 )
{
    // level 1 is the array of the arrays which contain your values
    // we need the keys and the arrays which hold the desired values
    foreach( $level1 as $key => $level2 )
    {
       // assign the key to be the value at 0 instead of its current value
       // (which happens to be level2)
       $level1[ $key ] = $level2[ 0 ];
    }
}
print microtime(1) - $st;
print PHP_EOL;
$st = microtime(1);
$input = array();
for( $i = 0; $i < 10000; $i++ )
    $input[] = array( array( 1 ),array(  2 ),array(  3 ) );

foreach ( $input as $k => $ary ) {
  $input[$k] = call_user_func_array ( 'array_merge' , $ary );
}
print microtime(1) - $st;
走走停停 2024-12-06 17:16:26

这是一个较短的版本。但实际上它仍然是一个嵌套循环。

foreach ( $input as $k => $ary ) {
  $input[$k] = call_user_func_array ( array_merge , $ary );
}

Here is a shorter version. It is still a nested loop in reality, though.

foreach ( $input as $k => $ary ) {
  $input[$k] = call_user_func_array ( array_merge , $ary );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文