Ralph
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.
-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:
Output:
Thanks for the assistance!
Ralph
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.
- Do I need to further tweak my callback? (There's a trailing space issue, but that wouldn't cause #2 just below.)
- Why is array_walk_recursive outputting to the browser rather than into the variable?
- 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 "; }
}
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
)
Thanks for the assistance!
Ralph
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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 :-)
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