Comparing two arrays in PHP foreach - php

What's the best way to compare both two arrays to each other in a foreach loop ?
I have two arrays, which one holds a boolean values and other one represents the real values.
First one of the arrays is some sort of configuration file where I tell what field is a required one TRUE or an optional FALSE:
$rules = array(
'fieldname1' => TRUE,
'fieldname2' => FALSE,
'fieldname3' => TRUE,
'fieldname4' => TRUE,
'fieldname5' => FALSE,
'fieldname6' => FALSE,
'fieldname7' => TRUE,
'fieldname8' => TRUE
);
And the other array which holds incoming data to be worked out:
$fields = array(
'fieldname1' => 'some value',
'fieldname3' => 'some value',
'fieldname4' => 'some value',
'fieldname7' => 'some value',
'fieldname8' => 'some value'
);
How do I make a check in foreach() loop, so the $fields array is going to compare each of its fieldnameX or (keys/indexes) with the other array named $rules ?
For example: $rules['fieldname1'] is required as it have a bool TRUE. So in foreach($fields as $field), the $field named fieldname1 should not be empty, null or false, if so, show an error.
This is a function I did so far which is not doing exactly what I want:
private function check_field_required_exist( $fields )
{
// Read rules from config to work with them accordingly
$rules = config_item('flow.format')[$fields['format']];
// Run throught the rules
foreach( array_keys($rules) as $field )
{
// Check wheter key existance is failed
if( !array_key_exists($field, $fields) )
{
// Checking failed, terminate the process
die('Required field: "'. $field .'" for requested format: "'.$fields['format'].'" does not exists.');
}
}
// Everything is alright
return TRUE;
}

// Goes through all the rules
foreach($rules as $key => $value)
{
// access all fields that are marked as required i.e key set as TRUE
if ($value)
{
//for those whose value is TRUE, check whether the corresponding field is set or
// whether it is empty (NULL,false)
if(!isset($fields[$key]) || empty($fields[$key]))
{
die('Required field does not exist');
}
}
}

foreach ($fields as $field => $value)
{
if (array_key_exists($field, $rules) && $rules[$field] === true)
{
if (!$value || $value == "" || $value === null)
{
die(); // return false;
}
}
}
return true;

I think that you can play around with the array functions in PHP. First thing I would do is to apply array_filter on your rules array. That way, you will only keep the TRUE values (and their indexes, since array_filter keep the index/value association).
Then I would use array_intersect_key to keep only the fields of interest in your fields var. And again array_filter with a custom callback to perform the verification you want to do.

$requiredField = array();
foreach($rules as $key=>$value) {
if($value == true && (!isset($fields[$key]) || $fields[$key] == "" )) {
$requiredField[] = $key;
}
}
if(count($requiredField) > 0) {
echo implode(',',$requiredField)." Are Required";
die;
}

USING PHP Array Functions:
$rules = array('name' => TRUE, 'phoneNumber' => TRUE, 'age' => FALSE);
$submittedFields = array( 'name' => 'aspirinemaga', 'age' => 38, 'phoneNumber' => '');
$requiredFields = array_keys(array_filter($rules));
$validFields = array_keys(array_filter($submittedFields));
$missingFields = array_diff($requiredFields, $validFields);
Given
$rules = array('name' => TRUE, 'phoneNumber' => TRUE, 'age' => FALSE);
$submittedFields = array( 'name' => 'aspirinemaga', 'age' => 38, 'phoneNumber' => '');
PhoneNumber is wrong here.
$requiredFields = array_keys(array_filter($rules));
This gives you the keys of all rule elements that are TRUE.
$validFields = array_keys(array_filter($submittedFields));
Same here, your definition of what's ok is the same as php truth table for boolean conversion. If you have a more advanced check, you could pass a function name into array_filter (see below).
$missingFields = array_diff($requiredFields, $validFields);
This will get you all the fieldNames that exist in $requiredFields but are not in $validFields.
More advanced validation
$rules = array('name' => 'MORE_THAN_3', 'phoneNumber' => TRUE, 'age' => 'POSITIVE');
$submittedFields = array( 'name' => 'aspirinemaga', 'age' => 38, 'phoneNumber' => '');
$requiredFields = array_keys(array_filter($rules));
function validateField($key, $value){
// get the rule (assuming no submittedFields that have no rule)
$rule = $rules[$key];
switch($rule){
case 'MORE_THAN_3': return strlen($value) > 3; break;
case 'POSITIVE': return $value > 0; break;
}
// in case of TRUE/FALSE
return TRUE;
}
$validFields = array_keys(array_filter("validateField", $submittedFields, ARRAY_FILTER_USE_BOTH));
$missingFields = array_diff($requiredFields, $validFields);
If you wanted to do a more advanced validation, you can integrate that nicely into above solution.
Say you wanted name to be more than 3 characters and age to be positive. Write it into your rules:
$rules = array('name' => 'MORE_THAN_3', 'phoneNumber' => TRUE, 'age' => 'POSITIVE');
$submittedFields = array( 'name' => 'aspirinemaga', 'age' => 38, 'phoneNumber' => '');
Required Fields are still the same (to check if one is missing).
$requiredFields = array_keys(array_filter($rules));
Now your validate function would be something like:
function validateField($key, $value){
// get the rule (assuming no submittedFields that have no rule)
$rule = $rules[$key];
switch($rule){
case 'MORE_THAN_3': return strlen($value) > 3; break;
case 'POSITIVE': return $value > 0; break;
}
// in case of TRUE/FALSE
return TRUE;
}
Pass that into array_filter and add to ARRAY_FILTER_USE_BOTH Flag:
$validFields = array_keys(array_filter("validateField", $submittedFields, ARRAY_FILTER_USE_BOTH));
And your missing Fields stays the same.
$missingFields = array_diff($requiredFields, $validFields);
This will only work in PHP 5.6+ (because ARRAY_FILTER_USE_BOTH). Not tested, should work.

Related

Can I make a if-else statement based on a array?

Here is my current code:
if ( by == 'id') {
handlePromises(identifier_websites)
} else if ( by == 'name'){
handlePromises(names_websites)
} else if ( by == 'email' ) {
handlePromises(email_websites)
} else if ( by == 'phone' ) {
handlePromises(phone_websites)
}
Now I want to make it a little bit more modular (functional). I mean, I want to make the whole code above based on an array like this:
$parts = ['id', 'name', 'email', 'phone'];
So if I add one item to that array, then automatically a else if block should be added to the code based on that item .
Is doing that possible?
Not sure if I get you but here's how you can automate the above slightly.
$parts = [
'id' => identifier_websites,
'name' => names_websites,
'email' => emails_websites,
'phone' => phone_websites
];
foreach ($parts as $cond => $params) {
if (by == $cond) {
handlePromises($params);
break;
}
}

PHP: Rewriting switch as class

So, I have a switch statement inside a foreach loop that checks the value of a field in the array and uses the value to build a named array and email address variable for each unique value. Question is below example code.
As an example, if the values are foo, bar, and foobar, then the following occurs:
switch($value['value']) {
case 'foo':
if(!isset($foo)){
$foo = array();
}
if(!isset($email_foo)){
$email_foo = convert_to_email($value['value']);
}
array_push($foo, $value);
break;
case 'bar':
if(!isset($bar)){
$bar = array();
}
if(!isset($email_bar)){
$email_bar = convert_to_email($value['value']);
}
array_push($bar, $value);
break;
case 'foobar':
if(!isset($foobar)){
$foobar = array();
}
if(!isset($email_foobar)){
$email_foobar = convert_to_email($value['value']);
}
array_push($foobar, $value);
break;
default:
if(!isset($default)){
$default = array();
}
if(!isset($email_default)){
$email_default = 'this#isemail.com';
}
array_push($default, $value);
}
Resulting in 4 different email addresses:
$email_foo = 'foo#isemail.com';
$email_bar = 'bar#isemail.com';
$email_foobar = 'foobar#isemail.com';
$email_default = 'default#isemail.com';
and 4 different arrays of data:
$foo = array(
0 => array(
'value' => 'F Oo',
'name' => 'Janet',
'age' => 23
),
1 => array(
'value' => 'F Oo',
'name' => 'Doug',
'age' => 42
)
)
$bar = array(
0 => array(
'value' => 'B Ar',
'name' => 'James',
'age' => 23
),
1 => array(
'value' => 'B Ar',
'name' => 'Donald',
'age' => 42
)
)
etc...
So, here is the question:
Is it possible to write a class that can be used to create all of the named arrays and email variables? Sort of something like this:
class Account_Manager_Build
{
public function __construct()
{
if(!isset($this)){
$this = array();
}
if(!isset("email_$this")){
"email_$this" = convert_to_email($this->value['value']);
}
array_push($this, $this->value);
}
}
For testing purposes, here is the function convert_to_email that is used throughout the examples:
function convert_to_email($input){
$returned = strtolower(substr($input, 0, 1)).strtolower(end(str_word_count($input, 2))).'#ismail.com';
return $returned;
}
Actually, you can create dynamically variables using the $$ notation:
$variable = "email_$name";
$$variable = "something";
However I'm not sure of what you're trying to do
What's the point of computing $email_something if it can be easily computed from something?
Why do you create those variables instead of just storing this in an array?

Changing key's name in a array

So, having an array like the following:
$input = [
'first_name' => 'Alex',
'last_name' => 'UFO',
'email' => 'test#test.com',
'phone_number' => '124124',
// .....
'referral_first_name' => 'Jason',
'referral_last_name' => 'McNugget',
'referral_email' => 'jingleball#nuggets.com',
'referral_phone_number' => '1212415',
];
After processing the first part, until referral..., do you think of any better way to replace referral_first_name with first_name and so on, then the following? Maybe a more dynamic and automatic way.
$input['first_name'] = $input['referral_first_name'];
unset($input['referral_first_name']);
$input['last_name'] = $input['referral_last_name'];
unset($input['referral_last_name']);
$input['email'] = $input['referral_email'];
unset($input['referral_email']);
$input['phone_number'] = $input['referral_phone_number'];
unset($input['referral_phone_number']);
Guys, I forgot to mention, but I have already done it with foreach, but the problem will be when the array gets pretty large (and usually does, and not by one person using that function, but by many), and that would mean extra unnecessary processing time, since it has to iterate through the whole array, before reaching the referral_.. part.
you must create another array, this code should do it dynamically:
$newInput = array();
foreach($input as $key => $element){
$newKey = explode("_", $key, 2);
$newInput[$newKey[1]] = $element;
}
OUTPUT
hope this helps :-)
Recreating the array is the only way..
Grab all the keys from the original array and put it in a temp array
Use array_walk to modify that temp array to add the referral word to it
Now use array_combine using the above keys and values from the old array.
The code..
<?php
$input = [
'first_name' => 'Alex',
'last_name' => 'UFO',
'email' => 'test#test.com',
'phone_number' => '124124'];
$ak = array_keys($input);
array_walk($ak,function (&$v){ $v = 'referral_'.$v;});
$input=array_combine($ak,array_values($input));
print_r($input);
OUTPUT:
Array
(
[referral_first_name] => Alex
[referral_last_name] => UFO
[referral_email] => test#test.com
[referral_phone_number] => 124124
)
Since you are looking for performance , Use a typical foreach
$newarr = array();
foreach($input as $k=>$v ) {
$newarr["referral_".$k]=$v;
}
How about something like this:
$replace = array('first_name', 'last_name', 'email');
foreach($replace AS $key) {
$input[$key] = $input['referral_' . $key];
unset($input[$input['referral_' .$key]]);
}
try the below one...
$input = [
//'first_name' => 'Alex',
// 'last_name' => 'UFO',
// 'email' => 'test#test.com',
//'phone_number' => '124124',
// .....
'referral_first_name' => 'Jason',
'referral_last_name' => 'McNugget',
'referral_email' => 'jingleball#nuggets.com',
'referral_phone_number' => '1212415',
];
foreach ($input as $key=>$value){
$refereal_pos = strpos($key, 'referral_');
if($refereal_pos !== FALSE && $refereal_pos == 0){
$input[str_replace('referral_', '', $key)] = $value;
unset($input[$key]);
}
}
print_r($input);
You can use following;
function changeKeys($input) {
$keys = array_keys($input);
foreach($keys as $key) {
if (strpos($key, "referral_") !== false) {
$tempKey = explode("referral_", $key);
$input[$tempKey[1]] = $input[$key];
unset($input[$key]);
}
}
return $input;
}
changeKeys($input);
Here is a working demo: codepad
Note: Be sure that, your keys overwrited due to duplicate keys after "referral_" removal

PHP - good practice reading POST variables

I am thinking of a good practice to read the client submitted POST data.
For example if I have a post variable that should have the following structure:
array(
[0] => array(
['test'] => array(1, 2, 3),
['test2'] => "string"
),
[1] => array(
['test'] => array(),
['test2'] => "string2"
),
)
Where the indices 'test' and 'test2' should always be present but their values may be empty (array() and "");
The functions that handle the POST data are expecting the correct format, so I have to make sure that the data has not been manipulated.
I could do the following:
$result = array();
if(isset($_POST['myVar']) && is_array($_POST['myVar'])) {
foreach($_POST['myVar'] as $array) {
$new = array('test' => array(), 'test2' = "");
if(isset($array['test']) && is_array($array['test'])) {
foreach($array['test'] as $expectedInt) {
$new['test'][] = (int)$expectedInt;
}
}
if(isset($array['test2']) && is_string($array['test2']))
$new['test2'] = $array['test2'];
}
$result[] = $new;
}
I think you get the idea what I mean. I wonder if there is a better practice of reading the POST data into the expected format.
I usually do this to assure I have default indices:
$par = $_POST;
$par += [
'key' => 'default',
'other' => 'default',
]
If $par doesn't contain those keys, they are set.
In your case, your could do this:
$ready = [];
foreach($_POST as $k => $v){
$v += [
'test' => [],
'test2' => "string2",
];
// Validate if needed
$v['test'] = (array)$v['test'];
$v['test2'] = (string)$v['test2'];
$ready[$k] = $v;
}
Later you can be sure, that $ready will contain values with test and test2 keys.
This is very useful in functions, where you replace a lot of arguments with one parameter array, and then later set default values,

How to set url parameters in an array?

Hey guys I am using open id authentication on my website and after authenticating I am getting a url from openid providers i.e yahoo and google
http://www.mysite.com/openid-login.php?
openid.identity=https://me.yahoo.com/a/1234567&
openid.ax.value.nickname=john&
openid.ax.value.email=john#yahoo.co.in&
http://www.mysite.com/openid-login.php?
openid.identity=https://www.google.com/accounts/o8/1234567&
openid.ext1.value.email=kevin#gmail.com&
openid.ext1.value.country=IN
I have trimmed the urls a bit for clarity. I would like to create a single function for both that can set the email(if exists), nickname(if exits), identity(openid ina ) in an array and return the values. eg.
function userdetails(array_get){
......
......
return $userdetails;
}
$userdetails =userdetails($_GET);
$userdetails['nickname'] would give me the nickname if exists and similarly for the email and identity. Thanks
I did not create this function nor take credit for it. This was pulled and modified from the Simple OpenID library. If somebody has a link please post it in the comments as I don't have access to the original source.
/**
* Method to filter through $_GET array for requested user information.
* Will return an array of trimmed userinfo.
*/
public function filterUserInfo($arr_get) {
$valid_ax_types = array('nickname' => 1, 'email' => 1, 'fullname' => 1, 'dob' => 1, 'gender' => 1, 'postcode' => 1, 'country' => 1, 'language' => 1, 'timezone' => 1, 'firstname' => 1, 'lastname' => 1);
$userinfo = array();
foreach ($arr_get as $key => $value) {
// trim the key
$trimmed_key = substr($key, strrpos($key, "_") + 1);
// check for valid openid_ext1 values
if (stristr($key, 'openid_ext1_value') && isset($value[1])) {
$userinfo[$trimmed_key] = $value;
}
// check for valid openid_ax values
if (stristr($key, 'openid_ax_value') && isset($value[1])) {
$userinfo[$trimmed_key] = $value;
}
// check for valid sreg_ values
else if (stristr($key, 'sreg_') && array_key_exists($trimmed_key, $arr_ax_types)) {
$userinfo[$trimmed_key] = $value;
}
}
return $userinfo;
}
I thought by "getting a url" you meant actually a URL. Accept the answer below me. :)
parse_url()
parse_str()
function userdetails( $url, $keep = array( 'email', 'nickname', 'identity' ) ) {
$array = parse_str( parse_url( $url, PHP_URL_QUERY ) );
$return = array();
foreach ( $keep as $key ) {
if ( isset( $array[ $key ] ) ) {
$return[ $key ] = $array[$key];
}
}
return $return;
}

Categories