PHP Recursive Array Search - returning nothing ?

I've created a recursive array that does exactly what I need it to do.  I can echo the results just fine from within the function but when I return the values I can't extract them so they are available in the original calling function.

In this code example I would expect echo $indexf." / ".$keyf; to show the Matched on values clearly echoed from within the function itself.

Exact Code Example:

<?
function cm_array_search_recurs($needle,$haystack,$index=0){    
    foreach ($haystack as $k => $value){
        if(is_array($value)){
           cm_array_search_recurs($needle, $value,$index);
        } else if(!is_array($value)){
            if($value==$needle){
                # jay we know that the 
                #       name of the returning field is $ $returnFields[$index]['dbFieldName']
                #       value of the returning field is $$ $returnFields[$index]['dbFieldName']
                echo "\n In function: Matched on index [$index] ($value to $needle) and key ($k) return data to original call as array";
                return array($index,$k);
                break;
            }
        }
        $index++;
    }
    
}

    $returnFields[0]['dbColName']='company_id';
    $returnFields[0]['fieldName']='company_id';
    $returnFields[0]['sqlAction']='sel';
    $returnFields[0]['styleInfo']='display:none';

    $returnFields[1]['dbColName']='company_name';
    $returnFields[1]['fieldName']='company_name';
    $returnFields[1]['sqlAction']='and';
    $returnFields[1]['styleInfo']='color:red;background:orange';

    $returnFields[2]['dbColName']='co_address1';
    $returnFields[2]['fieldName']='comp_address1[0]';
    $returnFields[2]['sqlAction']='or';
    $returnFields[2]['styleInfo']='color:red';

    $returnFields[3]['dbColName']='co_city';
    $returnFields[3]['fieldName']='comp_city[0]';
    $returnFields[3]['sqlAction']='or';
    $returnFields[3]['styleInfo']='color:red';

    $returnFields[4]['dbColName']='co_state';
    $returnFields[4]['fieldName']='co_state[0]';
    $returnFields[4]['sqlAction']='sel';
    $returnFields[4]['styleInfo']='color:red';

    $returnFields[5]['dbColName']='co_zip';
    $returnFields[5]['fieldName']='co_zip[0]';
    $returnFields[5]['sqlAction']='sel';
    $returnFields[5]['styleInfo']='color:red';

    $returnFields[6]['dbColName']='co_phonenum1';
    $returnFields[6]['fieldName']='phone_comp_ac-pre-line';
    $returnFields[6]['sqlAction']='or';
    $returnFields[6]['styleInfo']='color:red';

    $returnFields[7]['dbColName']='co_facebook_page';
    $returnFields[7]['fieldName']='facebook_page';
    $returnFields[7]['sqlAction']='sel';
    $returnFields[7]['styleInfo']='color:red';

    $returnFields[8]['dbColName']='co_domain_name';
    $returnFields[8]['fieldName']='co_domain_name';
    $returnFields[8]['sqlAction']='sel';
    $returnFields[8]['styleInfo']='color:red';
    
list($indexf,$keyf)=cm_array_search_recurs('co_state', $returnFields);

echo $indexf." / ".$keyf;
?>

Open in new window


What sayeth the experts ?

Jay
Jay LeporeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
The problem is most likely that you are not returning a value all the way to the top. For example, let's say you end up with:

cm_array_search_recurs("needle", $haystack, 0);
   cm_array_search_recurs("needle", $haystack, 1);
   cm_array_search_recurs("needle", $haystack, 1);
     cm_array_search_recurs("needle", $haystack, 2);
       cm_array_search_recurs("needle", $haystack, 3);
         return array(3, "key");
   cm_array_search_recurs("needle", $haystack, 2);

You are returning those values to the next-outer iteration, but that iteration does nothing with it. I would rewrite it so that it returns a null when nothing is found, and when an array is found, keep returning it up the chain:

function cm_array_search_recurs($needle,$haystack,$index=0){    
    foreach ($haystack as $k => $value){
        if(is_array($value)){

            // Check the results of the next iteration
           $results = cm_array_search_recurs($needle, $value,$index);

           if(is_array($results))
           {
              // We found a match, so pass the results back to the next outer layer
              return $results;
           }
        } else if(!is_array($value)){
            if($value==$needle){
                # jay we know that the 
                #       name of the returning field is $ $returnFields[$index]['dbFieldName']
                #       value of the returning field is $$ $returnFields[$index]['dbFieldName']
                echo "\n In function: Matched on index [$index] ($value to $needle) and key ($k) return data to original call as array";
                return array($index,$k);
                break;
            }
        }
        $index++;
        return null; // Indicate that we didn't find a match
    }
    
}

Open in new window

0
gr8gonzoConsultantCommented:
Also, you are passing an index, but the logic doesn't quite seem right. It might be right for your situation, though - not sure. Not sure what you are trying to do, but if you get the right values, then more power to you.
0
Ray PaseurCommented:
I don't see any use for a recursive search, given the test data.
http://www.laprbass.com/RAY_temp_compu523.php

If you want recursive things, you might look into using the SPL.
http://php.net/manual/en/spl.iterators.php

<?php // RAY_temp_compu523.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28236631.html

function cm_array_search_recurs($needle,$haystack,$index=0)
{
    while($haystack)
    {
        foreach ($haystack[$index] as $key => $value)
        {
            if ($value == $needle) return array($index, $key);
        }
        unset($haystack[$index]);
        $index++;
    }
    return FALSE;
}

$returnFields[0]['dbColName']='company_id';
$returnFields[0]['fieldName']='company_id';
$returnFields[0]['sqlAction']='sel';
$returnFields[0]['styleInfo']='display:none';

$returnFields[1]['dbColName']='company_name';
$returnFields[1]['fieldName']='company_name';
$returnFields[1]['sqlAction']='and';
$returnFields[1]['styleInfo']='color:red;background:orange';

$returnFields[2]['dbColName']='co_address1';
$returnFields[2]['fieldName']='comp_address1[0]';
$returnFields[2]['sqlAction']='or';
$returnFields[2]['styleInfo']='color:red';

$returnFields[3]['dbColName']='co_city';
$returnFields[3]['fieldName']='comp_city[0]';
$returnFields[3]['sqlAction']='or';
$returnFields[3]['styleInfo']='color:red';

$returnFields[4]['dbColName']='co_state';
$returnFields[4]['fieldName']='co_state[0]';
$returnFields[4]['sqlAction']='sel';
$returnFields[4]['styleInfo']='color:red';

$returnFields[5]['dbColName']='co_zip';
$returnFields[5]['fieldName']='co_zip[0]';
$returnFields[5]['sqlAction']='sel';
$returnFields[5]['styleInfo']='color:red';

$returnFields[6]['dbColName']='co_phonenum1';
$returnFields[6]['fieldName']='phone_comp_ac-pre-line';
$returnFields[6]['sqlAction']='or';
$returnFields[6]['styleInfo']='color:red';

$returnFields[7]['dbColName']='co_facebook_page';
$returnFields[7]['fieldName']='facebook_page';
$returnFields[7]['sqlAction']='sel';
$returnFields[7]['styleInfo']='color:red';

$returnFields[8]['dbColName']='co_domain_name';
$returnFields[8]['fieldName']='co_domain_name';
$returnFields[8]['sqlAction']='sel';
$returnFields[8]['styleInfo']='color:red';

list($indexf,$keyf)=cm_array_search_recurs('co_state', $returnFields);

echo $indexf." / ".$keyf;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Jay LeporeAuthor Commented:
Diagnosis was accurate, end result exact.  Thank you.
0
Jay LeporeAuthor Commented:
gr8gonzo's comment was considered and code tested but same end result, nothing returned. Thanks anyway gr8gonzo for your effort.

Jay
0
Ray PaseurCommented:
Thanks for the points.  Don't try this with an object.  It works nicely with an array because the function operates on a copy of the array, so unset() is working on the copy, not the original array!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.