skip to Main Content

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

5

Answers


  1. 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
    )
    
    Login or Signup to reply.
  2. 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)
    }
    
    Login or Signup to reply.
  3. 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 "$an$bn";
    print_r($c);
    ?>
    

    gives

    10
    20
    Array
    (
        [0] => 30
        [1] => 40
        [2] => 50
    )
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search