PHP array_push error - php

My code is as below,
$products = array();
for($i=0; $i < sizeof($sales); $i++){
if(!in_array($sales[$i]['Product']['product'], (array)$products)){
$products = array_push((array)$products, $sales[$i]['Product']['product']);
}
}
I'm getting an error called Fatal error: Only variables can be passed by reference...
I'm using php5

You don't use array_push like that, that's your basic problem. You're trying to fix an error you're producing by casting $products to an array, which causes a new error. You use array_push like this:
array_push($products, ...);
You do not assign the return value back to $products, because the return value is the new number of elements in the array, not the new array. So either:
array_push($products, $sales[$i]['Product']['product']);
or:
$products[] = $sales[$i]['Product']['product'];
Not:
$products = array_push($products, $sales[$i]['Product']['product']);
and most certainly not:
$products = array_push((array)$products, $sales[$i]['Product']['product']);
Please RTM: http://php.net/array_push

The first parameter ($products in your case) has to be a reference, therefore a variable has to be passed. You now cast the variable to an array first and the result of that cast cannot be passed by reference since it is not assigned to a variable. You will have to assign it to a variable first or remove the cast.

Related

PHP: Can't use function return value in write context

Can't use function return value in write context.
All the search result responses say its something to do with the empty function, but I'm not using that?
foreach ($permission as explode(',', $permissionString)) { // line 44
if ($this->hasPermission($permission))
$count++;
}
In a foreach the expression on the left of the as should be the array that you want to iterate through, and the expression on the right is a variable that gets overwritten with the value of each element inside the array.
The reason that you get an error is because php is trying to write an element of $permission into explode(',', $permissionString), but this returns an error because explode(',', $permissionString) is a function call, not a variable, and only variables can be written to.
To fix this, try reversing the order of the as, like this:
foreach (explode(',', $permissionString) as $permission) {

Indirect modification of overloaded element has no effect

I have the following Eloquent query:
$item = Item::where('sku', $sku)->first();
After this query comes in I'm adding a variety of elements manually such as:
$item['total'] = $item['subtotal'] + $this->currentInventory();
Statements like the above that modify the object work just fine.
It stops working when I do the following:
$item['fields'] = [];
$fields = DB::table('item_fields')->where('item_id', $item['id'])->get();
foreach ($fields as $f) {
if (!isset($item['fields'][$f->field_group_name]))
$item['fields'][$f->field_group_name] = [];
$item['fields'][$f->field_group_name]['valid_values'] = DB::table('item_field_valid_values')->where('item_field_id', $f->item_field_id);
}
This will cause the line $item['fields'][$f->field_group_name] = []; to produce the error:
Indirect modification of overloaded element of Item has no effect
How can it be that I can assign $item['fields'] = [] but when I try to add an actual element to the $item['fields'] array that I get this error?
PHP version 5.6.0.
First off, you're missing get() in your code, so either:
1 You are iterating over the Query\Builder instead of the array of results, because you never executed the query. (I suppose you forgot it only here, because otherwise you would probably get trying to get property of non-object)
or 2 one of the rows has '' or null value in the field_group_name column.
That's why your code does this:
$item['fields'][NULL] = [];
and that's causing Indirect modification ... error.
So add check for empty value first:
if ($f->field_group_name && ! isset($item['fields'][$f->field_group_name]))
$item['fields'][$f->field_group_name] = [];
you may need to adjust it to your needs, but you get the idea.

foreach without doing a loop in PHP

How is it possible to perform a foreach function without doing a loop for example
foreach($result['orders'] as $order) {
But I don't want to do a foreach I want something like
$result['orders'] == $order;
Or something like that instead of doing it inside an loop because $result['orders'] is only returning 1 result anyway so I don't see the point in doing it in a loop.
Thank you
You can get the first (and apparently only) element in the array with any array function that gets an element from the array, e.g. array_pop() or array_shift():
$order = array_shift( $result['orders']);
Or list():
list( $order) = $result['orders'];
Or, if you know it's numerically indexed, access it directly:
$order = $results['orders'][0];
Are you maybe just looking for this?
$result['orders'] = $result['orders'][0];
You have a comparison operator (==) rather than an assignment operator (=) in your second code example. If you are just trying to set a variable equal to a position in an array, you can use:
$order = $results['orders'];
I am not sure if that is what you are trying to accomplish though.

return empty array in php

I have a function that returns an array, and is passed to foreach i.e.
foreach(function() as $val)
Since the array I am returning is declared in a series of if statements, I need to return an empty array if all the if statements are evaluated to false. Is this a correct way to do it?
if (isset($arr))
return $arr;
else
return array();
I would recommend declaring $arr = array(); at the very top of the function so you don't have to worry about it.
If you are doing the check immediately before you return, I do not recommend isset. The way you are using foreach is depending on an array being returned. If $arr is set to a number, for example, then it will still validate. You should also check is_array(). For example:
if (isset($arr) && is_array($arr))
return $arr;
else
return array();
Or in one line instead:
return (isset($arr) && is_array($arr)) ? $arr : array();
But, like I said, I recommending declaring the array at the very top instead. It's easier and you won't have to worry about it.
To avoid complexity, Simply
return [];

Items in Arrays, are they references?

look at the code below:
$index = GetIndexForId($itemid);
$item = null;
if( $index == -1 )
{
$item = array();
$this->items[] = $item;
$index = count($this->items)-1;
}
else
$item = $this->items[$index];
$item['id'] = $itemid;
$item['qty'] = $qty;
$item['options'] = $options;
$this->items[$index] = $item; // This line is my question
The last line, is it necessary? I really dont know how php handles array assignment.
P.S. GetIndexForId just searches for if the current ID already exists in the array, and the other "undeclared" variables are parameters.
From the documentation:
Array assignment always involves value copying. Use the reference operator to copy an array by reference.
So yes, given your code, the last line is necessary, but $this->items[] = $item; is superfluous.
If you want to update your Object, yes you need this last line
Any value type like boolean, int... will not be passed by reference. But if your array is filled with objects, it WILL be passed by reference. In your exemple, you need the last line. But as I said, if $item were an object you wouldn't need the last line. It is possible to pass a value type by reference with the reference operator.
Learn how to use the reference operator
HERE

Categories