I'm trying to write a sort/filter script using an array filter function in PHP using the lambda syntax.
This achieves what I want to do, but it looks like it can be optimised:
$offersReceivedProcessedByItem = array();
foreach ($this->currentSessionUser->items as $item) {
$receivedOffersOnItem = array();
foreach ($offersReceived as $offerReceived) {
if ($offerReceived->item->id === $item->id) {
$receivedOffersOnItem[$offerReceived->id] = $offerReceived;
}
}
offersReceivedProcessedByItem[$item->id] = $receivedOffersOnItem;
}
(for each item a user has, filter through the offersReceived array and add the offer to a new array if it has an equal id, if that makes sense?!).
Here's what I'm trying:
$offersReceivedProcessedByItem = array();
foreach ($this->currentSessionUser->items as $item) {
$receivedOffersOnItem = array_filter($item->offers, function($offer){
return $offer->item->id === $item->id;
});
$offersReceivedProcessedByItem[$item->id] = $receivedOffersOnItem;
}
But i'm getting that $item is undefined, I expect it's closed scope so I tried passing $item as another argument but then i'm getting 'missing argument 2'.
I tried this using the relational approach built into Fuel but PHP eats too much memory and fails.
Any help? What am I getting wrong?
A closure doesn't know anything about the environment it is created it unless you tell it
$receivedOffersOnItem = array_filter($item->offers, function($offer) use($item){
return $offer->item->id === $item->id;
});
Notice the use-statement
A link to the manual http://php.net/functions.anonymous#example-119
Related
I need a bit of help ordering a SimpleXML array in an alphabetical order using one of it's attribute ($url). I have the following code that generate a page:
<?php
$xml = simplexml_load_file($xml_url);
$ids = array();
foreach($xml->list as $id) {
$ids[] = $id;
}
//It works but randomize instead of sorting alphabetically
usort($ids, function ($a, $b) {
return strnatcmp($a['url'], $b['url']);
});
foreach ($ids as $id) {
PopulatePage($id);
}
function PopulatePage($id) {
$url = $id->url;
$img = $id->img;
//OTHER CODES TO GENERATE THE PAGE
}?>
QUESTION RESOLVED!
There is no conversion needed, you already have an array which you can sort, you just need to understand how usort callbacks work. Each of the items in your $ids array is a SimpleXMLElement object, and each time your callback is run, it will be given two of those objects to compare. Those objects will be exactly the same as in the existing PopulatePage function, so accessing the URL needs to happen exactly as it does there ($url = $id->url;) not using a different notation ($id['url']).
To make it more explicit, let's write a named function with lots of clear variable names:
function CompareTwoIds(SimpleXMLElement $left_id, SimpleXMLElement $right_id): int {
$left_url = $left_id->url;
$right_url = $right_id->url;
return strncatcmp($left_url, $right_url);
}
Now you can test calling that function manually, and use it as the callback when you're happy:
usort($ids, 'CompareTwoIds');
Once you're comfortable with the principles, you may decide to skip the extra verbosity and just write this, which is completely equivalent:
usort($ids, fn($a,$b) => strncatcmp($a->url, $b->url));
To my understanding Input::post(); with no parameters returns an array containing all the data from a specific POST..
I am doing this $all_input = Input::post();
But then I am iterating through the array Java-like (is that how you even do it?)
for ($i=0; $i<count($all_input); $i++)
{
if (strpos($all_input[$i], 'something') == true) // ERROR...
but the application crashes with error Undefined offset: 0 which I believe means that the index was not found?
I have also tried adding this to no avail:
if (!isset($all_input))
{
return;
}
If so how can you access the data to iterate through them? I know it contains data cause I can see them when I press the button during the debugging on the browser if I remove that code.
If you didn't already figure it out I am coming from Java developer and I have just started learning php so bear with me please.
According to this: https://fuelphp.com/docs/classes/input.html#/method_post Input::post(); will return $_POST which is an assoc array.
Here is the source code, because the fuelphp's documentation doesn't cover it exactly.
/**
* Fetch an item from the POST array
*
* #param string $index The index key
* #param mixed $default The default value
* #return string|array
*/
public static function post($index = null, $default = null)
{
return (func_num_args() === 0) ? $_POST : \Arr::get($_POST, $index, $default);
}
You need to refer with your input names, so if you have an input which you call 'name', in that case you need to refer $all_input['name']. You can get your keys by using array_keys() function. Also it's better if you use foreach in this situation. Like:
foreach($all_input as $key => $value) {
echo 'Input key: ' . $key . ' value: ' . $value;
}
If you left the $key => you will get only the value, you can left it, if you doesn't use it inside the foreach.
If you don't want to use foreach somewhy:
$all_input = Input::post();
$keys = array_keys($all_input);
for ($i = 0; $i < count($keys); $i++) {
if (strpos($all_input[$keys[$i]], 'something') == true) {
// Do your stuff here.
}
}
But I still recommend foreach if possible, it's less overhead and cleaner code.
This won't work because you are dealing with an Object (Input) not an array.
I recommend using a foreach loop vice a for loop. To verify the contents/structure of the Input Object you can also do a dd() to see the Input Object in its entirety.
Basically,
$input = Input::post();
foreach($input as $i) {
echo $i; //this is a property of the Input Object. if you have arrays or other objects store inside of it, you may need to go deeper into the oject either with a foreach loop again or by calling the property directly ($i->property)
};
This is how I used to create keys that didn't exist inside an array when looping through data:
$array = [];
foreach ($results as $result) {
if (!isset($array[$result->id])) {
$array[$result->id] = [];
}
$array[$result->id][] = $result->value;
}
A colleague at work does the following. PHP doesn't error but I am not sure if it's a feature of PHP or if it's incorrect:
$array = [];
foreach ($results as $result) {
$array[$result->id][] = $result->value;
}
Is it incorrect for me to do the above?
if condition you put in your code is unnecessary. Let me explain.
if (!isset($array[$result->id])) {
$array[$result->id] = [];
}
This mean if $array[$result->id] is not exist than you are define it as an array, however $array[$result->id][] it self create new array if not existing without throwing any error. So no need to use if condition error. In conclusion, both code are correct, just you are using unnecessary if condition.
I'm wondering when, if ever it would be appropriate to use an ArrayObject() instead of an Array()? Here's an example i've been working on.
To me i'm thinking a simple array will work, but i found ArrayObject() in the manual and I'm wondering, if it would be better to use one instead of a simple array.
public function calculateTotal(){
if(count($this->items) > 0){
$n = 0;
foreach($this->items as $item){
if($item->size == 'small'){
$k = $item->price->small;
}
if($item->size == 'large'){
$k = $item->price->large;
}
$n += $k * $item->quantity;
}
}else{
$n = 0;
}
return (int) $n;
}
Now I'm confused as to how i should construct the object.
for instance can i construct it with the short array syntax?
$this->items = []; //empty object
or should i construct is as an Array object
$this->items = new ArrayObject(); //empty object
I'm also confused as to how exactly i should push new items to the array.
i have the following function i'm writing:
Also how should i append arrray objects to this object?
is this good?
public function additem($item){
$add = [
'item_id'=>$this->item_id(),
'name'=>$item['name'],
'size',$item['size'],
'quantity'=>$item['quantity'],
'price'=>[
'large'=>$item['price'],
'small'=>$item['price']
]
]
array_push($this->items,$add);
}
or should i instead use ArrayObject::append() or some other method?
I checked the manual and it says this:
public void ArrayObject::append ( mixed $value )
Appends a new value as the last element.
Note:
This method cannot be called when the ArrayObject was constructed from an object. Use ArrayObject::offsetSet() instead.
source http://php.net/manual/en/arrayobject.append.php
The reason i'm asking this now is, later on when needing to delete items from this list how will i find what i'm looking for? can i use in_array() on this object.
I apologize in advance for these questions that might seem dumb to you, but keep in mind I'm still learning some of the more technical things. Thank you
There's nothing in your first snippet that would require ArrayObject. KISS and use simple arrays: array_push($this->items,$add); or $this->items []= $add; are both fine.
As a side note, there's a discrepancy between calculateTotal and add in your code: you have to decide whether you want your item structures to be arrays ($item['price']) or objects ($item->price). My advice is to use arrays, but that's really up to you.
Here's a snippet of my code. I understand that it is generally advised against to modify an array whilst it is being looped, however in this scenario I think it is the right thing to do and saves resources.
$levels = array($post);
foreach ($levels as $level) {
$next_sibling = get_next_sibling_page($level);
if ($next_sibling) {
$next_page = $next_sibling;
break 1;
} else if ($level->post_parent) {
array_push($levels, get_page($level->post_parent));
}
}
The idea is that the foreach would reiterate with the pushed value if ($level->post_parent), I can see that the if statement is resolving to true and the array is being pushed to however the foreach does not reiterate and only runs the one time.
Does anyone know how I can recursively continue my foreach until $level->post_parent resolves to false?
To modify array which is being used in foreach pass array by referance.
foreach (&$levels as $level) {
.....
}