+ Operator For Array in PHP

+ operator for array in PHP?

Quoting from the PHP Manual on Language Operators

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

So if you do

$array1 = ['one',   'two',          'foo' => 'bar'];
$array2 = ['three', 'four', 'five', 'foo' => 'baz'];

print_r($array1 + $array2);

You will get

Array
(
[0] => one // preserved from $array1 (left-hand array)
[1] => two // preserved from $array1 (left-hand array)
[foo] => bar // preserved from $array1 (left-hand array)
[2] => five // added from $array2 (right-hand array)
)

So the logic of + is equivalent to the following snippet:

$union = $array1;

foreach ($array2 as $key => $value) {
if (false === array_key_exists($key, $union)) {
$union[$key] = $value;
}
}

If you are interested in the details of the C-level implementation head to

  • php-src/Zend/zend_operators.c

Note, that + is different from how array_merge() would combine the arrays:

print_r(array_merge($array1, $array2));

would give you

Array
(
[0] => one // preserved from $array1
[1] => two // preserved from $array1
[foo] => baz // overwritten from $array2
[2] => three // appended from $array2
[3] => four // appended from $array2
[4] => five // appended from $array2
)

See linked pages for more examples.

PHP Accessing array values using Object Operator with key

this code work 100% fine

$arr = (object) array(
'id' => 123,
'title' => 'Example Title',
);

echo $arr->title;

Apply logical operator between array elements in PHP

Maybe this brings you closer to your goal:

 for($i=0;$i<count($boolean)-1;$i++)
{
if ($i < count($and_or) && !empty($and_or[$i]) {
if($and_or[$i]=='and')
{
$operator='&&';
$result = $boolean[$i] && $boolean[$i+1];
}
else
{
$operator='||';
$result = $boolean[$i] || $boolean[$i+1];
}
$operation=($boolean[$i].$operator.$boolean[$i+1]);
}
var_dump($operation);
var_dump($result);

Please note that you use most boolean values two times (the second value is the second operator for the first operation and the first operator for the second operation).

Using the + operator to combine arrays in PHP

This happens because both arrays in the last example have the same keys:

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

Docs

using PHP's null coalescing operator on an array

You don't add anything to params. Your given code simply generates an unused return value:

$params['phone'] ?? 'default'; // returns phone number or "default", but is unused

Thus, you will still have to set it:

$params['phone'] = $params['phone'] ?? 'default';

How to make PHP understand *either* an array or the splat operator (3 dots ...) for function parameters?

It has different results in the function.

1) If you pass multiple strings to the function, it will get an array of strings like so:

test('a', 'series of', 'strings go here');

//vardump result
array (size=3)
0 => string 'a' (length=1)
1 => string 'series of' (length=9)
2 => string 'strings go here' (length=15)

2) If you pass an array to function it will get an array of arrays. The splat operator will add a first entry that you pass to function to the first index of an array, the second parameter will add into the second index and so on. so no matter what you pass to the function (array or string) it will go to index zero. but the problem is if you pass array it will generate an array of arrays in the first index of the array:

test([ 'a', 'series of', 'strings go here' ]);

//vardump result
array (size=1)
0 =>
array (size=3)
0 => string 'a' (length=1)
1 => string 'series of' (length=9)
2 => string 'strings go here' (length=15)

Conclusion:

you can solve this with one of these two ways:

1) put 3 dots before you pass the array to the function:

test(...[ 'a', 'series of', 'strings go here' ]);

2) Another way of doing this is by adding a function named is_multi_array() to check if the variable that passed to the function is multi-dimensional. After that you can simply get the first element of the array and put it in strings variable:

function is_multi_array( $arr ) {
rsort( $arr );
return (isset( $arr[0] ) && is_array( $arr[0] ));
}

function test(...$strings)
{
if(is_multi_array($strings)){
$strings = $strings[0];
}

//do the rest
}

This way you can use the function in both ways that you like:

test('a', 'series of', 'strings go here');
test([ 'a', 'series of', 'strings go here' ]);

How to use ternary operator with Array in php?

PHP 7+

Forget the ternary operator and just use the null coalesce operator:

<?= $cars[$index] ?? end($cars) ?>

This says, use $cars[$index] if it's available, else default to end($cars), which is the last thing in the array.

PHP 5

You can use the ternary operator:

<?= $cars[$index] ? $cars[$index] : end($cars) ?>

Or its shortcut form:

<?= $cars[$index] ?: end($cars) ?>

But note this will generate an E_NOTICE if the $index element does not exist. To avoid the notice, you'd need something like:

<?= isset($cars[$index]) ? $cars[$index] : end($cars) ?>


Related Topics



Leave a reply



Submit