Get input name from request, Laravel - php

In the laravel how can we made something like this?
For example if we write dd($request); in the controller:
"slug_en" => "english_slug_here"
"lang_en" => "english"
"slug_es" => "spanish_slug_here"
"lang_es" => "spanish"
If i must use "english", simply i can use $request->lang_en;
But what if i know just "english" and want to know input name?
$request->X = "english";
I want to X right here. I need to set language system with dynamically, but i stuck right here. If can anybody help me, i'll glad so much. Thanks in advance.

Try this
$x = array_search ('english', $request->all());

use
$x = array_keys ($request->all(),'english');
returns all the keys having value english
if given only the array returns all the keys. The search field is included as a second parameter to get keys for the given search value and there is a optional third parameter strict which may be either true or false, if set strict validation occurstrue

Related

mustache use current array value to index an array

In mustache:
I have 'matches'=>['foo', 'bar']. I also have:
[
'deals'=> [
'foo' => new Deal('name1'),
'bar' => new Deal('name2'),
'baz' => new Deal('name3')
]
]
What I am trying to do is this:
{{#matches}}
{{deals}}.{{.}}.{{name}}
{{/matches}}
Which doesn't work.
This works, except it isn't dynamic like I need:
{{#matches}}
{{deals.bar.name}}
{{/matches}}
Any thoughts or suggestions?
You may want to make a projection ahead of time that filters deals on matches in code before applying it to the template. If I'm understanding right, you're attempting to embed matching logic in the template which Mustache doesn't generally support.
You could either filter matches, or apply a Boolean property to each describing whether it has a match.

Comparing two or more values string in php

Can i check a data is exist on result data loop? and if it doesn't exist will be shown. for example,
i have a data on loop method like 1234, 2345, and 3456. and i want to check anywhere the same with my data like 1234, 9212, 3333, and 2345. and result must be..
"Found 1234, 2345. Not found number 9212, 3333"
anyone please can help me..
with a pleasure if there is someone to help me solve this problem :)
Thanks before.
You question is not very clear, but based on your explanation, instead of a loop, you may want to just use the build array_interssect function:
http://php.net/manual/en/function.array-intersect.php
This will let you compare two arrays, and check what values on one array exsits in the other.
For ex.:
$exists = array_intersect([1234,2345,67879], [1234,2345,9434,6439]);
This will result in $exist being an array with with two items [1234,2345]. Then you know the rest doesn't exist.
I hope this helps.

Modification in $_REQUEST response or any alternative to get correct value in array using explode

In my application there are different promotions and usera can answer to that promotions using single or multi choice questions. I am using following logic and it works fine except one problem:
for ($i=0;$i<count($questions);$i++)
{
if($questions[$i][type_of_question] == 'multiple_choice')
{
$options_array = explode(",", $_REQUEST["que".$questions[$i][id]]);
}
// Code here which insert answers in database table
}
This line of code:
$options_array = explode(",", $_REQUEST["que".$questions[$i][id]]);
this code shows comma seperated answers of the questions. For example:
Example 1: test1,test2,test3 [This scenario works fine]
Example 2: yes,no,I am not interested(cricket,footbal)
Example 2 has shows the problem e.g. if there is , inside the choice option. Php explode function break and values according to , so in this case I gets 4 array elements.
Array[0] => yes
Array[1] => no
Array[3] => I am not interested(cricket
Array[4] => footbal
while I required following:
Array[0] => yes
Array[1] => no
Array[3] => I am not interested(cricket,footbal)
How can I fix this problem? Any suggestion?
You need to fix the way the data is being fed in from the front-end: there's no way in this code alone to fix this problem. For example, you could have the input yes, no, maybe, and it could be either yes, no, maybe, or yes, no, maybe.
You need to perform some consistent encoding or escaping on the front-end so you can uniquely access the records. You haven't told us how it's submitted on the front, but just joining the answers together with commas won't be enough. You could try escaping the commas in the fields!

How to get value for multivalued get parameter in symfony2

How do you get the value for a get parameter that is a list of values? I tried using request->query->get('parameter') but this only returns one value.
Suppose you have a country parameter and want to pass more than 1 value. e.g. ...?country=us,gb using get gives you 'us' only.
I can not find this in the docs.
You have to format your query parameter this way
country[]=us&country[]=gb
From their manual (http://symfony.com/doc/current/book/http_fundamentals.html)
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
$request->query->get('foo');
Edit:
Sorry, I misunderstood your question. You want an array of all GET valiables like $_GET will give you?
That can be done with (will also include POST parameters):
<?php $request->getParameterHolder()->getAll();

Unique variables inside foreach()

Trying to create a variable with unique name for each $item.
To prevent error "Only variables can be passed by reference".
If there are 5 items in array $items, we should get 5 unique variables:
$item_name_1;
$item_name_2;
$item_name_3;
$item_name_4;
$item_name_5;
All of them should be empty.
What is a true solution for this?
You can dynamically create variable names doing the following:
$item_name_{$count} = $whatever;
I must warn you though, this is absolutely bad style and I've never seen a good reason to use this. In almost every use case an array would be the better solution.
Well, I guess that you can use $item_name_{$count} = "lorem ipsum"; for it
... But won't be better to use an array?
I'm not sure I understand what you want to do, so this may be completely wrong. Anyway, if what you want is an array with empty values you can use this code:
<?php
$arr = array_fill(0, 3, '');
var_export($arr) // array ( 0 => '', 1 => '', 2 => '', 3 => '', )
?>
See array_fill for more information.
If this is the wrong answer, please clarify what you mean. I may be able to help you.

Categories