I have and array with paths to files, but I need to filter out certain folders.
This is just an example but I want to remove files that are in this case in folders like 'thumbs', 'database' & 'test'.
I could not find an array filter for this.
[
[0] => uploads/projects/pathtofile.jpg,
[1] => uploads/projects/thumbs/pathtofile.jpg,
[3] => uploads/database/projects/pathtofile.jpg,
[4] => uploads/projects/thumbs/database/pathtofile.jpg,
[5] => uploads/thumbs/projects/test/pathtofile.jpg
]
You can use array_interset()
foreach($array as $key=> $ar){
if(count(array_intersect(explode('/',$ar),['thumbs', 'database','test']))>0){
unset($array[$key]);
}
}
Output:- https://eval.in/939916
I'd go with array_filter() and preg_match():
$filtered = array_filter($a, function($i) {
return preg_match('/(thumbs|database|test)/', $i) !== 1;
});
If your file path starts with / then this would work fine:
$blacklist = ['/thumb/', '/image/'];
$files = ['/dir/thumb/image.jpg', '/dir/image/thumb.jpg', '/dir/subdir/file.php'];
$filtered_files = array_filter($files, function($value) use($blacklist){
foreach($blacklist as $blk){
if(strpos($value, $blk) !== false){
return false;
}
}
return true;
});
print_r($filtered_files);
Output :
Array
(
[2] => /dir/subdir/file.php
)
What about using array_filter callable?
<?php
$a = array(
0 => 'uploads/projects/pathtofile.jpg',
1 => 'uploads/projects/thumbs/pathtofile.jpg',
3 => 'uploads/database/projects/pathtofile.jpg',
4 => 'uploads/projects/thumbs/database/pathtofile.jpg',
5 => 'uploads/thumbs/projects/test/pathtofile.jpg',
6 => 'uploads/thumb/projects/pathtofile.jpg'
);
$b = array_values(array_filter($a, function ($item) {
return !preg_match('/(\/thumbs\/|\/database\/|\/test\/)/', $item);
}));
echo '<pre>' . print_r($b, 1) . '</pre>';
Output:
Array
(
[0] => uploads/projects/pathtofile.jpg
[1] => uploads/thumb/projects/pathtofile.jpg
)
Related
I have array with the structure as below.
Array
(
[example1] => Array
(
[0] => 'banana'
)
[example2] => Array
(
[0] => 'orange'
[1] => 'apple'
[2] => 'plum'
[3] => 'watermelon'
[4] => 'pumpkin'
)
[example3] => Array
(
[0] => 'cherry'
[1] => 'strawberry'
)
)
I am trying to display the key name for a sample value.
Eg.
looking for a value: 'apple' - result: 'example2'
Simply loop your arrays and check if element exists:
function search(string $search, array $source) {
foreach ($source as $key => $sub) {
if (in_array($search, $sub)) {
return $key;
}
}
}
search('apple', $source);
Just iterate thru all the subarrays and return the parent key.
$array = [
'example1' => ['banana'],
'example2' => ['orange', 'apple', 'plum', 'watermelon', 'pumpkin'],
'example3' => ['cherry', 'strawberry']
];
$needle = 'apple';
$result = '';
foreach($array as $parentKey => $child) {
if(in_array($needle, $child)) {
$result = $parentKey;
break;
}
}
echo $result;
example2
that's a good example too, thanks.
I built my function on this basis
function CheckArray($my_array,$search) {
$result = array_keys(array_filter($my_array, function ($arr) use ($search) {
return in_array($search, $arr);
}));
if(isset($result[0])){
return $result[0];
}else{
return "not find";
}
}
I have an array like this
array(
[0] => 'sku_name'
[1] => 'price'
[2] => 'typesku_'
)
i want to detect which index that contain 'sku_' at the exact first 4 characters in string and remove the 'sku_'
This should do the trick:
$yourArray = array(
'sku_name',
'price',
'typesku_'
);
$replaced = array_map(
function($val)
{
if (stripos($val, "sku_") === 0)
{
return substr($val, 4);
}
return $val;
},
$yourArray
);
print_r($replaced);
Output:
Array (
[0] => name
[1] => price
[2] => typesku_
)
You will get the answer with stripos
$demo=array('sku_name','price','typesku_');
foreach($demo as $k=>$d){
if(stripos($d, 'sku_')===0){
$demo[$k]= str_replace('sku_',"",$d);
}
}
Output
Array
(
[0] => name
[1] => price
[2] => typesku_
)
<?php
$arr=array(0 => 'sku_name', 1 => 'price', 2 => 'typesku_');
$i=0;
$s=0;
foreach($arr as $index){
if(stripos($index,"sku_")===0){
$str=strlen($index);
for ($i=4;$i<$str;$i++){
$index[$s]=$index[$i];
$index[$i]="\0";
$s++;
}
echo $index;
echo "<br>";
}
}
?>
This is one of the ways you can do it, I just thought of it myself, it may not be the best way, but it works, cheers! :) Hope it helps.
this is the dynamic array. I'm trying to remove the repeated value and have only merged one. e.g. need to remove [0] => 1/2/48 & [1] => 1/2/48/56 and only have [2] => 1/2/48/56/58 .
Array(
[0] => 1/2/48
[1] => 1/2/48/56
[2] => 1/2/48/56/58
[3] => 1/2/245
[4] => 1/2/245/246
[5] => 1/2/265
)
So the array should look like
Array(
[0] => 1/2/48/56/58
[1] => 1/2/245/246
[2] => 1/2/265
)
One way to do it
$a = [
'1/2/48',
'1/2/48/56',
'1/2/48/56/58',
'1/2/245',
'1/2/245/246',
'1/2/265'
];
$result = array_values(
array_filter($a, function ($value, $index) use ($a) {
return !isset($a[$index + 1]) ||
strpos($a[$index + 1], $value) !== 0;
}, ARRAY_FILTER_USE_BOTH)
);
This assumes that your input array $a is sorted as you showed. Otherwise you'd need to sort it first.
if your php-version lt 5.6.0, and you haven't sort array like under
$a = [
'1/2/245/246',
'1/2/48',
'1/2/48/56',
'1/2/245',
'1/2/265',
'1/2/48/56/58'
];
you can try like this
$result = array_values(
array_filter($a, function ($value) use ($a) {
$is_exists = false;
foreach ($a as $v){
if (($v !== $value) && (strpos($v, $value) !== FALSE)){
$is_exists = true;
break;
}
}
return !$is_exists;
})
);
I come up with this code:
function multiArrayFlip($array)
{
$arrayCount = count($array);
if ($arrayCount != count($array, COUNT_RECURSIVE))
{
foreach($array as $key => $value)
{
if (is_array($value))
{
$array[$key] = multiArrayFlip($value);
}
}
}
else
{
array_flip($array);
}
return $array;
}
but it doesnt work.
It returns unchanged array.
here is the array data sample:
Array
(
[0] => Array
(
[0] => Array
(
[zip] => 02135
[hispanic_percent] => 7.4
[white_percent] => 73.1
[black_percent] => 4.2
[native_american_percent] => 0
)
)
[1] => Array
(
[0] => Array
(
[zip] => 02135
[school_number] => 1
[school_name] => ANOTHER COURSE TO COLLEGE
[school_address] => 20 WARREN STREET BRIGHTON MA 02135
[contact_number] => 617-635-8865
[start_grade] => 9TH GRADE
[reduced_lunch_students_count] => 8
[reduced_lunch_students_percent] => 120
[free_or_reduced_lunch_students_count] => 53
[free_or_reduced_lunch_students_percent] => 0
)
)
)
You have to reassign the return value of the array_flip function to your $array variable in order to work.
You need to modify your function to work it correctly. Reassign the values after array_flip
function multiArrayFlip($array)
{
$arrayCount = count($array);
if ($arrayCount != count($array, COUNT_RECURSIVE))
{
foreach($array as $key => $value)
{
if (is_array($value))
{
$array[$key] = multiArrayFlip($value);
}
}
}
else
{
$array = array_flip($array);
}
return $array;
}
Hope this helps :)
I have this array:
Array
(
[0] => CMS_ADMIN
[1] => CMS_ACADEMIC
[2] => CMS_FINANCE
[3] => HRM_HR
[4] => SRM_D
[5] => SRM_CNC
[6] => SRM_TM
[7] => SRM_DE
)
I would like to get the array by searching to the array value using the word provided. Let say I'm just provide the 'CMS' word, so how i'm gonna get the [0] => CMS_ADMIN, [1] =>CMS_ACADEMIC, [2] => CMS_FINANCE assign to the new array. Please help....
With a function that looks like this:
function array_filter_prefix($array, $prefix) {
$result = array();
foreach ($array as $value) {
if (strpos($value, $prefix) === 0) {
$result[] = $value;
}
}
return $result;
}
Given an input array, $test, you can then do this to get the result:
$result = array_filter_prefix($test, 'CMS');
print_r($result);
Codepad here.
batter style in PHP 5.3
$prefix = "CMS";
$new = array_filter($array, function ($str) use ($prefix) {
return (strpos($str, $prefix) === 0);
});