Get array that contain a value provided in PHP - php

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);
});

Related

Filter certain folders from array

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
)

How to convert two dimensional array to one dimensional array in php?

I am Having an mutidimensional array getting result like given below
Array
(
[0] => Array
(
[0] => 70
)
[1] => Array
(
[0] => 67
)
[2] => Array
(
[0] => 75
[1] => 73
[2] => 68
)
[3] => Array
(
[0] => 68
)
[4] => Array
(
[0] => 76
)
)
But I need to convert it to single array
And I want to convert in to single dimensional array as
Array
(
[0] => 70
[1] => 67
[2] => 75
[3] => 73
[4] => 68
[5] => 68
[6] => 76
)
How to convert it using php functions?
Or Is there any other way to do it?
You can try
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$l = iterator_to_array($it, false);
var_dump($l); // one Dimensional
Try with:
$input = array(/* your array*/);
$output = array();
foreach ( $input as $data ) {
$output = array_merge($output, $data);
}
You can use array_walk_recursive() for that coupled with a closure:
$res = array(); // initialize result
// apply closure to all items in $data
array_walk_recursive($data, function($item) use (&$res) {
// flatten the array
$res[] = $item;
});
print_r($res); // print one-dimensional array
This should do the trick
$final = array();
foreach ($outer as $inner) {
$final = array_merge($final, $inner);
}
var_dump($final);
Or you could use array_reduce() if you have PHP >= 5.3
$final = array_reduce($outer, function($_, $inner){
return $_ = array_merge((array)$_, $inner);
});
var_dump($final);
For a more generic function which can deal with multidimensional arrays, check this function,
function arrayFlattener($input = array(), &$output = array()) {
foreach($input as $value) {
if(is_array($value)) {
arrayFlattener($value, $output);
} else {
$output[] = $value;
}
}
}
You can find an example here.
By using this function you can convert any dimension array into a single dimention array.
$result = array();
$data = mearchIntoarray($result,$array);
function mearchIntoarray($result,$now)
{
global $result;
foreach($now as $key=>$val)
{
if(is_array($val))
{
mearchIntoarray($result,$val);
}
else
{
$result[] = $val;
}
}
return $result;
}
Where $array is your given array value.

Find in array and get count of each string

I am fetching some data from the db and then push them to an array. I need to find the count of some strings and print out the result (count) in an efficient way:
Array
(
[0] => q1-1,q2-2,q3-2,q4-1,q5-2,q6-3,q7-1,q8-4,
[1] => q1-1,q2-2,q3-1,q4-3,q5-3,q6-3,q7-2,q8-1,
[2] => q1-1,q2-1,q3-1,q4-1,q5-1,q6-2,q7-2,q8-2,
[3] => q1-3,q2-1,q3-1,q4-1,q5-2,q6-3,q7-1,q8-1,
[4] => q1-2,q2-2,q3-3,q4-1,q5-3,q6-3,q7-1,q8-1,
[5] => q1-1,q2-2,q3-3,q4-1,q5-2,q6-3,q7-1,q8-1,
[6] => q1-3,q2-1,q3-1,q4-3,q5-2,q6-3,q7-2,q8-4,
[7] => q1-2,q2-2,q3-3,q4-1,q5-2,q6-5,q7-1,q8-1,
[8] => q1-1,q2-1,q3-2,q4-3,q5-3,q6-5,q7-1,q8-1,
[9] => q1-2,q2-1,q3-1,q4-1,q5-3,q6-3,q7-1,q8-1,
[10] => q1-3,q2-2,q3-3,q4-3,q5-4,q6-3,q7-1,q8-1,
...
)
Sample data is above.
I need to know how many occurences of q1-1, q1-2 ... q8-4 is in the array and print out readable version. Ex. The are 23: q1-1, 412: q1-2 and so on.
I was going to create an array of each string that needs to be searched that iterate through the array. For every result increment the resultVariable for that string but I'm not sure if that's the best way.
Suggestions?
Pretty simple, loop on your array, create sub arrays, and create a counter array:
$counts = array () ;
foreach ( $your_array as $row ) {
$sub = explode(',', $row);
foreach ( $sub as $subval ) {
if ( array_key_exists ( $subval, $counts ) ) {
$counts[$subval] ++ ;
} else {
$counts[$subval] = 1 ;
}
}
}
Here is $counts:
Array (
'q1-1' => 23,
'q1-2' => 9,
// and so on....
);
Try:
$arr = array(...); //your array
$count = array();
foreach($arr as $v) {
$substr = explode(',', $v);
foreach($substr as $m) {
if(strstr($v, $m) !== FALSE)
$count[$m]++;
}
}
Printing the counts,
foreach($count as $k => $v)
echo "Count for '$k': ". $v;

need to search for values in each string of an array to construct another array

I have this array:
Array
(
[count] => 12
[6] => CN=G_Information_Services,CN=Users,DC=hccc,DC=campus
[7] => CN=WEBadmin,CN=Users,DC=hccc,DC=campus
[9] => CN=G_ISDept,CN=Users,DC=hccc,DC=campus
[10] => CN=STAFF,CN=Users,DC=hccc,DC=campus
)
and I want to create an array of values that consist of the value between the first CN= and , of each array value below.
I probably will have to loop thru the array above, do a regex search for the first occurrence of cn and the value that follows it
I am not sure what I am doing wrong.
I need the final result to be an array that resembles this:
array('G_Information_Services', 'WEBadmin', 'G_ISDept', 'STAFF');
Use preg_match on each of the array values to get only the first corresponding CN value.
$found = array();
foreach ($arr AS $values) {
if (preg_match('/CN=([^,]+),/',$values,$matches))
$found[] = $matches[1];
}
Output
Array
(
[0] => G_Information_Services
[1] => WEBadmin
[2] => G_ISDept
[3] => STAFF
)
Try this (not the most efficient way but it should work):
foreach ($array as $key => $value)
{
if (is_numeric($key))
{
$array[$key] = explode(',', $array[$key]);
$array[$key] = $array[$key][0];
$array[$key] = substr($array[$key], 3);
}
}
This gets the first value of CN= of each element of the array, it also ignores any DC= values.
$arr = array(
'count' => 12,
6 => 'CN=G_Information_Services,CN=Users,DC=hccc,DC=campus',
7 => 'CN=WEBadmin,CN=Users,DC=hccc,DC=campus',
9 => 'CN=G_ISDept,CN=Users,DC=hccc,DC=campus',
10 => 'CN=STAFF,CN=Users,DC=hccc,DC=campus'
);
$newArr = array();
foreach($arr as $key => $value)
{
if($key != 'count')
{
$temp = explode(',', $value);
foreach($temp as $item)
{
if(strpos($item, 'CN=') === 0)
{
$item = substr($item, 3 );
$newArr[] = $item;
break 1;
}
}
}
}
print_r($newArr);

PHP to find specific thing in an array

This is my array
Array (
[0] => "8266_hal_url"
[1] => "8266_hal_picture"
[2] => "8266_hal_status"
[3] => "8266_hal_qwert"
[4] => "4224423_hal_status"
[5] => "4223_hal_status"
)
How do I find all hal_status in an array and pass it to a query?
PHP >= 5.3.0
$testData = array ( '8266_hal_url',
'8266_hal_picture',
'8266_hal_status',
'8266_hal_qwert',
'4224423_hal_status',
'4223_hal_status',
);
$testNeedle = 'hal_status';
$result = array_filter($testData, function($arrayEntry) use ($testNeedle) {
return (strpos($arrayEntry,$testNeedle) !== false);
});
var_dump($result);
$result=array();
foreach ($myarray as $element)
if (strpos($element,'hal_status')===false) continue;
else $result[]=$element;
print_r($result);
Try this:
$result = array();
foreach($array as $item)
{
if(strpos($item, 'hal_status') !== false)
{
$result[] = $item;
}
}
print_r($result);

Categories