Search code examples
phprelated-contentassociate

PHP - identify and tag related child arrays


So I have a multidimensional array.
Each sub-array contains a value for IDs ... 1+ IDs separated by a hash.

array(
    array('name'=>'this', 'ids'=>'x'),
    array('name'=>'this', 'ids'=>'x#y'),
    array('name'=>'this', 'ids'=>'x#y#z'),
    array('name'=>'this', 'ids'=>'y'),
    array('name'=>'this', 'ids'=>'z#b#a'),
    array('name'=>'this', 'ids'=>'d'),
    array('name'=>'this', 'ids'=>'e'),
    array('name'=>'this', 'ids'=>'f#g'),
    array('name'=>'this', 'ids'=>'d#g')
)

Now, as you can see, some of those have an ID in common ... but sometimes it's multiple, other times just 1 out of n, or none.
What I need to do is add a Key to each child array, and have the same Key if they are related.

Thus;

array(
    array('key'=>'1', 'name'=>'this', 'ids'=>'x'),
    array('key'=>'1', 'name'=>'this', 'ids'=>'x#y'),
    array('key'=>'1', 'name'=>'this', 'ids'=>'x#y#z'),
    array('key'=>'2', 'name'=>'this', 'ids'=>'y'),
    array('key'=>'1', 'name'=>'this', 'ids'=>'z#b#a'),
    array('key'=>'3', 'name'=>'this', 'ids'=>'d'),
    array('key'=>'4', 'name'=>'this', 'ids'=>'e'),
    array('key'=>'3', 'name'=>'this', 'ids'=>'f#g'),
    array('key'=>'3', 'name'=>'this', 'ids'=>'d#g')
)

That way I can readily find all related/matching items.

Note : those entries with Key=3 ... ... that's the bit that is screwing me over. a simple foreach won't do the job. I've tried looping through the original array, looking to see if a Key is set, if not, create one ... then extract the IDs and assign them to a new array with that Key. but I never seem to catch the "backward move".

No - no code, as all I've managed so far is a straightforward foreach and new key->IDs array, which fails miserably.

I've searched around - but it seems there isn't anything that tackles this (which I find odd, as I would have thought it common for adding products to DBs etc.).


Solution

  • Okay, try this. Afterwards you have an array with each key once and an array of all ids but every id maximum once. You could implode() the ids array with # if you want a string again.

    <?php
    
    $data = array(
        array('name'=>'this', 'ids'=>'x'),
        array('name'=>'this', 'ids'=>'x#y'),
        array('name'=>'this', 'ids'=>'x#y#z'),
        array('name'=>'this', 'ids'=>'y'),
        array('name'=>'this', 'ids'=>'z#b#a'),
        array('name'=>'this', 'ids'=>'d'),
        array('name'=>'this', 'ids'=>'e'),
        array('name'=>'this', 'ids'=>'f#g'),
        array('name'=>'this', 'ids'=>'d#g')
    );
    
    $keyedData = array();
    
    foreach($data as $k => $r) {
        $newArr = $r;
        $newArr['key'] = $k;
        $newArr['related_to'] = array();
    
        $idArr = explode('#', $r['ids']);
    
        foreach($idArr as $id) {
            foreach($data as $kkey => $kd) {
                if(strpos($kd['ids'], $id) === false)
                    continue;
    
                $newArr['related_to'][] = $kkey;
            }
    
            $newArr['related_to'] = array_unique($newArr['related_to']);
        }
    
        $keyedData[] = $newArr;
    }
    
    echo'<pre>'; var_dump($keyedData);
    
    /* EOF */