English 中文(简体)
虽然将阵列数据结构化,但能否将新内容分配给原始投入阵列?
原标题:While destructuring array data in the head of a foreach() loop, can new elements be assigned to the original input array?

Since PHP7.1, a foreach() expression can implement array destructuring as a way of unpacking row values and make individualized variable assignments for later use.

When using array destructuring within the head/signature of a foreach() loop, can new elements be declared instead of merely being accessed?

例如:

$array = [
    [ foo  =>  a ,  bar  => 1],
    [ foo  =>  b ,  bar  => 2],
];

Can 我用钥匙<代码>new对原阵列的每一行添加一个新的内容?

问题回答

是的,数据可直接附在<代码>foreach(>上原始输入阵列的行文。

使用阵列结构的总体好处是能够在特定阵列中具体获取/分离数据,而无需将不需要的数据输入变量。

详细情况:

  • The new element must be declared by reference (the value must be a variable prepended with &) -- otherwise there is no indication to modify the original array.
  • If the reference variable is not later assigned a value, the default value will be null.

守则: (Demo

$array = [
    [ foo  =>  a ,  bar  => 1],
    [ foo  =>  b ,  bar  => 2],
];

foreach ($array as [ new  => &$x,  bar  => $x]);

var_export($array);

产出:

array (
  0 => 
  array (
     foo  =>  a ,
     bar  => 1,
     new  => 1,
  ),
  1 => 
  array (
     foo  =>  b ,
     bar  => 2,
     new  => 2,
  ),
)

Using the above sample input array, a body-less loop can be used to assign the first level keys as a new column in the array without declaring the full row as a reference. Code: (Demo)

foreach ($array as $x => [ new  => &$x]);

这种办法要求宣布一个范围较小的变量与:

foreach ($array as $x => &$row) {
    $row[ new ] = $x;
}

如果没有逐条修改,则使用<代码>foreach()的签字要求值变量。

foreach ($array as $x => $row) {
    $array[$x][ new ] = $x;
}

产出(全部):

array (
  0 => 
  array (
     foo  =>  a ,
     bar  => 1,
     new  => 0,
  ),
  1 => 
  array (
     foo  =>  b ,
     bar  => 2,
     new  => 1,
  ),
)

http://stackoverflow.com/a/77741493/2943403” 回答是使用这一技术,并通过另一个文件将数值分配给参考变量。


@Dharman在一项PHP内提到,没有<条码>的阵列结构实施相同的行为:Demo

$arr = [];
[ foo  => &$v] = $arr;
$v = 1;
var_export($arr);
// output: array( foo  => 1)

我认为,这好像:

foreach (bars as$trendy=>[new => &$cocktail= Daiquiri , ed =>? (单位:千美元)

  1. First quirk

foreach (array as [new => &$x, bar =>$x]);

我预计:

致命错误: 重定参数x

海事组织最好写:

foreach (array as & [new => &$x, bar =>$y]$x=$x;/ 影响

或:

<代码>foreach (array as [new =>$x, bar => &$y])$y = 美元x;/ 影响美元内容

  1. Second quirk

foreach (user as [ state =>$state, work => &$ref[state]);

我预计:

差错:辛塔克斯错误,意想到的“......”

海事组织最好写:

foreach (user as [ state =>$state, work => &$job]$ref[state]=$job;

或:

foreach (user as &[State =>$state, work =>$jobs]$ref[state]=&$jobs; 用户[0][工作]的参考资料

  1. third quirk

foreach (array as$x => [new => &$x]/* 我的关键金额在哪里?

我预计:

致命错误: 重定参数x

  1. fourth quirk

The destructuring-rules of parser of PHP is similar to array construct. But on the one hand array() structures the data and constructs it at the same time.

In array( key =>hydrate()) structur/construct you can call a fonction but in destructuring syntax I can t imagine it.

  1. fifth quirk

为什么和计划;在阵列中创造新的入口?

这就是公共卫生和社会福利部的行为。

function foo($new, int $old) {
    $ref = "Quirikou";
}
$trendly = [ old =>0];
foo($trendly[ new ], $trendly[ old ]);// Warning: Undefined array key "new" in
var_dump($trendly);

function baz(&$new, int $old) {
    $ref = "Quirikou";
}
$trendly = [ old =>0];
baz($trendly[ new ], $trendly[ old ]);


var_dump($trendly);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...