Matching a string with an array key - php

I'm trying how to figure something out for my website.
This is not my code just an example to make it easier to understand what i mean.
Lets say i have an array like this:
$services = array(
Marketing => marketing:342343423423,
Sales => sales:779876786,
)
And i have a form on my website. I can get the posted values with a POST request.
The POST request can for example look like this
$_POST['service_request']
Now what i want to know is how to do the next:
if $_POST['service_request'] matches one of the array keys inside $services then print the relevant value of this array key.
So lets say an user fills my form, and his service request is marketing then i want to check if this service request exists inside the $service variable and if it exist print the value.

use key_exists function of php.
if(key_exists($_POST['service_request'],$services)){
//exists, perform rest of the logic here.
}
Update: it's an alias of array_key_exists so both are basically same.

Edit:
Code below can give an exception if the key isn't set. You should use the key_exists, given in the other answer. Or change it to:
if ( isset($services[$_POST['service_request']]) ) {
echo $services[$_POST['service_request']];
}
This code will print the value from the array with the given key:
$value = $services[$_POST['service_request']];
echo (isset($value) ? $value : '');

Related

Laravel Eloquent: any way to update DB with dynamic fields?

I am trying to implement the "Edit Application Settings" feature. After a bit of thinking, my configuration values are stored in the DB with key -> value structure, like this:
id
key
value
1
logo_path
img/logo.png
As you can see, for each setting, there is only a key & value column. I made an App Service provider to cache them forever, and a helper function (config('setting_key')) to get the value, but now I'd like to update it in the most efficient way.
The user interface consists of the <form action="post" ...> and input with a corresponding name, like this: <input name="setting_key_name" ... />. As you can see, the name attribute here has the value of the key column value and the actual value of the input would be the value column value (a bit of confusion here).
First thing that came to my mind, was to make a foreach loop and find & update every row in DB, but IMHO it is very unoptimized way, cause if the page has a form with 10 values, it is 10 SQL queries. But till now, this is what I've done:
$keys = collect($request->except('_token'))->keys()->toArray();
// get all settings if the key name matches the request's input name
$setting = Setting::whereIn('key', $keys)->get();
$logo = self::GENERAL_APP_LOGO; // contant with a key-name (general_application_logo);
if($request->has(self::GENERAL_APP_LOGO) && $request->$logo) {
// Processing uploaded image here;
$this->uploadLogo($image, self::LOGO_IMAGE_PATH, $name); // Using an upload trait
$setting->where('key', $logo)->value = self::LOGO_IMAGE_PATH . $name; // just a try to update the DB this way
}
foreach ($keys as $key) {
$setting->where('key', $key)->value = $request->$key; // putting all request's input values to corresponding key
}
$setting->save(); // saving the DB.
As you can see, this won't work and will throw an Exception, like Call to undefined method ...\Eloquent\Builder::save(). I tried the same code with an update, but the difficult part here is to update it multiple times (since the if section should have the update as well, for the logo), as well as binding the key to value.
So, a little bit of your help would be appreciated - what the logic should be here? How can I update a DB rows with corresponding column's value? I mean - like this (update where key = 'general_app_name' set value, 'some_setting_value'), but using the optimized and clear way?
Working solution
As #miken32 stated in his answer, I used hid version of code, but with slight changes:
// Changed the $request->settings->keys() to PHP native method array_keys():
$settings = Settings::whereIn('key', array_keys($request->settings))->get()->groupBy('id');
// Also, here I changed the `whereIn('id', ...)` to `whereIn('key', ...)`, since it was my primary index.
foreach ($request->settings as $k=>$v) {
if ($k === self::GENERAL_APP_LOGO_ID) {
// not sure about this one, but I think this is
// how you'd access a file input in an array
$image = $request->file('settings')[$k];
$this->uploadLogo($image, self::LOGO_IMAGE_PATH, $name);
$v = self::LOGO_IMAGE_PATH . $name;
}
// take the Setting object out of the list we pulled
// Here I added the ->first() to get the first element from the retrieved collection;
$setting = $settings->get($k)->first();
$setting->value = $v;
$setting->save();
}
Since I was fetching the configuration values via helper, that only returns the value of the current key (and no id column), I changed the id to key and made the key as my PK in a model. Works like a charm!
With each setting in a separate row, there's no way to avoid multiple database queries – one to get the current values for all settings, and other to update each one. Looking up items by primary key is more efficient, so I'd recommend putting the contents of the id column in your blade view, like this:
<label for="setting_{{$setting->id}}">{{$setting->key}}</label>
<input name="settings[{{$setting->id}}]" id="setting_{{$setting->id}}" value="{{$setting->value}}"/>
Now in your controller, $request->settings will be an array you can loop through. You can continue treating your file upload separately, but now you've got the id column to look up, so change your constant to that.
$settings = Settings::whereIn('id', $request->settings->keys())->get()->groupBy('id');
foreach ($request->settings as $k=>$v) {
if ($k === self::GENERAL_APP_LOGO_ID) {
// not sure about this one, but I think this is
// how you'd access a file input in an array
$image = $request->file('settings')[$k];
$this->uploadLogo($image, self::LOGO_IMAGE_PATH, $name);
$v = self::LOGO_IMAGE_PATH . $name;
}
// take the Setting object out of the list we pulled
$setting = $settings->get($k);
$setting->value = $v;
$setting->save();
}
Note that Laravel does offer methods to bulk-update multiple models at once, but they are doing separate queries to the database in the background. IIRC, the save() method doesn't do anything if the value hasn't changed, which will spare you some hits.
You could try creating a text field, or a json field if your database supports it, and storing all of your settings as a JSON string in that field.
id
settings
1
{ "logo_path" : "img/logo.png", "foo" : "bar", "thing_count" : 17 }
2
{ "logo_path" : "img/logo2.png", "foo" : "baz", "thing_count" : 4 }
In your Laravel model, you can cast it as an array
protected $casts = ["settings" => "array"];
and then use it from the model
echo $theModel->settings['logo'];
echo $theModel->settings['foo'];
or you can cast it as a fully fledged object if you need to using value object casting.
One gotcha that can be confusing for people is the setting of the values in the array to update it. This will not work:
$theModel->settings['foo'] = "boz";
The reason is due to the way the Laravel mutators work. Instead, you make a value copy of the settings, change that, and reassign it to the model:
$settings = $theModel->settings;
$settings['foo'] = "boz";
$theModel->settings = $settings;
This approach has the capacity to infinitely expandable in the future as you just add new keys to your json. Be sure to do checks on the settings array to ensure fields you are looking for are set (which is why value objects can be very handy to do validation).
It also solves your database query problem - it's only ever one.
You don't need to put
$setting->where('key', $logo)->value = ...;
Just call
$setting->where('key', $logo)->update($request->toArray());
$setting->save(); called when you instantiated setting class like :
$setting = new Setting();
Or
$setting = Setting::whereIn('key', $keys)->get()->first();
Then
$setting->val = ...;
$setting->save(); // then it work's

Creating appropriate array from request values in laravel controller

I'm trying to figure out how to properly structure request data into an array in my controller. I have 3 values coming from my http request and I'm trying to get two of them into a specific array structure. My request values are dumping so they are definitely there.
Here's my controller where I"m putting them into an array:
$subItems = array("title" => $request->itemTitle, "description" => $request->itemDesc);
But it's failing because the function I'm calling with the array $subItems is expecting an array structure where it can get the paramaters by the index of title and description like this:
[ $subtask['title'], $subtask['description']]
How do I need to change my controller/array code to accomplish this?
You can do it like below
$subItems = [];
$subItems['title'] = $request->itemTitle;
$subItems['description'] = $request->itemDesc;
You can use like this,
$subItems['title'] = $request->itemTitle;
After you cant get values using its key.
In your question you add values to $subItems but you getting values from $subtask Please check that one also.

validate fields where field name suffixed with number in laravel

I have dynamically created element in JQuery where i have assigned controls name as location_1,location_2,location_3, floor_1, floor_2,floor_3 and so on. It was not possible to use html-control array because these all fields are interdependent. So for me the ultimate solution was suffixing number.
Now i want to validate these fields where rules should looks something like
['location_*' => 'required|exists:locations,id'],
['Floor_*' => 'required|exists:floor,id']
how can i do this so that i could get an error back if it failed?
The easiest way is probably to loop over the request data and create rules based on the locations and floors you find. For instance for floors, given your example:
$rules = [...] // initialise your rules array here
$locationRule = 'required|exists:locations,id';
foreach ($request->all() as $input) {
if (preg_match('#^location_#', $input) {
$rules[$input] = $locationRule;
}
}

PHP Function with array_search Not working

I have a function using array_search not working ... here is my code
function LangFull($name){
$languageCodes = array(
"abkhazian"=>"ab",
"afar"=>"aa",
"afrikaans"=>"af",
"afrikaans"=>"af-za",
"zulu"=>"zu",
"zulu"=>"zu-za"
);
return ucwords(array_search(strtolower($name),$languageCodes));
}
echo LangFull("zu"); /// Gives no output
echo LangFull("zu-za"); /// Gives output
same with af is no output ... please help
If its possible to interchange, (values to keys and keys to values) and won't have those key collisions, then you can do it that way also:
function LangFull($name){
$languageCodes = array(
"ab" => "abkhazian",
"aa" => "afar",
"af" => "afrikaans",
"af-za" => "afrikaans",
"zu" => "zulu",
"zu-za" => "zulu",
);
return isset($languageCodes[$name]) ? ucwords(strtolower($languageCodes[$name])) : 'Not found';
}
echo LangFull("zu"); /// Gives output
echo LangFull("zu-za"); /// Gives output
echo LangFull("yahoo!");
You have two identical array keys:
"zulu"=>"zu",
"zulu"=>"zu-za"
You need to name one of them something else.
As they are the same, trying to access one of them specifically is futile as PHP does not know which of the two you are requesting.
Alternatively, if you are trying to store more than 1 data value for a given key, you can make the value of a key an array, so can then store more data as required.
e.g.
array (
"afrikaans"=> array(
"af",
"af-za",
),
"zulu"=> array(
"zu",
"zu-za",
)
);
EDIT.
In response to you asking about swapping the keys and values:
You can, and Ghost has shown you how.
However retaining your keys as they are (as my above array example) allows you to collate all relevant data into one index, and can access it easily.
Swapping values and keys will likely make it harder to obtain data you need, as your key is now the "data". So to grab data from an array you'd need to know the data (as it's now the key) and you'd actually be grabbing the reference (what was your key).
Which is a bit odd. It can work, but it's not really ideal.

PHP find the array index key of multi dimensional array to update array

I am trying to come up with a means of working with what could potentially be very large array sets. What I am doing is working with the facebook graph api.
So when a user signs up for a service that I am building, I store their facebook id in a table in my service. The point of this is to allow a user who signs up for my service to find friends of their's who are on facebook and have also signed up through my service to find one another easier.
What I am trying to do currently is take the object that the facebook api returns for the /me/friends data and pass that to a function that I have building a query to my DB for the ID's found in the FB data which works fine. Also while this whole bit is going on I have an array of just facebook id's building up so I can use them in an in_array scenario. As my query only returns facebook id's found matching
While this data is looping through itself to create the query I also update the object to contain one more key/value pair per item on the list which is "are_friends"=> false So far to this point it all works smooth and relatively fast, and I have my query results. Which I am looping over.
So I am at a part where I want to avoid having a loop within a loop. This is where the in_array() bit comes in. Since I created the array of stored fb id's I can now loop over my results to see if there's a match, and in that event I want to take the original object that I appended 'are_friends'=>false to and change the ones in that set that match to "true" instead of false. I just can't think of a good way without looping over the original array inside the loop that is the results array.
So I am hoping someone can help me come up with a solution here without that secondary loop
The array up to this point that starts off as the original looks like
Array(
[data](
[0] => array(
are_fb_friends => false
name => user name
id => 1000
)
[1] => array(
are_fb_friends => false
name => user name
id => 2000
)
[2] => array(
are_fb_friends => false
name => user name
id => 3000
)
)
)
As per request
This is my current code logic, that I am attempting to describe above..
public function fromFB($arr = array())
{
$new_arr = array();
if((is_array($arr))&&(count($arr) > 0))
{
$this->db->select()->from(MEMB_BASIC);
$first_pass = 0;
for($i=0;$i < count($arr);$i++)
{
$arr[$i]['are_fb_friends'] = "false";
$new_arr[] = $arr[$i]['id'];
if($first_pass == 0)
{
$this->db->where('facebookID', $arr[$i]['id']);
}
else
{
$this->db->or_where('facebookID', $arr[$i]['id']);
}
$first_pass++;
}
$this->db->limit(count($arr));
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result = $query->result();
foreach($result as $row)
{
if(in_array($row->facebookID, $new_arr))
{
array_keys($arr, "blue");
}
}
}
}
return $arr;
}
To search a value and get its key in an array, you can use the array_search function which returns the key of the element.
$found_key = array_search($needle, $array);
For multidimensional array search in PHP look at https://stackoverflow.com/a/8102246/648044.
If you're worried about optimization I think you have to try using a query on a database (with proper indexing).
By the way, are you using the Facebook Query Language? If not give it a try, it's useful.

Categories