lookup function in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there any built in function to enable the lookup function in PHP like the excel one? I thought the array_search would be the closest one, but it won't work if the mixed needle(value) has more than one values. To illustrate, the lookup I try to perform is:
function lookup($datatype){
$array = array(
'BINARY' => 'RAW',
'REAL' => 'FLOAT',
'INTEGER' => 'NUMBER',
'DATETIME' => 'DATE',
'VARCHAR' => 'VARCHAR2',
'DATETIME' => 'TIMESTAMP',
'VARBINARY' => 'BFILE',
'INT' => 'NUMERIC'
);
$key = array_search($datatype, $array);
return $key;
}
If the lookup is for the 'RAW', array_search will return 'BINARY', but when the lookup is 'DATE', it won't return anything.
Any thoughts would be much appreciated.

the $array has the same key 'DATETIME' twice ,second one will overrite the first, so $array don't have the 'DATE' key

You can't have the same key twice in your array! You can instead of 'DATETIME' to 'DATETIME_test'
function lookup($datatype){
$array = array(
'BINARY' => 'RAW',
'REAL' => 'FLOAT',
'INTEGER' => 'NUMBER',
'DATETIME_test' => 'DATE',
'VARCHAR' => 'VARCHAR2',
'DATETIME' => 'TIMESTAMP',
'VARBINARY' => 'BFILE',
'INT' => 'NUMERIC',
);
$key = array_search($datatype, $array);
return $key;
}
var_dump(lookup('DATE'));

As you mentioned your array, there is duplicate key of "DATETIME", so it will overwrite by last value. and your array would be as follow.
$array = array(
'BINARY' => 'RAW',
'REAL' => 'FLOAT',
'INTEGER' => 'NUMBER',
'VARCHAR' => 'VARCHAR2',
'DATETIME' => 'TIMESTAMP',
'VARBINARY' => 'BFILE',
'INT' => 'NUMERIC'
);
so whenever you are trying to filter out or willing to get, only one key is available.
Better to write down with different key. and then try to array_search for key.

Related

Using Gravity Forms GFAPI to find duplicate entry with all identical field values

I'm using GFAPI to add an entry via GFAPI::add_entry function but before that, I wanna check if there's already an identical entry. Identical here means ALL the fields have the SAME values and SAME date_created.
I'm trying to detect duplication of an entry, not just a field in an entry. Preferably, able to detect duplicate "date_created" too. I have the code below and it doesn't work. Seems like "'mode' => 'all'" doesn't detect ALL fields, as stated in GF doc. This means if I have a new entry with all identical field values except for one field, the function will still indicate there's a duplicate.
I've been searching and found this but it's using a different approach and it's 7 years old so wonder if there's a better method. While this is checking for duplicates for specific field in the entire form. I'm basically checking if there's a entry with ALL the same field values.
function is_duplicate( $user_id, $currency, $description, $amount, $transaction_type, $date_created = '' ) {
$form_id = FORM_ID_FOR_TRANSACTIONS;
$date_created = is_string($date_created) ? $date_created : date_format($date_created, "Y-m-d");
$search_criteria = array(
'date_created' => $date_created,
'status' => 'active',
'field_filters' => array( //which fields to search
'mode' => 'all',
array(
'key' => '1', 'operator' => 'is', 'value' => $user_id,
'key' => '6', 'operator' => 'is', 'value' => $currency,
'key' => '3', 'operator' => 'is', 'value' => $description,
'key' => '4', 'operator' => 'is', 'value' => $amount,
'key' => '8', 'operator' => 'is', 'value' => $transaction_type
)
)
);
$result = GFAPI::count_entries( $form_id, $search_criteria );
if ( $result > 0 ) {
return true;
} else {
return false;
}
}

How to get a specific value based on adjacent key from a keyless multidimensional array?

Consider the following array:
$serviceNames = array(
0 => array(
'language' => 'en',
'value' => 'something',
'type' => 'name',
),
1 => array(
'language' => 'fi',
'value' => 'jotain',
'type' => 'name',
),
2 => array(
'language' => 'sv',
'value' => 'någonting',
'type' => 'name',
),
);
I need to get the 'value' definitions based on language. The problematic part is that the array $serviceNames does not have a predefined length (comes originally as a JSON file from an API), and the items can come in any order (in my example it goes like en, fi, sv, but it could be de, en, sv, fr... you get it).
If I wanted to get 'value' within the array where 'language' equals to 'en', how could I do that?
My advice is that you make the array associative.
Once that is done you access the value by ["language"]["value"].
$serviceNames = array_column($serviceNames, Null, "language");
echo $serviceNames["fi"]["value"]; //jotain
echo $serviceNames["en"]["value"]; //something
echo $serviceNames["sv"]["value"]; //någonting
https://3v4l.org/ssGQa
You can array_search() and array_column() function. first find the key where "en" is in the array then get the value.
$key = array_search('en', array_column($serviceNames, 'language'));
echo $serviceNames[$key]['value'];
Demo
simple:
$serviceNames = array(
0 => array(
'language' => 'en',
'value' => 'something',
'type' => 'name',
),
1 => array(
'language' => 'fi',
'value' => 'jotain',
'type' => 'name',
),
2 => array(
'language' => 'sv',
'value' => 'någonting',
'type' => 'name',
),
);
function myfunction(array $serviceNames, $field)
{
foreach($serviceNames as $service)
{
if ( $service['language'] === $field )
return $service['value'];
}
return false;
}
echo myfunction($serviceNames, 'en');
Output will : something
you would have to go through each elements of the array, using the foreach statement.
something like:
function getValueForLang($lang, array $arr)
{
foreach ($arr as $desc) {
if ($desc['language'] == $lang) {
return $arr['value'];
}
}
return null;
}
getValueForLang('en', $serviceNames); // gets your value, null if not found
see also:
https://secure.php.net/manual/en/control-structures.foreach.php

wp.editPost 'API Wordpress' doesn't edit custom fields

When I try to edit custom_fields with wp.editPost. Only edit the other fields, but not custom fields. Custom fields are created again(repeat fields), but will have to be edited.
I am looking: http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.editPost
My array with custom fields is:
$content = array(
'post_id' => (int)$idPostWp,
'title' => $modificarPostWpDecode['title'], //ok edit
'description' => $modificarPostWpDecode['content'], //ok edit
'categories' => $modificarPostWpDecode['category'], //ok edit
'custom_fields' => array(
array('key' => 'precio', 'value' => $modificarPostWpDecodeCustom['price']), // no edit, fields will be create again
array('key' => 'category', 'value' => $modificarPostWpDecodeCustom['category']), // no edit, fields will be create again
array('key' => 'estrenar', 'value' => $modificarPostWpDecodeCustom['new']), // no edit, fields will be create again
array('key' => 'currency', 'value' => $modificarPostWpDecodeCustom['currency']), // no edit, fields will be create again
array('key' => 'search', 'value' => $modificarPostWpDecodeCustom['search']) // no edit, fields will be create again
)
);
My call to wordpress is:
$params = array(1, WPUSER, WPPASS, (int)$idPostWp, $modificarPostWpDecode);
$request = xmlrpc_encode_request('wp.editPost', $params, array('encoding' => 'UTF-8', 'escaping' => 'markup'));
Thank a lot!
As noted here, you must pass the custom field's ID to edit the field, rather than the key, which would end up creating a duplicate.
So you need to do two requests, unless you already know the custom field IDs. One request to get all the custom data, loop through the fields, and collect the appropriate IDs to the fields you want to update. The second request will update the fields, specified using the field IDs, not just the key.
The collection of IDs can look similar to the following
$custom_fields_to_edit = array(
'key1' => null,
'key2' => null
);
foreach($post->custom_fields as $custom){
if (array_key_exists($custom->key, $custom_fields_to_edit)){
$custom_fields_to_edit[$custom->key] = $custom->id;
}
}
where $post has been collected using the wp.getPost procedure.
You can then proceed as previously, with the following modification to your code.
'custom_fields' => array(
array('id' => $custom_fields_to_edit['key1'], 'key' => 'key1', 'value' => $modificarPostWpDecodeCustom['key1']),
array('id' => $custom_fields_to_edit['key2'], 'key' => 'key2', 'value' => $modificarPostWpDecodeCustom['key2'])
)

PHP: Sort array according to another array

I have a problem in php, i tried searching but i only got more confused. I need to sort an array according to another array using the "priority" key value.
This is the order i need it filtered with.
$custom_order = array(
array('priority' => 0, 'field_id' => 'password'),
array('priority' => 1, 'field_id' => 'username')
);
And this is the array that needs to be filtered
$default_order = array(
'name' => array(
'username' => array(
'type' => 'text',
'priority' => 0
),
'password' => array(
'type' => 'password',
'priority' => 1
),
),
);
And this is the order that i would like to get
$final_order = array(
'name' => array(
'password' => array(
'type' => 'password',
'priority' => 0
),
'username' => array(
'type' => 'text',
'priority' => 1
),
),
);
I'm confused, i'm not sure whether i should use uasort, or array_intersect, by reading other articles about it i got more confused. Can anybody please explain how would i go to sort the array in this way?
Thank you so much.
If priority is the actual sort criteria, then usort would be an easy way:
usort( $array['name'], function($a, $b) {
return ($a->priority < $b->priority) ? -1 : 1;
});
This just compares the value of priority between elements of the array. If not, perhaps you could enhance your example to show how the sort should actually be determined.
EDIT: Based on the clarified question.
Because of the structure of your arrays, I don't believe that you will be able to just use PHP functions.
First, arrange the priorities in a form better for prioritzing:
$priorities = array();
foreach ($custom_order as $item) {
$priorities[$item->field_id] = $item->priority;
}
Then assign updated priorities to the $default items:
$final_order = array();
foreach ($default_order as $item) {
$item->priority = $priorities[$item->type];
}
Then do the sort originally suggested to get them in priority order:
usort( $final_order['name'], function($a, $b) {
return ($a->priority < $b->priority) ? -1 : 1;
});

CakePHP getting data form an ID which is inside another table?

Right, so my data returns in the following way,
(int) 0 => array(
'MODEL-XX' => array(
//DATA HERE
'xxs_id' => '11',
'aas_id' => '44',
'vvs_id' => '2'
),
'xxs' => array(
'id' => '11',
'customername' => 'XX name here'
),
'aas' => array(
'id' => '44',
'clientname' => 'aa name here',
'xxs_id' => '11'
),
'vvs' => array(
'id' => '2',
'start' => '1405296000',
'end' => '1405814400',
'users_id' => '1'
)
This works fine, but I want to know how to link my users table to this model. So the details of each user for my VV model would become apart of the data. My MODEL-XX does not have any links with my users table so the place I need to call in the users details are held with my VV model.
I have been looking into this but have not been able to find a simple easy method for doing this?
I was thinking that this would be doable with my model, so I opened my my XX model and added the following within my '$belongsTo' section,
'Users' => array(
'className' => 'Users',
'foreignKey' => 'vvs.users_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
So is there a easy method for linking data like this?
Please give me time, if I have not explained myself right or not enough data, please tell me and let me fix or explain better.
Thanks,
Either set your recusive higher:
$this->MODEL-XX->recursive = 1; //or 2
Or and this should be your prefered way to go, start using the containable behaviour:
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
In your appModel:
public $actsAs = array('Containable');
Then try this find:
$this->MODEL-XX->recursive = -1;
$data = $this-MODEL-XX>find(
'all', array(
'conditions' => $conditions,
'contain' => array('xxs', 'aas', 'vvs', 'user')
)
);
I might be 'vvs.user' but I forgot what is used for deeper models

Categories