|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-12-19 12:17 UTC] tony2001@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 04:00:02 2025 UTC |
Description: ------------ It seems that I am not being able to return an array from a function. I read a serialized string, unserialize it and then call a recursive function to get the deepest keys and values Reproduce code: --------------- $test_serial = <<< FO a:5:{i:0;a:7:{s:8:"question";s:31:"What is your tv watching habits";s:5:"field";s:10:"checkboxes";s:7:"default";N;s:9:"charwidth";N;s:5:"lines";N;s:6:"maxlen";N;s:7:"options";s:54:"never watch tv |always am on the tube |i dont own a tv";}i:1;a:7:{s:8:"question";s:27:"What is your favorite genre";s:5:"field";s:12:"radiobuttons";s:7:"default";N;s:9:"charwidth";N;s:5:"lines";N;s:6:"maxlen";N;s:7:"options";s:19:"horror |sex |comedy";}i:2;a:7:{s:8:"question";s:0:"";s:5:"field";s:9:"textfield";s:7:"default";s:6:"sfasdf";s:9:"charwidth";s:8:"sadfasdf";s:5:"lines";N;s:6:"maxlen";s:7:"asdfasd";s:7:"options";N;}i:3;a:7:{s:8:"question";s:27:"what is your favorite color";s:5:"field";s:9:"textfield";s:7:"default";s:0:"";s:9:"charwidth";s:0:"";s:5:"lines";N;s:6:"maxlen";s:0:"";s:7:"options";N;}i:4;a:7:{s:8:"question";s:14:"favorte things";s:5:"field";s:9:"textfield";s:7:"default";s:0:"";s:9:"charwidth";s:0:"";s:5:"lines";N;s:6:"maxlen";s:0:"";s:7:"options";N;}} FO; $test = unserialize($test_serial); $test_keys = array(); $test_val = array(); function getarr_items($array,$mode){ $newarray = array(); if(is_array($array)){ foreach($array as $key => $val){ if(is_array($val)){ getarr_items($val,$mode); } else { if($mode == "k"){ $newarray[] = $key; } else $newarray[] = $val; } } } return $newarray; } $test_keys = getarr_items($test,'k'); $test_val = getarr_items($test,'v'); print_r($test_keys); echo "<------>"; print_r($test_val); Expected result: ---------------- should return an array with values Actual result: -------------- empty array