I'm starting to wonder if this is possible at all, but I'm using Laravel and I need to check to see if the $errors object contains any keys containing a search string.
I know it contains the keys 'monitors.0.size' and 'monitors.1.size' but I need to be able to check using just 'monitors'.
$errors->has('monitors') returns false and the attempts I've made at inserting different wildcards have resulted in page errors.
Am I missing something simple? Is there a way to do this?
as $errors is an instance of \Illuminate\Support\MessageBag I think the only way would be to loop over the $errors and compare them.
Related
So i am using this PHP code to create the json output, and I am having an issue where it’s creating an array of array with the info. I would like to get rid of one array and just display the list of API’s thats been used and number that has been used.
Looks as though the difference is you have...
"apis":[{"item_search":"0\n"},{"item_recommended":"0\n"}]
and want
"apis":{"item_search":"0\n","item_recommended":"0\n"}
If this is the case, you need to change the way you build the data from just adding new objects each time to setting the key values directly in the array...
$zone_1 = [];
foreach($zone_1_apis as $api_name ) {
$zone_1[substr($api_name, 0,-5)] = file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name);
}
You also need to do the same for $zone_2 as well.
It may also be good to use trim() round some of the values as they also seem to contain \n characters, so perhaps...
trim(file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name))
I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!
I am trying to do a thing that I dont know if can work. Also accept other ideas.
I have an array passed as a parameter where the index is the task id in the database and the value is the last syncronization for the task with the database
$sync=Array
(
[22805] => 1406822699
[22806] => 1406824500
[22807] => 1406838670
)
Then I do a select in the database which gives me the whole of tasks and I one to update on the database only some tasks, basically the ones that are out of date.
//$tasks is the list of all tasks from the database and $sync is the array which is pased by the user
foreach($tasks as $task)
{
if($task['sync']<=$sync[$task['taskList_id']])
{}
else
{//to be updated
$taskModel->updateLastSync($task['taskList_id'],$time);
$task['sync']=$time;
}
}
This is the problematic line and what I need to know how to do.
$sync[$task['taskList_id']]
I want to use a parameter as index to get the value of an array.
How can I achieve this.
Because this other idea is another foreach for $sync inside the foreach for $tasks
Without seeing the full script or having access to var_dump at certain locations it's hard to say what's going on but here are some things to check:
Make sure you're getting results from the database query and that they are assigned to the $tasks variable
Make sure your $sync array values and $task['sync'] are integer types and not strings or the '<=' comparison may have unexpected results
I don't know where $time gets its value from based on code I see so verify that it does actually have a value when you try to set it
A good first step when debugging is to try to isolate the problem's location first and then worry about the cause. Using something like var_dump to verify that values of certain variables are as expected at various locations in your script is very useful. Once you know where things start going wrong you can focus your attention in the right place to find out why and come up with a solution. I recommend doing this and letting us know what you find.
I solved my question.
Which was if this is allowed, I tested and it is.
$ar=array('1'=>'a','2'=>'b','3'=>'c');
$index=2;
echo $ar[$index];
I am trying to restrict content within a Wordpress template file and am using a plugin called Paid Memberships Pro to do so.
The code below restricts content to members with 'levels' of 1 or 2.
if(pmPro_hasMembershipLevel(array(1,2))){
restricted content goes here
}
The problem comes when I try to use a variable to provide the levels. These levels are held in a custom field group 'restrictions' with field name 'pmpro_id'. I access these levels within the template using...
foreach($restrictions as $restriction){
$temp=get_field('pmpro_id', $restriction->ID );
$temp_array[]=$temp;
}
$levels=implode(',', $temp_array);
If I then pass $levels to pmPro_hasMembershipLevel, this works fine if there is only one level but fails if there are 2 or more. I believe this is because the variable type is then a string rather than integer? I had previously tried to pass the $temp_array directly though I felt this wouldn't work and was correct.
I realise this is probably PHP 101. I have searched but don't really know what I'm looking for to be honest! I am not a developer and this is the last thing holding me back from finishing this project so ANY help anyone could provide would be brilliant. Thanks in advance.
You don't need to implode $temp_array if pmPro_hasMembershipLevel accepts array as its argument. When you implode an array you get string as a return value — that's not what you want here. If you think that the issue might be with the type of values, then you can try to cast them to integers, like this $temp_array[]= (int) $temp;
I'm working on a project where all of the members and their info are stored in a JSON file. I'm in the process of creating a search form and I need help on how to iterate through the members and check to see if there's an exact match or a similar match.
The members are stored in a SESSION variable:
$_SESSION['members'] = json_decode($jsonFile);
but I'm uncertain how to use regex to check for matches that are similar (and not just exact). For example, if a member's name is "Jonathan", I'd like that result to be returned even if the user searches "Jon". Is regex the correct approach? Any help will be greatly appreciated - thank you!
-Manoj
I think I'd be using a database to store the data rather than JSON so that you can use the LIKE searches, e.g.
SELECT * FROM users WHERE name LIKE 'Jon%'
If you absolutely have to use JSON you could loop through all members and use a regexp like
preg_match('/^'.$term.'.*/i', $element, $matches);
to check them all.
If the $jsonFile contents is an array of some sort, you may find preg_grep() of use, though it doesn't work on multidimensional arrays. You might have have to loop over each individual member record and grep the relevant fields yourself, something like:
foreach ($_SESSION['members'] as $idx => $member) {
... match relevant fields...
}