dsyct08008
2019-08-19 08:10
浏览 186
已采纳

在for循环中创建数组php [duplicate]

This question already has an answer here:

This is my array:

$result = [["space 1","space 2","space 3"],["150 x 150","250 x 
250","350 x 350"],["50","100", "150"]];

I'm trying to get the following result below.

Array
(
 [0] => Array
    (
        [0] => "space 1"
        [1] => "150 x 150"
        [2] => "50"

    )

[1] => Array
    (
        [0] => "space 2"
        [1] => "250 x 250"
        [2] => "100"
    )

[2] => Array
    (
        [0] => "space 3"
        [1] => "350 x 350"
        [2] => "150"
    )

)

This is what i got

    $new_result = [];

$y = 0;

for ($x = 0; $x < count($result); $x++) {
    foreach ($result as $value) {
        $new_result [$y][$x] = $value[$x];

        //echo $value[$x];
        //echo "<br>";

    }

    $y++;
    echo "<hr>";
}

print_r($new_result);

I have tried this appending the value with .= but i get an error saying that Undefined offset: 0.

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:</ p>

  • 在PHP中转置多维数组 13 answers </ span> </ li> </ ul> </ div>

    这是我的数组:</ p>

      $ result = [[“ 空间1“,”空间2“,”空间3“],[”150 x 150“,”250 x 
    250“,”350 x 350“],[”50“,”100“,”150“]]  ; 
     </ code> </ pre> 
     
     

    我正在尝试获得以下结果。</ p>

      Array 
    (
     [  0] =&gt;数组
    (
     [0] =&gt;“空格1”
     [1] =&gt;“150 x 150”
     [2] =&gt;“50”
     
    )  
     
     [1] =&gt;数组
    (
     [0] =&gt;“空格2”
     [1] =&gt;“250 x 250”
     [2] =&gt;“100”  
    )
     
     [2] =&gt; Ar  ray 
    (
     [0] =&gt;  “空间3”
     [1] =&gt;  “350 x 350”
     [2] =&gt;  “150”
    )
     
    )
     </ code> </ pre> 
     
     

    这就是我得到的</ p>

      $ new_result =  []; 
     
     $ y = 0; 
     
    for($ x = 0; $ x&lt; count($ result); $ x ++){
     foreach($ result as $ value){
     $  new_result [$ y] [$ x] = $ value [$ x]; 
     
     // echo $ value [$ x]; 
     // echo“&lt; br&gt;”; 
     
    } 
      
     $ y ++; 
     echo“&lt; hr&gt;”; 
    } 
     
    print_r($ new_result); 
     </ code> </ pre> 
     
     

    我试过这个附加了 。= </ code>的值,但是我得到一个错误,说明未定义的偏移量:0。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题