php foreach外循环

时间:2019-12-26 00:14:26

标签: php arrays loops foreach

  

我想在循环外使用我的新数组。   我该怎么办?

$new = array();
foreach ($array as $data) {

    $url   = $data["result"]["url"];
    $name  = $data["result"]["name"];

    $new = array(
        $name => $url
    );
}
print_r($new);

1 个答案:

答案 0 :(得分:1)

代替

 $new = array(
    $name => $url
 );

将其编辑为

$new[$name] = $url;

所以它会像这样结束

$new = array();
foreach ($array as $data) {

    $url   = $data["result"]["url"];
    $name  = $data["result"]["name"];

    $new[$name] = $url;
}
print_r($new);