PHP Santizing/Validating Array of Integers - php

I have the following array and would like to know what the best way would be of validating and santizing this array to make sure only integers are allowed?
if(is_array($_POST['taxonomy'])) {
$term_ids = array_map('esc_attr', $_POST['taxonomy']);
}
Which looks like this when printed:
Array
(
[0] => 13
[1] => 12
)
I know the esc_attr isn't very secure so would like something a bit more beefed up.
Any help would be great.
Cheers,
Dave

Since it's $_POST data, you'll want to check for ctype_digit (i.e. a string containing only digits):
$sanitizedValues = array_filter($_POST['taxonomy'], 'ctype_digit');
Note that this simply discards non-numeric values.

An alternative would be using phps filter functions:
$array = array(
13, 12, '1', 'a'
);
$result = filter_var($array, FILTER_VALIDATE_INT, array(
'flags' => FILTER_REQUIRE_ARRAY,
'options' => array('min_range' => 1)
));
var_dump($result);
/*
array(4) {
[0]=>
int(13)
[1]=>
int(12)
[2]=>
int(1)
[3]=>
bool(false)
}
*/

if(is_array($_POST['taxonomy'])) {
$term_ids = array_map('intval', $_POST['taxonomy']);
}
Should do the trick. NOTE: this is sanitation. More: http://php.net/manual/en/function.intval.php

foreach( $array as $key => $value) {
$array[$key] = (int) $value;
if( $array[$key] != $value ) {
// error
}
}

If you are looking for one-liner to check against specified condition you can use:
$onlyIntegers = Arr::check($_POST['taxonomy'], 'ctype_digit');
assuming your $_POST['taxonomy'] can contain numeric strings as #deceze suggested, or just plain:
$onlyIntegers = Arr::check($_POST['taxonomy'], 'is_int');
if you are sure that $_POST['taxonomy'] values should be in fact integers.
Arr::check method is a part of this php array library which contains various methods to help you deal with different types of arrays.

Related

Search for array row with specific column value and return another value from qualifying row

I have a multidimensional array that I am trying to search for a specific value (url) and then retrieve an another value in the same row (value). I also need to divert to an else if it is not found.
array(2) {
[0]=> array(2) {
["url"]=> string(7) "fareham"
["value"]=> string(7) "Fareham"
}
[1]=> array(2) {
["url"]=> string(11) "southampton"
["value"]=> string(11) "Southampton"
}
}
I have been experimenting with array_key_exists() and isset() to check it's set and just coming up null!
For example, if I search for fareham, I want to return Fareham. If I search for nottingham, I expect null.
How can I isolate the qualifying value?
Use array_column() to index the data by the url columns and then use isset() to check if the value is there...
$data = array_column($data, "value", "url");
$search = 'southampton';
$value = isset($data[$search])?$data[$search]:"not found";
echo $value;
or for PHP 7+, you can use the null coalescing operator (??)
$value = $data[$search]??"not found";
Here is the minimal way to do it (no checks)
$a = array (
0 =>
array (
"url" => 'fareham',
"value" => 'Fareham'
),
1 =>
array (
"url" => 'southampton',
"value" => 'Southampton'
)
);
$u = 'fareham';
$i = $a[false===($f=array_search($u,array_column($a,'url')))?-1:$f]['value'];
print_r($i);
Output
Fareham
Sandbox
How it works
First we create an array we can search by using array_column on the column we want to search in this case url. It looks like this [0=>'fareham', 1=>'southampton']
Then we use the normal array_search which returns an index, if you notice above the indexes are correlated to the original array. Which means we can put that in as the top level key, then it's a simple matter of adding the key we want.
Because array_search can return a boolean(false) which PHP sees as 0 or the first index I put a little hack in for that. But a better way is to check it like this:
$a = array (
0 =>
array (
"url" => 'fareham',
"value" => 'Fareham',
"extra" => 'Foo'
),
1 =>
array (
"url" => 'southampton',
"value" => 'Southampton',
"extra" => 'Bar'
)
);
function serchMultiDimensionalValue($needle, $haystack, $fields='value'){
if(false === ($f=array_search($needle,array_column($haystack,'url')))) return false; //or return [];
if(!is_array($fields)) $fields = [$fields];
return array_intersect_key($haystack[$f], array_flip($fields));
}
var_dump(serchMultiDimensionalValue('foo',$a));
var_dump(serchMultiDimensionalValue('fareham',$a));
var_dump(serchMultiDimensionalValue('fareham',$a, 'extra'));
var_dump(serchMultiDimensionalValue('fareham',$a, ['extra','url']));
Ouput
bool(false)
array(1) {
["value"]=>
string(7) "Fareham"
}
array(1) {
["extra"]=>
string(3) "Foo"
}
array(2) {
["url"]=>
string(7) "fareham"
["extra"]=>
string(3) "Foo"
}
Sandbox
I added a bit more "functionality" to it, hope you don't mind.
While it might be alluring to seek out a one-liner like:
array_column($array, $returnColumn, $haystackColumn)[$needle] ?? null
Be aware that array_column() will be unconditionally doubling the memory usage (because it is generating a new array with the same length as the original) for the sake of assigning new first level keys to search by.
Metaphorically speaking, it is like building a new registry of residences in a neighborhood including every homeowner's name and their address. Yes, you can find out who lives at a given home without knocking on the front door, but you had to knock on every door to collect every home's details before enjoying this convenience. Why not just knock on the door of each original house and ask if they are who you are looking for? It is much easier, and you will be able to "quit" early as soon as you find the homeowner that you are looking for.
For best efficiency and for least memory usage, I recommend the below classic loop with conditional break. It is built with dynamic search and return variables for general usage, but you can use hardcoded values if you like.
Code: (Demo)
$array = [
["url" => 'fareham', "value" => 'Fareham'],
["url" => 'southampton', "value" => 'Southampton']
];
$needle = 'fareham';
$haystackColumn = 'url';
$returnColumn = 'value';
$value = null;
foreach ($array as $row) {
if ($row[$haystackColumn] === $needle) {
$value = $row[$returnColumn];
break;
}
}
var_export($value); // 'Fareham'

Sorting an array by the values of another array

I have a some messy data coming in from a feed, and am trying to figure out how to sort it correctly. I posted a simplified example below. I'd like to sort the people array alphabetically by the Group name.
$people = array(
"category_id_1" => array (
"Mark",
"Jenny",
"Andrew"
),
"category_id_2" => array (
"John",
"Lewis",
"Andrea"
),
"category_id_3" => array (
"Hannah",
"Angie",
"Raleigh"
)
);
$categories = array(
"category_id_1" => "Group B",
"category_id_2" => "Group C",
"category_id_3" => "Group A"
);
Ideally, the end result would be
$people = array(
"category_id_3" => array ( // Group A
"Hannah",
"Angie",
"Raleigh"
),
"category_id_1" => array ( // Group B
"Mark",
"Jenny",
"Andrew"
),
"category_id_2" => array ( // Group C
"John",
"Lewis",
"Andrea"
)
);
I've been spinning my wheels for a while now, and the closest I have gotten is this uasort, which still isn't doing the trick.
uasort($people, function ($a, $b) {
return strcmp($categories[$a], $categories[$b]);
});
Thanks so much for any help.
This can be achieved in a simpler way by taking advantage of array_replace:
// Work on a copy just to be sure the rest of your code is not affected
$temp_categories = $categories;
// Sort categories by name
asort($temp_categories);
// Replace the values of the sorted array with the ones in $people
$ordered_people = array_replace($temp_categories, $people);
You want to sort $people by its keys not its values. You can use uksort for this. Additionally you need to make $categories available in your function. I prefer use for that; but you could also make it a global variable. Final code:
uksort($people, function ($a,$b) use ($categories) {
return strcmp($categories[$a], $categories[$b]);
});
Manual for uksort
use language construct. Before example 3.
I think what you need is to Asort categories and the use that sorted array in a foreach.
Asort($categories);
Foreach($categories as $key => $group){
$new[$key] =$people[$key];
}
Var_dump($new);
https://3v4l.org/kDAQW
Output:
array(3) {
["category_id_3"]=> array(3) {
[0]=> "Hannah"
[1]=> "Angie"
[2]=> "Raleigh"
}
["category_id_1"]=> array(3) {
[0]=> "Mark"
[1]=> "Jenny"
[2]=> "Andrew"
}
["category_id_2"]=>array(3) {
[0]=> "John"
[1]=> "Lewis"
[2]=> "Andrea"
}
}
Try this(tested and working):
asort($categories);
$sorted = array();
foreach ($categories as $key => $value)
$sorted[$key]=$people[$key];
A better shorter approach:(tested and working)
asort($categories);
$result = array_merge($categories,$people);
The second method takes advantage of the fact that array_merge function replace the values in the first array with those in the second one when keys are the same.
warning : The second approach will not work if the keys are numbers. Use only string keys with it. Furthermore if the categories array has entries without corresponding entries in the people array they will be copied to the result
To solve this problem we use array_replace :
asort($categories);
$result = array_replace($categories,$people);
Var_dump($result);// tested and working

extract selected array values based on list php

I have a list of array keys in form of a string
"hasAddStrict,freqItems,freqAmount,freqUnit,freqFirstDayOfWeek"
Now, want to extract only those values, i.e $myarray['hasAddstrict'] should become $hasAddStrict, etc.
Is there a short way to do it? My rather dirty solution:
$ff = explode(',' ,"hasAddStrict,freqItems,freqAmount,freqUnit,freqFirstDayOfWeek");
foreach ( $ff as $key){ $ff[$key] = $SERVICE[$key] ; }
extract($ff);
I agree with Jeff that there isn't really a better way, and that keeping the variables as an array is probably a better idea, but you could wrap it up in a function for convenience (tested in php 5.6):
<?php
function array_extract(array $assoc_array, $keys_list)
{
return array_intersect_key($assoc_array, array_flip(explode(',', $keys_list)));
}
extract(array_extract($SERVICE, "hasAddStrict,freqItems,freqAmount,freqUnit,freqFirstDayOfWeek"));
When tested as follows:
$SERVICE = [
'hasAddStrict' => 123,
'freqItems' => 456,
'freqAmount' => 789,
'freqUnit' => 'abc',
'freqFirstDayOfWeek' => 'def',
];
extract(array_extract($SERVICE, "hasAddStrict,freqItems,freqAmount,freqUnit,freqFirstDayOfWeek"));
var_dump($hasAddStrict, $freqItems, $freqAmount, $freqUnit, $freqFirstDayOfWeek);
You get output:
int(123)
int(456)
int(789)
string(3) "abc"
string(3) "def"

Restructure multidimensional array of column data into multidimensional array of row data [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I have the following associative array of column data:
$where = array(
'id'=>array(
12,
13,
14
),
'date'=>array(
'1999-06-12',
'2000-03-21',
'2006-09-31'
)
);
I need to transpose / rotate the structure to be an array of rows (with merged column data assigned to their respective row). I don't need the column names in the result.
Expected output:
$comb = array(
array(12, '1999-06-12'),
array(13, '2000-03-21'),
array(14, '2006-09-31')
);
As Kris Roofe stated in his deleted answer, array_column is indeed a more elegant way. Just be sure to put it into some kind of a foreach loop, similar to what Sahil Gulati showed you. For example, like this:
$result = array();
foreach($where['id'] as $k => $v)
{
$result[] = array_column($where, $k);
}
The var_dump output of $result is exactly what you're looking for
array(3) {
[0]=>
array(2) {
[0]=>
int(12)
[1]=>
string(10) "1999-06-12"
}
[1]=>
array(2) {
[0]=>
int(13)
[1]=>
string(10) "2000-03-21"
}
[2]=>
array(2) {
[0]=>
int(14)
[1]=>
string(10) "2006-09-31"
}
}
Solution 1: Hope this simple foreach to get the desired result
Try this code snippet here
<?php
ini_set('display_errors', 1);
$where = array('id'=>array(12,13,14),'date'=>array('1999-06-12','2000-03-21','2006-09-31'));
$result=array();
foreach($where["id"] as $key => $value)
{
$result[]=array($value,$where["date"][$key]);
}
Solution 2: Here we are using array_walk to achieve the same result
Try this code snippet here
<?php
ini_set('display_errors', 1);
$result=array();
$where = array('id'=>array(12,13,14),'date'=>array('1999-06-12','2000-03-21','2006-09-31'));
array_walk($where["id"], function($value,$key) use(&$result,&$where){
$result[]=array($value,$where["date"][$key]);
});
print_r($result);
Solution 3: Here we are using array_shift on $where["date"].
Try this code snippet here
<?php
ini_set('display_errors', 1);
$result=array();
$where = array('id'=>array(12,13,14),'date'=>array('1999-06-12','2000-03-21','2006-09-31'));
foreach($where["id"] as $value)
{
$result[]=array($value, array_shift($where["date"]));
}
print_r($result);
I've completely re-written my answer because it was unnecessarily bloating this page. Truth is, there is a very clean and native way to handle this specific task of "transposing". Using null as the function argument and passing in the two known rows from the input array is all that is required.
Code: (Demo)
$where = [
'id' => [12, 13, 14],
'date' => ['1999-06-12', '2000-03-21', '2006-09-31']
];
var_export(
array_map(null, $where['id'], $where['date'])
);
Output:
array (
0 =>
array (
0 => 12,
1 => '1999-06-12',
),
1 =>
array (
0 => 13,
1 => '2000-03-21',
),
2 =>
array (
0 => 14,
1 => '2006-09-31',
),
)
For anyone that truly needs a dynamic solution (because the number of rows may fluxuate/change and you don't want to keep maintaining the processing code), then I recommend that you check the version history of my answer.

PHP Remove elements from associative array

I have an PHP array that looks something like this:
Index Key Value
[0] 1 Awaiting for Confirmation
[1] 2 Assigned
[2] 3 In Progress
[3] 4 Completed
[4] 5 Mark As Spam
When I var_dump the array values i get this:
array(5) { [0]=> array(2) { ["key"]=> string(1) "1" ["value"]=> string(25) "Awaiting for Confirmation" } [1]=> array(2) { ["key"]=> string(1) "2" ["value"]=> string(9) "Assigned" } [2]=> array(2) { ["key"]=> string(1) "3" ["value"]=> string(11) "In Progress" } [3]=> array(2) { ["key"]=> string(1) "4" ["value"]=> string(9) "Completed" } [4]=> array(2) { ["key"]=> string(1) "5" ["value"]=> string(12) "Mark As Spam" } }
I wanted to remove Completed and Mark As Spam. I know I can unset[$array[3],$array[4]), but the problem is that sometimes the index number can be different.
Is there a way to remove them by matching the value name instead of the key value?
Your array is quite strange : why not just use the key as index, and the value as... the value ?
Wouldn't it be a lot easier if your array was declared like this :
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
That would allow you to use your values of key as indexes to access the array...
And you'd be able to use functions to search on the values, such as array_search() :
$indexCompleted = array_search('Completed', $array);
unset($array[$indexCompleted]);
$indexSpam = array_search('Mark As Spam', $array);
unset($array[$indexSpam]);
var_dump($array);
Easier than with your array, no ?
Instead, with your array that looks like this :
$array = array(
array('key' => 1, 'value' => 'Awaiting for Confirmation'),
array('key' => 2, 'value' => 'Asssigned'),
array('key' => 3, 'value' => 'In Progress'),
array('key' => 4, 'value' => 'Completed'),
array('key' => 5, 'value' => 'Mark As Spam'),
);
You'll have to loop over all items, to analyse the value, and unset the right items :
foreach ($array as $index => $data) {
if ($data['value'] == 'Completed' || $data['value'] == 'Mark As Spam') {
unset($array[$index]);
}
}
var_dump($array);
Even if do-able, it's not that simple... and I insist : can you not change the format of your array, to work with a simpler key/value system ?
...
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
return array_values($array);
...
$key = array_search("Mark As Spam", $array);
unset($array[$key]);
For 2D arrays...
$remove = array("Mark As Spam", "Completed");
foreach($arrays as $array){
foreach($array as $key => $value){
if(in_array($value, $remove)) unset($array[$key]);
}
}
You can use this
unset($dataArray['key']);
Why do not use array_diff?
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
$to_delete = array('Completed', 'Mark As Spam');
$array = array_diff($array, $to_delete);
Just note that your array would be reindexed.
Try this:
$keys = array_keys($array, "Completed");
/edit
As mentioned by JohnP, this method only works for non-nested arrays.
I kinda disagree with the accepted answer. Sometimes an application architecture doesn't want you to mess with the array id, or makes it inconvenient. For instance, I use CakePHP quite a lot, and a database query returns the primary key as a value in each record, very similar to the above.
Assuming the array is not stupidly large, I would use array_filter. This will create a copy of the array, minus the records you want to remove, which you can assign back to the original array variable.
Although this may seem inefficient it's actually very much in vogue these days to have variables be immutable, and the fact that most php array functions return a new array rather than futzing with the original implies that PHP kinda wants you to do this too. And the more you work with arrays, and realize how difficult and annoying the unset() function is, this approach makes a lot of sense.
Anyway:
$my_array = array_filter($my_array,
function($el) {
return $el["value"]!="Completed" && $el!["value"]!="Marked as Spam";
});
You can use whatever inclusion logic (eg. your id field) in the embedded function that you want.
The way to do this to take your nested target array and copy it in single step to a non-nested array.
Delete the key(s) and then assign the final trimmed array to the nested node of the earlier array.
Here is a code to make it simple:
$temp_array = $list['resultset'][0];
unset($temp_array['badkey1']);
unset($temp_array['badkey2']);
$list['resultset'][0] = $temp_array;
for single array Item use reset($item)

Categories