barrywood Posted October 31, 2007 Share Posted October 31, 2007 I'm realtively new to PHP and I'm trying to get my head around a particular issue. I've got an array that looks like this: $field_array = array ( array (field=>"dist_Company", type=>"text"), array (field=>"dist_Active", type=>"select", options=>array(array(oname=>"yes", oval=>"1"), array(oname=>"no", oval=>"0"))), array (field=>"dist_CompanyID", type=>"select"), array (field=>"dist_Shipping", type=>"text") ); What I'm trying to figure out is how to add to this array after it's been defined. In this case I'm trying to add the "options" subarray to dist_CompanyID element so that it has options similar to those predefined in the dist_Active element. I can do it if I specify the index like this: $field_array[2]['options'][] = array(oname=>"one", oval=>"1"); $field_array[2]['options'][] = array(oname=>"two", oval=>"2"); $field_array[2]['options'][] = array(oname=>"three", oval=>"3"); Is there some elegant way to find the index number for the element containing field=>"dist_CompanyID"? TIA Quote Link to comment Share on other sites More sharing options...
Barand Posted November 1, 2007 Share Posted November 1, 2007 I think you will need a custom search function for that one, such as my_array_search() below <?php $field_array = array ( array (field => "dist_Company", type=>"text"), array ( field => "dist_Active", type => "select", options => array( array( oname=>"yes", oval=>"1"), array( oname=>"no", oval=>"0") ) ), array (field=>"dist_CompanyID", type=>"select"), array (field=>"dist_Shipping", type=>"text") ); $index = my_array_search("field", "dist_CompanyID", $field_array); echo $index; function my_array_search($ind, $value, &$arr) { for ($i=0, $k = count($arr); $i < $k; $i++) { if ($arr[$i][$ind] == $value) return $i; } return false; } ?> Quote Link to comment Share on other sites More sharing options...
barrywood Posted November 1, 2007 Author Share Posted November 1, 2007 Thanks Barand. Being new to PHP I didn't know if there was just some handy function that I was missing. Doing a search to find the index works nicely. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.