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.
Related
I have an array named 'users' in which I am storing some variables as you see below.I want to verify if the 'nume' (name) value already existing in this array is the same as the value introduced by a person in a search box. I've tried some methods but I haven't obtained the wanted result. I think it is something obvious I can't see at the moment.
EDIT : The image is the result of var_dump() of my code. I am using it for people to understand what I want to know.
it's simple, try that:
$searchValue = 'victor';
$index = array_search($searchValue, array_column($users, 'nume'));
I have a dynamic amount of arrays in a specific array.
Let's call this specific array: FatherArray
This FatherArray has a dynamic amount of arrays in it, right now for example: Child1Array,Child2Array. Next time it gets called it could have more or less than those 2 Child(number)Arrays.
So I want to use the function array_intersect() with the arrays (children) of FatherArray as parameters, so like array_intersect(Child1Array,Child2Array).
I don't have a clue how i could do this dynamically, neither could I find anything about it, any help would greatly be appreciated
If your version is reasonably new (v5.6):
array_intersect(...$FatherArray);
Otherwise:
call_user_func_array('array_intersect', $FatherArray);
Demo: see comment by Mark (thx #MarkBaker)
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];
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!
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...
}