Match array elements beginning with letters specified in another array in PHP - php

I have the following array:
Array
(
[0] => BCD
[1] => ACE
[2] => AHP
[3] => BGH
[4] => ART
[5] => COT
[6] => ARG
[7] => BGT
)
I need to match all elements whose first letter is in the following array:
Array
(
[0] => B
[1] => A
)
to get:
Array
(
[0] => ACE
[1] => AHP
[2] => BGH
[3] => ART
[4] => ARG
[5] => BGT
)
Short of looping through the whole array, how do I do this in PHP? Is there a built-in PHP array function for this or a combination of so? The order does not matter for both keys and values of the resulting array. Thanks much.

You can use array_filter for these operations:
$array = array('CBD', 'NHN', 'NHP', 'WHC', 'NND', 'CQN', 'WST', 'WVT');
$whitelist = array('W', 'N');
$filtered = array_filter($array, function($val) use ($whitelist) {
// check if first letter is in the whitelist array
if (in_array($val{0}, $whitelist)) {
return $val;
}
return false;
});
Output:
Array
(
[1] => NHN
[2] => NHP
[3] => WHC
[4] => NND
[6] => WST
[7] => WVT
)

Related

Sort a subArray by based on order of Array in php?

I have a big array as $orderArr:
$orderArr = array("IZQ", "AOH", "VNP", "ICW", "IOQ", "BXP", "SHH", "EAY", "ZAF", "CUW");
which looks like
Array ( [0] => IZQ [1] => AOH [2] => VNP [3] => ICW [4] => IOQ [5] => BXP [6] => SHH [7] => EAY [8] => ZAF [9] => CUW )
and I have two small arrays as $subArr1 and $subArr2:
$subArr1 = array("VNP", "BXP", "ICW", "IZQ");
$subArr2 = array("ZAF", "IZQ", "AOH");
looks like
Array ( [0] => VNP [1] => BXP [2] => ICW [3] => IZQ )
Array ( [0] => ZAF [1] => IZQ [2] => AOH )
Both small arrays (sub array) own elements belong to the big array.
I want to sort two small arrays according to the order of big array, as following:
Array ( [0] => IZQ [1] => VNP [2] => ICW [3] => BXP )
Array ( [0] => IZQ [1] => AOH [2] => ZAF )
I am looking for the simplest codes to do it in php. Any suggestions are welcome.
Probably the simplest would be to compute the intersection and it will return in the order of the first array:
$subArr1 = array_intersect($orderArr, $subArr1);
That will return with the keys of the first array; if you want to reindex instead:
$subArr1 = array_values(array_intersect($orderArr, $subArr1));
You could use usort to sort based on array position:
usort($subArr1, function ($a, $b) use ($orderArr) {
return (array_search($a, $orderArr) < array_search($b, $orderArr)) ? -1 : 1;
});
var_dump($subArr1);

Removing null or blank values of array

I have an array, I just print it as-
print_r($data);
It shows output as-
Array
(
[0] => Array
(
[0] => Title
[1] => Featured Image
[2] => Catagories
[3] => Tags
[4] => Content
)
[1] => Array
(
[0] => title 1
[1] => img1.jpg
[2] => cat 1
[3] => tag 1
[4] => post 1 content
)
[2] => Array
(
[0] => title 2
[1] => img2.jpg
[2] => cat2
[3] => tag 2
[4] => post 2 content
)
[3] => Array
(
[0] => title 3
[1] => img3.jpg
[2] => cat3
[3] => tag3
[4] => post 3 content
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[6] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
)
I just want to remove blank or null values from array.
I tried array_dif() and array_filter() but still I couldn't remove null values.
How is this possible?
Thanks.
You can loop through the array, look for empty variables and use unset to remove them.
This code will loop through and check if the length of the first value in each array is at least one character long and unset it if its not.
<?php
foreach($data as $key => $value) {
if(!isset($value[0][0]))
unset($data[$key]);
}
This code will loop through the array in a similar way, except to check every value of every array to determine if its parrent array should be kept or left to be unset.
<?php
foreach($data as $key => $values) {
foreach($values as $value) {
if(isset($value[0]))
continue 2;
}
unset($data[$key]);
}
You should use array_filter with a function that also runs array_filters against the subarrays and returns false if the filtered subarrays become empty.
<?php
$array = Array(
Array(1, 2, 3),
Array(null, null, null),
Array(false, false, false),
Array(3, 2, 1)
);
$filtered = array_filter($array, function($elem) {
return count(array_filter($elem));
});
print_r($filtered);
?>

Build array by string name OR make multiple array by string values

I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it

PHP three dimensional array, reset key if second element different

Let say i have this print_r output, this is dynamic and not same each condition
Array
(
[2] => Array
(
[1] => 24
[2] => 23,25
)
[3] => Array
(
[3] => 27
[4] => 27,26
[5] => 28,27,26
)
)
As you can see, array element [3] starts from [3][4][5], how do it make it start from [1][2]...[n] if the 2nd element is not same.
Ideally what i am looking for is something like
Array
(
[2] => Array
(
[1] => 24
[2] => 23,25
)
[3] => Array
(
[1] => 27
[2] => 27,26
[3] => 28,27,26
)
)
How do i achieve that? Thanks
array_values returns the values of an array with new numeric indices:
foreach($a as $k => $v) {
$a[$k] = array_values($v);
}
Add conditions if you only want to re-index some of your sub-arrays.
Functional approach:
$a = array_map(function($v) {
return array_values($v);
}, $a);

Convert Array to Multidimensional Array

I like to convert a single array into a multidimensional array. This is what I get have web scraping a page, except it is not the end result that I am looking for.
Change:
Rooms: Array (
[0] => name
[1] => value
[2] => size
[3] =>
[4] => name
[5] => value
[6] => size
[7] =>
[8] => name
[9] => value
[10] => size
[11] =>
[12] => name
[13] => value
[14] => size
[15] =>
)
Into:
Rooms: Array (
Room: Array (
[0] => name
[1] => value
[2] => size
),
Room: Array (
[0] => name
[1] => value
[2] => size
),
Room: Array (
[0] => name
[1] => value
[2] => size
)
)
First use array_filter() to get rid of the nodes:
$array = array_filter($array, function($x) { return trim($x) != ' '; });
// Or if your PHP is older than 5.3
$array = array_filter($array, create_function('$x', 'return trim($x) != " ";'));
Then use array_chunk() to split the array into chunks of 3:
$array = array_chunk($array, 3);
This of course assumes you will always and only get tuples containing name, value and size, in that order.

Categories