3

I'm currently building code in Laravel and adding the (array) part to my code fixed my laravel sync problem when there was no data being passed in the array. This is the question I learned from and got the (array) code to use.

I'm having a hard time finding the documentation for this in laravel or php and was wondering which language/framework the (array) code originates from and what it exactly does. If you could direct me to the right documentation page I would love that as well.

Community
  • 1
  • 1
Simon Suh
  • 10,599
  • 25
  • 86
  • 110
  • I would assume Laravel. I've never seen '(array)' in any php code I've looked at. – Elroy Jetson Dec 02 '16 at 04:12
  • 2
    [Type Juggling](http://php.net/manual/en/language.types.type-juggling.php) is what you are looking for. – Gerrit0 Dec 02 '16 at 04:12
  • @ElroyJetson Laravel has no syntax. It's not a language. – Joseph Silber Dec 02 '16 at 04:18
  • @JosephSilber I agree my assumption was ill advised . I thought maybe it was something within the framework. I've never seen or heard of typecasting arrays. – Elroy Jetson Dec 02 '16 at 04:23
  • 1
    @ElroyJetson You can type cast to any type, actually. You can do `$x = 1; $y = (object)$x;` and get a `stdClass` object back. It's not terribly useful, but you can do it. – Machavity Dec 02 '16 at 04:25
  • @Machavity is this popular amoung other languages, or is it just a feature of PHP? Not typecasting objects to other objects. But primitives to objects, or primitives to arrays? – Elroy Jetson Dec 02 '16 at 04:29
  • 1
    @ElroyJetson It's just a side effect of PHP being [weakly typed](http://stackoverflow.com/questions/3376252/what-are-the-benefits-and-drawbacks-of-a-weakly-typed-language). Most languages do not allow you to convert types on the fly, but PHP does for simplicity – Machavity Dec 02 '16 at 04:34
  • Cool, thanks for the lesson! – Elroy Jetson Dec 02 '16 at 13:05

3 Answers3

3

It's just another way of creating an array by using an existing variable

$x = 1; // int
$y = (array)$x; // array[0] => 1
$z = [$x]; // array[0] => 1

I should note that the last way would be preferred (directly declaring it as an array), since it's clearer what will happen (type juggling can produce unexpected results when converting values like this).

http://php.net/manual/en/language.types.array.php#language.types.array.casting

Machavity
  • 30,841
  • 27
  • 92
  • 100
0

I think you are struggling with the process of type casting in php, like:

PHP type casting to array

We can convert any data type variable in array using (array) keyword. Any scalar data type conversion into array will create array and add element at 0th index.

For example:

<?php
var_dump((array) , 5);// value 5 in the array with 0th index
var_dump((array) NULL);// Will be empty array
?>

Explanation with example

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • var_dump((array) , 5);// value 5 in the array with 0th index what's this mean, there should no ',' – LF00 Dec 02 '16 at 04:17
0

It's just an array castring, according to the php manual

Converting to array

For any of the types integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue).

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:

<?php
> 
> class A {
>     private $A; // This will become '\0A\0A' }
> 
> class B extends A {
>     private $A; // This will become '\0B\0A'
>     public $AA; // This will become 'AA' }
> 
> var_dump((array) new B()); ?>

The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'.

Converting NULL to an array results in an empty array.

LF00
  • 27,015
  • 29
  • 156
  • 295