Destructuring an array using spread operator on left side of assignment to collect all remaining elements in a single variable
Asked Answered
D

6

5

I want to perform Destructuring in php just like in javascript code below:

[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a,b,rest);

Output:

10 20 [ 30, 40, 50 ]

How can I preform that operation in php?

My php code is:

<?php
$array = [10, 20, 30, 40, 50]; 

// Using the list syntax:
//list($a, $b, $c[]) = $array;

// Or the shorthand syntax:
[$a, $b, $c[]] = $array;

echo "$a<br>$b<br>";
print_r ($c);
?>

Which prints:

10
20
Array ( [0] => 30 )

But I want "[ 30, 40, 50 ]" in $c

Dermatologist answered 3/3, 2023 at 5:58 Comment(0)
D
5

I did this using spread operator and function:

function f1($a,$b,...$c) {
    return ['a' => $a, 'b' => $b, 'c' => $c];
}
$array = [10, 20, 30, 40, 50]; 
extract (f1(...$array));
echo "$a<br>$b<br>";
print_r ($c);

Please let me know if this is correct way to do it.

Dermatologist answered 25/4, 2023 at 5:5 Comment(1)
I've updated my answer to show a version of your answer that reduces the number of hardcoded data points.Subsidy
L
2

Unfortunately that isn't possible with the spread parameter in PHP. However if you would like to achieve your result you could also first destructerize your wanted values. And then use the array_slice() function for your $c parameter like the following:

<?php
$array = [10, 20, 30, 40, 50]; 

// Using the list syntax:
//list($a, $b, $c[]) = $array;

// Or the shorthand syntax:
[$a, $b] = $array;

// Returns the array except the first two elements.
$c = array_slice($array, 2); 

print_r($c);
?>

Results into:

Array
(
    [0] => 30
    [1] => 40
    [2] => 50
)
Lolanthe answered 3/3, 2023 at 7:23 Comment(0)
B
2

The array keeps on loading, so use

[$a, $b, $c[], $c[], $c[]] = $array;

instead.

<?php
$array = [10, 20, 30, 40, 50]; 
[$a, $b, $c[], $c[], $c[]] = $array;
echo "$a\n$b\n";
print_r($c);
?>

gives

10
20
Array
(
    [0] => 30
    [1] => 40
    [2] => 50
)
Berton answered 3/3, 2023 at 8:43 Comment(0)
S
2

PHP will not allow you to unpack a dynamic amount of data using the left side of the assignment, so for a fully dynamic one-liner, use the right side of the assignment to prepare/group the data after the first two elements.

Code: (Demo)

[$a, $b, $c] = array_merge(array_splice($array, 0, 2), [$array]);

The above snippet will work as intended for input arrays which have at least 2 element.


Another approach is to use an "Immediately-invoked function expression" (a term which I read here) and return an associative array from the variables declared in the function signature. (Demo)

extract(
    (fn($a, $b, ...$c) => get_defined_vars())(...$array)
);

This concise technique offers the convenience of only maintaining the function signature to affect the returned array.

A word of caution though, unpacking the whole array before feeding it to the anonymous function may perform poorly on relatively large arrays.

Subsidy answered 7/3, 2023 at 6:39 Comment(0)
T
0

PHP does not implement spread operator in the left side of assignments. As an alternative, you can extract elements from the head of the array:

$rest = [10, 20, 30, 40, 50];
$a = array_shift($rest);
$b = array_shift($rest);
var_dump($a, $b, $rest);

... or:

$rest = [10, 20, 30, 40, 50];
[$a, $b] = [array_shift($rest), array_shift($rest)];
var_dump($a, $b, $rest);
int(10)
int(20)
array(3) {
  [0]=>
  int(30)
  [1]=>
  int(40)
  [2]=>
  int(50)
}
Trinatte answered 3/3, 2023 at 7:28 Comment(0)
K
0

you can't destructure array directly like

[$a, $b, $c[]] = $array;

There is no direct method to Destructuring an array You can use array_slice()

$arr=['a','b','c','d'];

[$a,$b]=$arr;

print_r($a);
$c=array_slice($arr,2);
print_r($c);

So how you can destructure an array.

Kasey answered 28/3, 2023 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.