Compare PHP array value to PART of a string - php

I'm trying to automate sifting through my online bank statements. Here's a trivial example of what I need.
I have an array of restaurants against which I sort my credit card statements:
$restaurants = array(
array("vendor" => "default",
"type" => "default"
),
array("vendor" => "dunkin",
"type" => "pastry"
),
array("vendor" => "mcdonald",
"type" => "fastfood"
),
array("vendor" => "olive",
"type" => "italian"
)
);
The statement entries themselves can be a rather descriptive string:
$string = "McDonald's Restaurants Incorporated";
I've tried using array_search and in_array, but they seem to do the reverse of what I need, or they need an exact match like in the example below, but it is not what I need:
$result = array_search($string, array_column($restaurants, 'vendor'));
return $restaurants[$result]['type'];
// returns "default" because "McDonald's Restaurants Incorporated" != "mcdonald"
I would like to be able to match the array value "mcdonald" to any string that contains that chunk of it, and then return type "fastfood" for it. Don't worry about handling multiple occurrences.

You'll need a combination of things - a search-in-string method, and for it to be case insensitive.
You can accomplish this with something like this:
/**
* Perform a string-in-string match case insensitively
* #param string $string
* #param array $restaurants
* #return string|false
*/
function findRoughly($string, $restaurants)
{
$out = false;
foreach ($restaurants as $restaurant) {
// Set up the default value
if ($restaurant['type'] == 'default' && !$out) {
$out = $restaurant['type'];
// Stop this repetition only
continue;
}
// Look for a match
if (stripos($string, $restaurant['vendor']) !== false) {
$out = $restaurant['type'];
// Match found, stop looking
break;
}
}
return $out;
}
And use it like so:
$result = findRoughly("McDonald's", $restaurants);
Example here.

I don't think there's a function in PHP that will handle this quite as cleanly as you want. But you can whip up a quick function to loop through the array looking for matches:
$type = call_user_func( function( $restaurants, $string ) {
foreach ( $restaurants as $restaurant ) {
if ( stripos( $string, $restaurant['vendor'] ) !== FALSE ) {
return $restaurant['type'];
}
}
return $restaurant[0]['type'];
}, $restaurants, $string );
If $string is "McDonald's Restaurants Incorporated", then $type will be "fastfood". The above makes the assumption that the first instance in the array is your default return if none of the specified values match.
I just built this as an anonymous function/closure out of convenience, which I usually would to do cleanly enclose something I only plan to run once. But it may be cleaner as a named function in your application.

I took a different (functional) approach by using array_map and array_filter. It's rather compact due to the use of builtin functions, and gets the job done.
// Anonymous function which gets passed to array_map as a callback
// Checks whether or not the vendor is in the $key_string (your restaurant)
$cmp = function ($array) use ($key_string) {
if (stristr($key_string, $array['vendor'])) {
return $array['type'];
}
return "default";
};
function validResult($item) {
return isset($item) && gettype($item)=="string";
}
$key_string = "McDonald's Restaurants Incorporated";
$results = array_map($cmp, $restaurants);
$results = array_pop(array_filter($results, validResult));

I got fixated on the in_array portion of the question. Editing this to use strpos instead.
Try this:
foreach($restaurants as $restaurant)
{
if(strpos($restaurant['vendor'], $string) !== FALSE)
{
return $restaurant['type']; //Or add to an array/do whatever you want with this value.
}
}
http://php.net/manual/en/function.strpos.php

Related

How do I match the value of an array to a key of another array in PHP?

I have 2 arrays, one of which is built dynamically and one which I use as a map.
The map array:
$social_links_map = [
'twitter' => 'twitter-icon',
'facebook' => 'facebook-icon'
];
And my dynamic array, which is just a simple list of social links:
$dynamic_social_links = [ 'twitter.com/me', 'facebook.com/me' ];
$social_links = explode( ',', $dynamic_social_links );
The $dynamic_social_links is an user input, as such, their input can be wrongly typed.
And I need to check if within my $social_links exists any of the $social_links_map keys and return the $social_links_map item accordingly:
if( !empty( $social_links ) ) {
foreach( $social_links_map as $handle => $icon ) {
foreach( $social_links as $social_link ) {
if( strpos( $social_link, $handle ) ) {
echo $handle;
}
}
}
}
This doesn't allow for "duplicate removal", nor does it look very pretty. Any ideas?
use
array_unique
to remove "duplicated data".
Try
$social_links = array_unique(explode( ',', $dynamic_social_links ));
Firstly, is it possible that you can allow the user to select from a list of your $social_links_map?
If not then the approach you are using is likely the simplest most readable way of doing this as there is no sure fire way to be able to match potentially random user input with a predefined array of options.
A couple of things to note:
As mentioned by le Mandarin, you can use array_unique to get rid of duplicates.
The other is that strpos will return 0 (which is falsey) if the search string (needle) is found at the very start of the context variable (haystack).
eg:
strpos('twitter.link/stuff', 'twitter');
will return 0 which is false Causing your if statement to fail.
instead try something like
if (false !== strpos('twitter.link/stuff', 'twitter')) {
Take note of the extra = in !==. This is necessary because you are saying "If the result is not exactly the boolean false.

Modifying an array sub-value that has been returned by a function

I needed a function that recursively parses a multi-dimensional array for a (part) of a certain string value and, if found, returns the entire value the string is contained in.
I came up with the following solution:
function & ransackArray(array & $haystack, $needle) {
foreach($haystack as &$element) {
if(is_array($element)){
if($v=ransackArray($element, $needle))
return $v;
} else {
if(strstr($element, $needle))
return $element;
}
}
return false;
}
This works fine. For instance, providing:
$data=array(
'key' => 'something',
'children' => array(
'someValue' => 'myTest',
'someMore' => 'yes'
)
);
And running:
$r=ransackArray($data, 'myTes');
This will result in $r containing 'myTest'.
The problem is that now that i found this value, i want to change it in $data, right on the fly, by writing:
$r='new value';
Which should then effectively result in data looking like this:
$data=array(
'key' => 'something',
'children' => array(
'someValue' => 'new value',
'someMore' => 'yes'
)
);
This however, doesn't seem to work. Perhaps i misunderstand something about references. In any case, the reason i needed this to work is why i pass $haystack as a reference and also return the function's result as one.
Can this be done? If yes, how? And if not - why not?
You're missing two ampersands...one on this line:
if($v = self::ransackArray($element, $needle))
which should be:
if($v = &self::ransackArray($element, $needle))
and one on this line:
$r = ransackArray($data, 'myTes');
which should be:
$r = &ransackArray($data, 'myTes');
(Note: it looks like your ransackArray function is actually a method in a class, so if you're calling that method from within the class it would be $r = &$this->ransackArray($data, 'myTes');)
When passing variables to a function, you don't need to use & - just put the & in front of the parameter in the function signature - but in cases like yours where you are getting a return value from a function, there needs to be a & both in the function call and in the function signature. For more info see http://www.php.net/manual/en/language.references.pass.php

$this->$array[$key] returning nothing when there is a value in place

I'm attempting to dynamically use a value in an array within an object.
In my particular case I have an array like this.
$this->customer = array(
[dealerId] => 4
[billFirstName] => Joe
[billLastName] => Blo
[billAddress1] => 1010s
[billAddress2] => 1020s
[billCity] => Anytown
[billState] => ST
[billCountry] => USA
[billPostalCode] => 11111
[dEmail] => emailaddress
[billPhone] => 8008008888
[password] => password
[distPrice] => 5
[distCost] => 20);
$result = $this->keyCheck('dealerId', 'customer');
The method I'm using:
protected function keyCheck($key, $array, $type = false)
{
if(array_key_exists($key, $this->$array) && $this->$array[$key]):
return $this->$array[$key];
else:
return $type;
endif;
}
The first check works (array_key_exists($key, $this->$array)). But the second check fails ($this->$array[$key]) even though there is a value held in that index of the array. I've proven that the array exists inside the keyCheck() method by using, print_r($this->$array); inside the method. And I know the value I'm looking for is available inside the method by using, print $this->$array['dealerId'];
Don't get hung up on the names, or the methodology I'm using, what I'm really interested in is finding out how to address a value held in an array that is dynamically addressed in this way.
It's probably so easy that I'll be slapping my head once it's revealed...
You are running into the dreaded treat string as an array trap, i.e.:
$str = 'customer'; echo $str[0]; // prints c
The $array[$key] portion of $this->$array[$key] is being interpreted as show me the nth index of string $array (if you pass customer it will try to access $this->c, which doesn't exist). Assign the array to a variable first. Here is an example:
class A
{
public $customer = array('dealerId'=>4, 'billFirstName'=>'Joe');
public function keyCheck($key, $arrayName, $type = false)
{
$array = $this->$arrayName;
if(array_key_exists($key, $array) && $array[$key]) {
return $array[$key];
} else {
return $type;
}
}
}
$a = new A();
echo $a->keyCheck('billFirstName', 'customer'); // Joe
Alternatively, you could use the complex syntax: $this->{$arrayName}[$key] as suggested in Artjom's answer.
PHP 5.4 addresses this gotcha:
[In PHP 5.4] Non-numeric string offsets - e.g. $a['foo'] where $a is a
string - now return false on isset() and true on empty(), and produce
a E_WARNING if you try to use them.
This E_WARNING should help developers track down the cause much more quickly.
Well u have to use
$this->{$array}[$key];

check is Array empty or not (php)

I am using Cakephp and try to customize the paginator result with some checkboxes .I was pass parameters in URL like this
"http://localhost/myproject2/browses/index/page:2/b.sonic:1/b.hayate:7/b.nouvo:2/b.others:-1/b.all:-2"
and cakephp translate URL to be the array like this
Array
(
[page] => 2
[b.sonic] => 1
[b.hayate] => 7
[b.nouvo] => 2
[b.others] => -1
[b.all] => -2
)
other time when I pass params without checking any checkbox.
"http://localhost/myproject2/browses/index/page:2"
and cakephp translate URL to be the array like this
Array
(
[page] => 2
)
how to simple check if [b.????] is available or not? I can't use !empty() function because array[page] is getting on the way.
If you want to check if a specific item is present, you can use either :
isset()
or array_key_exists()
But if you want to check if there is at least one item which has a key that starts with b. you will not have much choice : you'll have to loop over the array, testing each key.
A possible solution could look like this :
$array = array(
'page' => 'plop',
'b.test' => 150,
'b.glop' => 'huhu',
);
$found_item_b = false;
foreach (array_keys($array) as $key) {
if (strpos($key, 'b.') === 0) {
$found_item_b = true;
break;
}
}
if ($found_item_b) {
echo "there is at least one b.";
}
Another (possibly more fun, but not necessarily more efficient ^^ ) way would be to get the array of items which have a key that starts with b. and use count() on that array :
$array_b = array_filter(array_keys($array), function ($key) {
if (strpos($key, 'b.') === 0) {
return true;
}
return false;
});
echo count($array_b);
If page is always going to be there, you could simply do a count.
if (count($params) == 1) {
echo "There's stuff other than page!";
}
You could be more specific and check page is there and the count is one.
I think this is what you are looking for, the isset function so you would use it like...
if(isset(Array[b.sonic]))
{
//Code here
}

Selectively flattening a PHP array according to parent keys

I need to selectively flatten an array in PHP, but do it selectively. If a key matches a pattern then all sub elements below that key should be included in the 'flat' output.
SO If I had a catalogue of music:
=> array of albums => each of which is an array of song titles
Then I could search for a string, and would get a flat array in reponse. SO if I searched for 'sun' then I would get the entire catalogue for any artist with 'sun' in their name, plus the albums for other artists where 'sun' was in the album name.
Hopefully that makes sense.
Anyone got any thoughts?
Is there a reason you're not using a database to store what sounds like a significant amount of info? It would be fairly simple to write a query in SQL to pull the data out that you want.
Ok, I'm going to assume your data looks like this:
$data = array(
"Bill Withers" => array (
"Lovely Day",
"Use Me",
"Ain't No Sunshine"
),
"Fleet Foxes" => array (
"Sun It Rises",
"White Winter Hymnal"
),
"Billy Joel" => array (
"Piano Man"
)
);
...and that given the input "Bill", you want the output: ["Lovely Day", "Use Me", "Ain't No Sunshine", "Piano Man"]. Here's one way you could do it.
function getSongs($data, $searchTerm) {
$output = array();
foreach ($data as $artist => $songs) {
if (stripos($artist, $searchTerm) !== false)) {
$output = array_merge($output, $songs);
}
}
return $output;
}
...I'll also assume you've got a good reason to not use a database for this.
Do you mean something like that?
$a = array(
"Sunny" => "Bla-bla1",
"Sun" => "Bla-bla2",
"Sunshine" => "Bla-bla3",
);
foreach ($a as $k => $v)
{
// check here whenever you want for $k
// apply something to $v
if ( ... )
$b[$i++] = $v;
}
$sample_data = array(
'artist_name' => array(
'album_name' => array(
'song_name',
'other_mp3_id_info'
)
)
);
function s2($needle,$haystack) {
$threads = array();
foreach ($haystack as $artist_name => $albums) {
if (stripos($artist_name, $needle) !== false)) {
$threads[] = $haystack[$artist_name]; //Add all artist's albums
} else {
foreach ($albums as $album_name => $songs) {
if (stripos($album_name, $needle) !== false)) {
$threads[$artist_name][] = $haystack[$album_name]; //add matching album
}
}
}
}
return $threads;
}
To build off NickF's work, assuming your data looks like his, I'd write the function this way.
function getSongs($data, $searchTerm) {
foreach ($data as $artist => $songs) {
if (stripos($artist, $searchTerm) !== false)) {
$output[$artist] = $songs;
}
}
return $output or null;
}
The results of this function, when no matches are found will obviously return null instead of a blank array; when several matches are found they will then be grouped by their artist. I find direct assignment, $output[$artist] = $songs, to provide more predictable results than array_merge in my own experience. (This also preserves the artist for outputting that data.)
Like NickF said, I would assume you've good reason to not do this with a database? SQL for this would be very simple, such as,
SELECT artist, song FROM songs WHERE artist LIKE '%Bill%' GROUP BY artist;
You can use preg_grep for the searches, be sure to sanitize the input, though:
$matchingArtists = preg_grep('/' . preg_quote($searchString) . '/', array_keys($data));
$matchingSongs = array();
foreach ($data as $artist => $songs) {
$matchingSongs = array_merge($matchingSongs, preg_grep('/' . preg_quote($searchString) . '/', $songs));
}

Categories