indigo diingo Posted June 12, 2009 Share Posted June 12, 2009 Hi, I am learning more about arrays and now i have a problem: Why does my output array ($inputs) has a missing key, 'values' and is there and empty key with empty value on the end of the array? Can somebody please help me out because this isn't making any sense to me. $array = array('title','stage'=>array('width','height','color'),'values','text','colors','direction','system','margin'); $keys = array_keys($array); $values = array_values($array); $i = 0; while($i<count($array)) { $key = $keys[$i]; $value = $value[$i]; if(is_numeric($key)) { $inputs[$array[$i]] = $_POST[$array[$i]]; } else { $inputs[$key] = 'lala'; } $i ++; } print_r($inputs); Print_r: Array ( [title] => titel [stage] => lala [text] => [colors] => [direction] => horizontal [system] => absolute [margin] => on [] => ) 1 Thanks, indigo Link to comment https://forums.phpfreaks.com/topic/161969-array-with-sub-arrays-doesnt-work/ Share on other sites More sharing options...
Ken2k7 Posted June 12, 2009 Share Posted June 12, 2009 The reason it skipped values is because $i is incremented whether $key is numeric or not. So $i starts at 0. Goes to 1 after the first iteration. At index 'stage', $i is 1. After the iteration step, $i will be 2 right? So when you say $array[2], it gives you 'text'. Remember, keys and numbers are at different indexes. The index 'stage' is as is. 'value' will be at index 1, not 2. Get it? You can move $i++ to inside the if statement. That should do it. Link to comment https://forums.phpfreaks.com/topic/161969-array-with-sub-arrays-doesnt-work/#findComment-854708 Share on other sites More sharing options...
indigo diingo Posted June 13, 2009 Author Share Posted June 13, 2009 Thanks dude, That's some clear info you gave me, i appreciate it. The problem is solved but now i have a very difficult script related to the array with subarray topic. So i would like to make a challenge for simplifying. Not because i am lazy (i have worked 3 days on it) but because i believe some of you guys have far more notion about php then me. This what i wrote a function for: $framework = array( 'title', 'stage'=>array('width'=>'','height'=>'','color'=>''), 'values'=>array(), 'texts'=>array(), 'colors'=>array(), 'direction', 'system', 'margin' ); $fruitstock = array( 'Fruit stock', array(670,360,'FFFFFF'), array(10,7,5,10,3,1,, array('peren','appels','bananen','aardbeien','appelsienen','meloenen','bessen'), array('D88A35','3377DB','DB338F','36D69C','CC3B3B','6C39D1','B1D337'), 'vertical', 'relative', 15 ); So my question is, is there a simple and dynamic function for adding the fruitstock array values to the framework without doing it manually? At the moment I have 170 lines of code just to do this! function procesinputs($framework,$statmap) { $array = $framework; $keys = array_keys($array); $values = array_values($array); $i = 0; $j = 0; while($i<count($array)) { $key = $keys[$i]; $value = $value[$i]; if(is_numeric($key)) { $inputs[$array[$j]] = $statmap[$i]; $j ++; } else { $subarray = $array[$key]; $subkeys = array_keys($subarray); $subvalues = array_values($subarray); $subinputs = array(); $k = 0; $m = 0; while($k<count($subarray)) { $subkey = $subkeys[$k]; $subvalue = $subvalues[$k]; if(is_numeric($subkey)) { $subinputs[$m] = $statmap[$i][$k]; $m ++; } else { $subinputs[$subkey] = $statmap[$i][$k]; } $k ++; } $inputs[$key] = $subinputs; } $i ++; } return $inputs; } procesinputs($framework,$fruitstock); I appreciate all the help! Link to comment https://forums.phpfreaks.com/topic/161969-array-with-sub-arrays-doesnt-work/#findComment-854990 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.