Search the Community
Showing results for tags 'associative'.
-
Hello, Have an associative array of variable length and variable key names. The POST array can have the following structure, where [rec_n] can have one or many elements. : $_POST Array ( [rec_1769057] => on [rec_1768743] => on [ponumb] => D000000034 [strnbr] => 100 ) The 'n' is a variable SKU number and concatenated from a select statement. There are thousands of SKUs. PONUMB and STRNBR are static and will always be at the end of the array. I have an algorithm for slicing this array into two separate arrays. skuitems Array ( [rec_1769057] => on [rec_1768743] => on ) postrnbr Array ( [ponumb] => D000000034 [strnbr] => 100 ) The two arrays are then assigned to a $params array one for each update statement... $params Array ( [0] => Array ( [0] => 1768743 [1] => D000000034 [2] => 100 ) [1] => Array ( [0] => 1769057 [1] => D000000034 [2] => 100 )) I would like to accomplish the above in one go... At present the solution has and O(2) notation. Here is the algorithm that works, but seems clunky to me. if( isset($postar) && is_array($postar)) { // first order of mag. $cnt = count($postar) - 2; // total minus last two elements $postrnbr = array_slice( $postar, -2, 2 ); // always ponumb and strnbr $skuitems = array_slice($postar, 0, $cnt); // will be one or more sku items $skukeys = array_keys($skuitems); $qparams = array(); //bind params for update statement foreach($skukeys as $sku) { // second order of mag. $skusplit = explode("_",$sku); // ie Array( [0]=>rec, [1]=>1234545 ) $qparams[] = array($skusplit[1], $postrnbr['ponumb'], $postrnbr['strnbr']); } } Thanks in advance! rwhite35
-
I'm trying to get some values from a specifc row of an array. My array has this sort of structure: $places = array( array("name" => "Cabot Cove", "area" => "12", "lat" => "-11.003", "lon" => "-151.2285", "pop" => "17"), array("name" => "Smallville", "area" => "32;", "lat" => "-19.910", "lon" => "-50.205", "pop" => "18"), array("name" => "Gotham City", "area" => "85", "lat" => "-39.9294", "lon" => "-40.199", "pop" => "14"), array("name" => "Springfield", "area" => "21.6", "lat" => "-43.9534", "lon" => "-24.2118", "pop" => "18"), ); If a variable $city is set to "Gotham CIty", for example the url of the page could be "/cities/script.php?city=Gotham+City", how do I look through the array and get the "lat" and "lon" values for just this one $city from the array?