Link to home
Start Free TrialLog in
Avatar of Ralph
RalphFlag for United States of America

asked on

PHP array_walk_recursive not behaving for me

I have a set of PHP functions that scan an HTML page and creates a lot of metadata about it.
That's first in the output below.

What I ultimately want is the index (the outer numeric index) for keys of 'name' for values I'm looking for.
Or, programmatically I want to find 0 for name='cust_name'.

I'm trying array_walk_recursive and it's doing it's thing seemingly correctly, but it's outputting its results rather than suffing it into my string.

  1. Do I need to further tweak my callback?  (There's a trailing space issue, but that wouldn't cause #2 just below.)
  2. Why is array_walk_recursive outputting to the browser rather than into the variable?
  3. Ultimately, how can I get the numeric index to the values I want?

-or-

Or what's a better way rather than array_walk_recursive?
and then, #2 above still has me scratching my head!!!


Snippets of (debugging) code:  
$fields = array() ;

$form_details = get_form_input_details($fileinpath, $fileinname.$fileintype,  $lines ) ;   // Generates my array I'm looking at from HTML page

echo "<pre>";
print_r($form_details) ;
echo "1  ";
array_walk_recursive($form_details, 'array_walk_callback', 'name') ; 

echo "<p>2  " ;
$list = array_walk_recursive($form_details, 'array_walk_callback', 'name') ;

echo "<p>3  " ;
echo "list=".$list ;

echo "<p>4  " ;
$fields = explode(' ', $list );

echo "<p>" ;
print_r($fields) ;

exit() ;

# ======================
  function array_walk_callback($item, $key, $match)
{
    if ($key ===$match) { echo "$item ";  }
}

Open in new window


Output:
Array
(
    [0] => Array
        (
            [name] => cust_name
            [type] => <input> type="text"
            [parts] => Array
                (
                    [0] => Array
                        (
                            [value_spec] => Array
                                (
                                    [value] => 
                                    [line_no] => 30
                                    [chars_before] => 100
                                    [chars_after_loc] => 100
                                )

                        )

                )

        )

    [1] => Array
        (
            [name] => cust_desig_2char
            [type] => <input> type="text"
            [parts] => Array
                (
                    [0] => Array
                        (
                            [value_spec] => Array
                                (
                                    [value] => 
                                    [line_no] => 31
                                    [chars_before] => 131
                                    [chars_after_loc] => 131
                                )

                        )

                )

        )

    [2] => Array
        (
            [name] => cust_desig_3char
            [type] => <input> type="text"
            [parts] => Array
                (
                    [0] => Array
                        (
                            [value_spec] => Array
                                (
                                    [value] => 
                                    [line_no] => 32
                                    [chars_before] => 119
                                    [chars_after_loc] => 119
                                )

                        )

                )

        )

    [3] => Array
        (
            [name] => region
            [type] => <input> type="text"
            [parts] => Array
                (
                    [0] => Array
                        (
                            [value_spec] => Array
                                (
                                    [value] => 
                                    [line_no] => 34
                                    [chars_before] => 85
                                    [chars_after_loc] => 85
                                )

                        )

                )

        )

    [4] => Array
        (
            [name] => table_row
            [type] => <input> type="hidden"
            [parts] => Array
                (
                    [0] => Array
                        (
                            [value_spec] => Array
                                (
                                    [value] => 
                                    [line_no] => 49
                                    [chars_before] => 71
                                    [chars_after_loc] => 71
                                )

                        )

                )

        )

)
1  cust_name cust_desig_2char cust_desig_3char region table_row
2  cust_name cust_desig_2char cust_desig_3char region table_row    <-- This shouldn't be here but should be in $list!

3  list=1    //  Why is this 'one'  ???

4  

Array
(
    [0] => 1
)

Open in new window


Thanks for the assistance!
Ralph
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a code example showing some of the "interesting" behaviors of array_walk_recursive().  PHP global declarations make me itch, but in this case, there may not be a good way to avoid them.
<?php // demo/temp_softwarejockey.php
/**
 * https://www.experts-exchange.com/questions/28951283/PHP-array-walk-recursive-not-behaving-for-me.html
 *
 * http://php.net/manual/en/function.array-walk-recursive.php#112339
 * http://php.net/manual/en/function.array-walk-recursive.php#106146
 */
error_reporting(E_ALL);
echo '<pre>';


// VISUALIZE SOME TEST DATA
$num = [ 1, 2, 3,   4, 5,   6, 7, 8  , 9  , 10 ]; // ONE-DIMENSIONAL ARRAY
$arr = [ 1, 2, 3, [ 4, 5, [ 6, 7, 8 ], 9 ], 10 ]; // NESTED ARRAY
print_r($num);
print_r($arr);


// SUM UP THE ONE-DIMENSIONAL ARRAY
$sum = array_sum($num);
echo PHP_EOL . "Array_sum( {ONE-DIMENSIONAL ARRAY} ): $sum";

// NO RECURSION, AND NO ERROR, NO WARNING, NO NOTICE FROM THIS!
$sum = array_sum($arr);
echo PHP_EOL . "Array_sum( {NESTED ARRAY} ): $sum";


// SHOW HOW array_walk_recursive() GETS ONLY LEAF NODES
function print_it($it)
{
    echo PHP_EOL . $it;
}
array_walk_recursive($num, 'print_it');
array_walk_recursive($arr, 'print_it');


// SHOW HOW array_walk_recursive() IS REALLY LAME IF YOU WANT TO SUM UP THE ARRAY USING $sum BY REFERENCE
$sum = 0;
function sum_it($it, &$sum) // $sum IS ENCAPSULATED INSIDE THE SCOPE OF Array_walk_recursive()
{
    $sum += $it;
}
array_walk_recursive($arr, 'sum_it');
echo PHP_EOL . "Array_walk_recursive( {NESTED ARRAY}. 'sum_it' ): $sum";


// SHOW HOW array_walk_recursive() CAN WORK WITH A GLOBAL VARIABLE
$gsum = 0;
function global_sum_it($it)
{
    global $gsum;
    $gsum += $it;
}
array_walk_recursive($arr, 'global_sum_it');
echo PHP_EOL . "Array_walk_recursive( {NESTED ARRAY}. 'global_sum_it' ): $gsum";

Open in new window

Probably should have mentioned this, too.  
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

PHP is a language that has grown "by topsy" over two decades.  As a result, nobody really understands all of it any more and it's full of quirks and inconsistencies.  But that aside, it's reasonably well documented and there are some known good learning resources that can help you level-up.  If you're an experienced programmer, you can read past some parts of the introductory article without need to study all of it.  Hopefully the article and its references can help you avoid the many terrible, insecure, and outdated PHP examples that litter the internet!

Here are some of the things to avoid.
https://www.experts-exchange.com/articles/12293/AntiPHPatterns-and-AntiPHPractices.html
Avatar of Ralph

ASKER

Thanks all.  I fixed my problem.

Ray, because of your link to a link (to a link?), and because of the "Iron Druid" series of books, I now better appreciate the education that the St. Johns colleges provide.  Head spaces for different kinds of thought processing.
Great!  Glad things are pointed in the right direction :-)