When dealing with arrays I am forced to add a bunch of repetitive code to handle arrays with one child versus multiple:
//If more than one step, process each step, elcse processs single
if(!array_key_exists('command',$pullcase['steps']['step'])) {
foreach($pullcase['steps']['step'] as $step) {
$command=$step['command'];
$parameter=$step['parameter'];
if(isset($step['value'])){
$value = $step['value'];
$this->runCommands($command,$parameter,$value);
} else {
$this->runCommands($command,$parameter);
}
}
} else {
$command = $pullcase['steps']['step']['command'];
$parameter = $pullcase['steps']['step']['parameter'];
if(isset($pullcase['steps']['step']['value'])){
$value = $pullcase['steps']['step']['value'];
$this->runCommands($command,$parameter,$value);
}
else { $this->runCommands($command,$parameter); }
}
As you can see, I'm having to duplicate my efforts depending on if there is a single item in an array versus multiple:
$pullcase['steps']['step'][0]['command']
vs
$pullcase['steps']['step']['command']
How can I simplify this code so that I can use a single variable for all instances?
If you control the creation of the array, make step an array of one even if there is only one so you always have an array. Is that possible?
You either have a step array [step][0][command] or you have a single step [step][command]. So when you create the array instead of [step][command] make it [step][0][command] etc. Standard way of doing it, problem solved as you only need the foreach.
If you can't do it at array creation then consider doing it before the loop:
if(is_array($pullcase['steps']['step'])) {
$steps = $pullcase['steps']['step'];
} else {
$steps[] = $pullcase['steps']['step'];
}
foreach($steps as $step) {
$value = isset($step['value']) ? $step['value'] : null;
$this->runCommands($step['command'], $step['parameter'], $value);
}
Also, if runCommands() can detect a empty argument, then an alternative to the if/else for the function call is used above.
The following may help. It doesn't do much but call the function "runcommands" on a value if the key is 'command'. I am using it to show how you can use array_walk_recursive to possibly solve your problem.
First, you need the function to use:
function runcommandswhencommand($value, $key)
{
if($key == 'command') runcommands($value);
}
Now, you can use the recursive walk on your array:
array_walk_recursive($pullcase, 'runcommandswhencommand');
With this, whenever the key is 'command', the value of that index will be used in the parameter of the function runcommands().
Related
i have some condition if request isset or no and retun in view blade laravel like this:
$compare1 = $request->compare1;
$compare2 = $request->compare2;
if (isset($compare1)) {
$laptop1 = Laptop::where('slug', $compare1)->firstOrFail();
return view('compare.index', ['laptop1' => $laptop1->id]);
} elseif(isset($compare2)) {
$laptop2 = Laptop::where('slug', $compare2)->firstOrFail();
return view('compare.index', ['laptop1' => $laptop1->id, 'laptop2' => $laptop2->id]);
}elseif(isset($compare1, $compare)) {
$laptop1 = Laptop::where('slug', $compare1)->firstOrFail();
$laptop2 = Laptop::where('slug', $compare2)->firstOrFail();
return view('compare.index', ['laptop1' => $laptop1->id, 'laptop2' => $laptop2->id]);
}else {
return view('compare.index');
}
if isset($compare1, $compare) run, $laptop2 not found, any solution for this case...?
Thanks before
The structure you have is incorrect. Currently, your code elseif(isset($compare1, $compare)) will never execute because if either $compare1 or $compare2 are set, your if statement will already exit before it gets to the 3rd one. You also have a lot of redundant code (repeating a line of code depending on which if block is executed) which is easily reduced to fewer lines and cleaner code.
Simple Approach
Consider this; (You should be able to replace your entire if block with this)
//set up an empty array to return
$return = [];
//check if `$compare1` is set, and add to return array if it is
if(isset($compare1)) {
$return['laptop1'] = (Laptop::where('slug', $compare1)->firstOrFail())->id;
}
//same as above but for `$compare2`
if(isset($compare2)) {
$return['laptop2'] = (Laptop::where('slug', $compare2)->firstOrFail())->id;
}
return view('compare.index', $return);
Dynamic Approach
This may be a little bit overkill if you are just doing 2 comparisons but it definitely has some upsides.
Similar amount of code as the simple approach
No need to define separate variables for every comparison (e.g $compare1 = $request->compare1;, etc)
Easily add more comparisons to your return by simply adding them to the $comparisons array
Future Proof
Code:
//empty array to return
$return = [];
//list of variables to compare
$comparisons = ['compare1', 'compare2'];
//loop through each comparison
foreach($comparisons as $key => $request_object) {
$count = $key + 1; //keys start at 0, so we add 1 to make it count sequentially 1,2,3 ...
$comparison = $request->{$request_object}; //grab your comparison object
//check if comparison object is set, add it to return array if it is
if(isset($comparison)) {
$return["laptop{$count}"] = (Laptop::where('slug', $comparison)->firstOrFail())->id;
}
}
return view('compare.index', $return);
Maybe it should be better to split code in parts to improve code quality.
Some thoughts after analyze your code:
Always returns view('compare.index')
Laravel Request contains a has method
Multiple conditional concatenation using if elseif.. (it decrease legibility)
It's more logical to pass the complete model to view instead of only id.
Otherwise, rename var to laptop1Id or similar
Take care of code duplication
Proposal
if($request->has('compare1')) {
$laptop1 = Laptop::where('slug', $request->get('compare1'))->firstOrFail();
}
if($request->has('compare2')) {
$laptop2 = Laptop::where('slug', $request->get('compare2'))->firstOrFail();
}
return view('compare.index', compact('laptop1', 'laptop2'));
If find/search a Laptop is a domain rule, you could encapsulate it in a method/scope in model, querying like:
$laptop = Laptop::findOrFailBySlug($val);
$laptop = Laptop::slug($val)->firstOrFail();
My problem is that I have lots of functions with VERY long lists of function parameters such as this one:
function select_items($con,$type,$id_item,$item_timestamp,$item_source_url,$item_type,$item_status,$item_blogged_status,$item_viewcount,$item_language,$item_difficulty,$item_sharecount,$item_pincount,$item_commentcount,$item_mainpage,$item_image_width,$item_image_height,$item_image_color,$item_modtime,$order,$start,$limit,$keyword,$language,$id_author,$id_sub_category,$id_category,$id_tag,$id_user){ ... }
As you can see its super long and (of course) very hard to maintain. Sometimes I need all of the variables to construct a super complex sql query, but sometimes I just use 1 or 2 of them. Is there a way to avoid this colossal list of parameters? For example with some strict / special naming convention ?
So basically I need something like this:
$strictly_the_same_param_name="It's working!";
echo hello($strictly_the_same_param_name);
function hello() //<- no, or flexible list of variables
{
return $strictly_the_same_param_name; // but still able to recognize the incoming value
}
// outputs: It's working!
I thought about using $_GLOBALs / global or $_SESSIONs to solve this problem but it doesn't seems really professional to me. Or is it?
For a first step, as you said, sometimes you need to call the function with only 2 args, you can set default values to your arguments in the declaration of your function. This will allow you to call your function with only 2 args out of 25.
For example:
function foo($mandatory_arg1, $optional_arg = null, $opt_arg2 = "blog_post") {
// do something
}
In a second step, you can use, and especially for that case, arrays, it will be way more simple:
function foo(Array $params) {
// then here test your keys / values
}
In a third step, you can also use Variable-length argument lists (search in the page "..."):
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
But ultimately, I think you should use objects to handle such things ;)
You can try use ... token:
$strictly_the_same_param_name= ["It's working!"];
echo hello($strictly_the_same_param_name);
function hello(...$args) //<- no, or flexible list of variables
{
if ( is_array( $args ) {
$key = array_search( 'What you need', $args );
if ( $key !== false ) {
return $args[$key];
}
}
return 'Default value or something else';
}
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
The function aims at finding an item in a range of arrays, and then returning its key.
Problem is that the function doesn't return anything, whereas it would echo the expected result...
Here is my code:
function listArray($tb, $target){
foreach($tb as $key => $value){
if(is_array($value)){ // current value is an array to explore
$_SESSION['group'] = $key; // saving the key in case this array contains the searched item
listArray($value, $target);
}else {
if ($target == $value) { // current value is the matching item
return $_SESSION['group']; //Trying to return its key
break; // I'd like to close foreach as I don't need it anymore
}
}
}
}
By the way, an other little thing: I'm not used to recursive function, and I didn't find any other solution than using a session variable. But there might be a nicer way of doing it, as I don't use this session variable elsewhere...
You need a return before the recurring listArray call.
Thank about it ..
return;
break;
That break is never reached (I don't believe you can use break to exit a function in php anyway)
The second return returns from a recursive call. Let's say that this was not two separate functions:
function foon() {
barn();
}
function barn() {
return true;
}
foon has no return statement.
I finally bypassed the problem by storing my result in a $_SESSION variable.
So, no return anymore...
$_SESSION['item'][$target] = $_SESSION['group'];
I have a foreach loop, that will loop through an array, but the array may not exist depending on the logic of this particular application.
My question relates to I guess best practices, for example, is it ok to do this:
if (isset($array))
{
foreach($array as $something)
{
//do something
}
}
It seems messy to me, but in this instance if I dont do it, it errors on the foreach. should I pass an empty array?? I haven't posted specific code because its a general question about handling variables that may or may not be set.
Just to note: here is the 'safest' way.
if (isset($array) && is_array($array)) {
foreach ($array as $item) {
// ...
}
}
Try:
if(!empty($array))
{
foreach($array as $row)
{
// do something
}
}
That's not messy at all. In fact, it's best practice. If I had to point out anything messy it would be the use of Allman brace style, but that's personal preference. (I'm a 1TBS kind of guy) ;)
I'll usually do this in all of my class methods:
public function do_stuff ($param = NULL) {
if (!empty($param)) {
// do stuff
}
}
A word on empty(). There are cases where isset is preferable, but empty works if the variable is not set, OR if it contains an "empty" value like an empty string or the number 0.
If you pass an empty array to foreach then it is fine but if you pass a array variable that is not initialized then it will produce error.
It will work when array is empty or even not initialized.
if( !empty($array) && is_array($array) ) {
foreach(...)
}
I would say it is good practice to have a 'boolean' other value that is set as 0 (PHP's false) to start, and any time some function adds to this array, add +1 to the boolean, so you'll have a definite way to know if you should mess with the array or not?
That's the approach I would take in an object oriented language, in PHP it could be messier, but still I find it best to have a deliberate variable keeping track, rather than try to analyze the array itself. Ideally if this variable is always an array, set the first value as 0, and use it as the flag:
<?PHP
//somewhere in initialization
$myArray[0] = 0;
...
//somewhere inside an if statement that fills up the array
$myArray[0]++;
$myArray[1] = someValue;
//somewhere later in the game:
if($myArray[0] > 0){ //check if this array should be processed or not
foreach($myArray as $row){ //start up the for loop
if(! $skippedFirstRow){ //$skippedFirstRow will be false the first try
$skippedFirstRow++; //now $skippedFirstRow will be true
continue; //this will skip to the next iteration of the loop
}
//process remaining rows - nothing will happen here for that first placeholder row
}
}
?>
I'd like to check if there is a value on an array like this:
function check_value_new ($list, $message) {
foreach ($list as $current) {
if ($current == $message) return true;
}
return false;
}
function check_value_old ($list, $message) {
for ($i = 0; $i < count ($status_list); $i ++) {
if ($status_list[$i] == $$message) return true;
}
return false;
}
$arr = array ("hello", "good bye", "ciao", "buenas dias", "bon jour");
check_value_old ($arr, "buenas dias"); // works but it isn't the best
check_value_new ($arr, "buenas dias"); // argument error, where I'm wrong?
I've read the check_value_new method is a better way to work with arrays, but I'm not used to work with it, how I should fix it?
PHP offers a function called in_array that checks if a value exists in a given array.
You can change your check_value_new function to include this:
function check_value_new ($list, $message) {
foreach ($list as $current) {
if (in_array($message, $current)) {
return true;
}
return false;
}
If you'd like to, you could also make the function work without the foreach loop, like so
function check_value_new ($list, $message) {
// returns true if found, else returns false.
return in_array($message, $list);
}
The reasons that your check_value_old() function breaks is because you have a typo in your loop's second parameter. $status_list is not one of the variables passed into your function and it is not declared anywhere before it is used. You merely need to use $list instead. It is also not necessary to call count() on every iteration -- since that value never changes in the loop, just call it once before the loop and use that value in the second parameter.
As for deciding how to loop over array values, there is no reason to reinvent a native function call. in_array() was already designed and optimized to search linear values in an array and return true as soon as it finds a match; otherwise it returns false.
I don't know what #AnthonyForloney is doing by writing in_array() inside of a foreach() loop. Not only will the snippet break because there aren't enough curly braces, the input array does not have two levels of data, so in_array() will choke when it receives $current which is a string (not the expected array).
Ultimately, my advice is: Only write your own custom function when there isn't a native function to suit your needs. Throw away both of the manual looping ideas and only call:
$arr = ["hello", "good bye", "ciao", "buenas dias", "bon jour"];
var_export(in_array("buenas dias", $arr));
One final insight that I'd like to provide is regarding microoptimization. If you are planning to check the existence of a unique value in a "very large" array, or if you are going to be making thousands of searches on the same array, then it would be more efficient to set up your array in a "flipped" orientation and call isset() or array_key_exists(). isset()/array_key_exists() will always outperform in_array() or array_search() because of the way that php handles arrays as "hash maps". More reading here.