Search code examples
phparraysregexstringarray-key

compare string to sub key value in php array partial match


I need to figure out a way to partial match a string to a sub key in my PHP array.

example:

string = howdy-doody show as you can see there is a - dash and a space between the words. In my PHP array the sub key might be howdy doody show with no dashes or it might be howdy doody-show with the dash between a different word in the string.

How can I find the sub key in the array with the string given?

sample array

$pages = array(

'Administrator' => array(
    'network-administrator' => array('title' => 'Network '.$li_1, 'description' => 'Network '.$li_1.' '.$temp_content, 'post' => '<p>Network '.$li_1.' '.$temp_content.'.</p>'),
    'database administrator' => array('title' => 'Database '.$li_1, 'description' => 'Database '.$li_1.' '.$temp_content, 'post' => '<p>Database '.$li_1.' '.$temp_content.'.</p>'),
),

'Analyst' => array(
    'business systems analyst' => array('title' => 'Business Systems '.$li_2, 'description' => 'Business Systems '.$li_2.' '.$temp_content, 'post' => '<p>Business Systems '.$li_2.' '.$temp_content.'.</p>'),
    'data-analyst' => array('title' => 'Data '.$li_2, 'description' => 'Data '.$li_2.' '.$temp_content, 'post' => '<p>Data '.$li_2.' '.$temp_content.'.</p>'),
),

);

sample string

network administrator

sample variable to locate the array value

$content = $pages['Administrator']['network administrator'];

with the above variable it won't find the sub key in the array because the sub key uses a - dash like this network-administrator.

So how would I get the array value including the original subkey and returns its contents using the string that has the space instead of dash like so, network administrator?

Much appreciated for help!


Solution

  • Here's one way to do it: remap your original keys to a new array containing stripped keys, and store the original key in the value for the array.

    $t_keys = array();
    
    foreach ($pages as $k => $arr2) {
        foreach (array_keys($arr2) as $a) {
            // perform whatever transformations you want on the key here
            $new_key = str_replace("-", " ", $a);
            // use the transformed string as the array key;
            // we still need to access the data in the original array, so store the outer
            // array key ("Administrator", "Analyst", etc.) and the inner array key
            // ("network-administrator", etc.) in a subarray.
            $t_keys[$new_key] = array( $k, $a );
        }
    }
    

    An example key-value pair from $t_keys:

    $t_keys['network administrator'] = ['Administrator', 'network-administrator']
    

    To access the value in the original array, we need to get $pages['Administrator']['network-administrator'], or, using the equivalent values from $t_keys: $pages[ $t_keys['network administrator'][0] ][ $t_keys['network administrator'][1] ].

    To match against your search string:

    $str = str_replace("-", " ", $original_search_string_from_url);
    
    // check if it's in the transformed keys array
    if (array_key_exists($str, $t_keys)) {
        // now we can access the data from our $pages array!
        $target = $pages[ $t_keys[$str][0] ][ $t_keys[$str][1] ];
        echo "the proper name for $str is " . $t_keys[$str][1] . "\n";
    //  output: "the proper name for network administrator is network-administrator"
    
        // access various attributes of the network-administrator:
        echo "the title of $str is " . $target['title'];
    }
    

    If you don't need to know what the keys in $pages are (e.g. 'Administrator' and 'network-administrator') and just want to get straight to the relevant data, you could create references instead of storing the keys. Here's an example:

    $refs = array();
    
    foreach ($pages as $k => $arr2) {
        foreach (array_keys($arr2) as $a) {
            // perform whatever transformations you want on the key here
            $new_key = str_replace("-", " ", $a);
            // create a reference ( =& ) to the contents of $pages[$k][$a]
            // $refs[$new_key] points directly at $pages[$k][$a]
            $refs[$new_key] =& $pages[$k][$a];
        }
    }
    

    Now $refs['network administrator'] acts like a shortcut to $pages['Administrator']['network-administrator']; $refs['network administrator']['post'] accesses $pages['Administrator']['network-administrator']['post'], and so on.