Jump to content

fiasst

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Everything posted by fiasst

  1. Having had a long think I came up with this which seems to work too! <?php $arr = array( 0 => array('filename' => 'image_id.jpg'), 1 => array('filename' => 'image_site1.jpg'), 2 => array('filename' => 'image_site2.jpg'), ); $type = "_site"; for ($i = 0; $i < count($arr); $i++){ $key = ((preg_match("/$type/i", $arr[$i]['filename'])) ? -$i : $i); $newarr[$key] = $arr[$i]['filename']; } ksort($newarr); ?> Not sure how clean a solution it is though.
  2. Blimey, never would have come to that conclusion. Thankyou, I'll have a play with it!
  3. Hi, I've been trying to re-order an array and I can't seem to even do it let alone neatly. I've ran out of time and would appreciate any help! $array = array( '0' => array( 'filepath' => 'files/image_site1.png', 'title' => 'Website', ), '1' => array( 'filepath' => 'files/image_id.png', 'title' => 'Identitity', ), '2' => array( 'filepath' => 'files/image_site2.png', 'title' => 'Website', ) ) I'm looking to re-order the arrays within the parent array depending on whether the 'filepath's contain a sub string of '_id' or '_site'. I've been using: $type = '_site'; foreach ($array as $item){ if (preg_match("/$type/i", $item['filename'])){ //add this $item to the top of array, somehow... } } So with $type set to '_site', I'd like to re-arrange the array so both arrays containing _site1.png and _site2.png appear at the top of the parent array. I.e: $array('0' = array(...), '2' => array(...), '1' => array(...)); I hope I've explained it well enough. Thanks,
  4. That's the ticket! *massive sigh of relief* Thank you so much for your help! I kept re-writing it and always ended up with about 5 foreach() statements before getting confused. Really appreciate it :]
  5. I've been trying to find a way of checking if build_date is an Array and if so, do any of the array_values contain another array. If it doesn't then implode the values. "build_date" => array( "month" => 6, "day" => 14, "year" => 2010, ), I just can't for the life of me code it, hehe. Been trying since sunday morning!
  6. Hi Alex, thanks for replying. This code is spot on appart from one array instance. I've stripped the code down temporarily for ease: function hidden_fields_recursive($arr){ foreach($arr as $key => $val) { if (is_array($val)){ hidden_fields_recursive($val); } else { if (substr($key, 0, 6) == 'build_') { echo 'name: '.$key.' | value: '.$val; } } } } $arr = array( "build_age" => 20, "build_list" => array( 11 => 11, 7 => 7, 8 => 8, ), "build_options" => array( "build_date" => array( "month" => 6, "day" => 14, "year" => 2010, ), "build_hour" => 10, "build_hour" => 17, ), ); hidden_fields_recursive($arr); Results in: name: build_age | value: 20 name: 11 | value: 11 name: 7 | value: 7 name: 8 | value: 8 name: build_options | value: Array name: build_date | value: Array//(can't always guarantee the name will be build_date) name: month | value: 6 name: day | value: 14 name: year | value: 2010 ...ect but I'm hoping to return the build_list and build_date values as strings. ie: name: build_list | value: 11,7,8 name: build_date | value: 6,14,2010 Hope that makes sense, thanks again for any help
  7. Hi there, I'm having real trouble writing a script to print hidden form fields to my page from an array. I've been trying to print <input type="hidden" name="[array_key]" value="[array_value]" /> for each item that has 'build_' included in the key. I've ran out of time and talent and would really appreciate some help! Here's an example of my array and below the outcome i'd like returned to the page: Array( [build_AGE] => 20 [foo] => dont use this as it doesn't have 'build_' in the key [bUILD_OPTIONS] => Array( [build_HOUR] => 06 [build_MIN] => 10 [build_DATE] => Array( [month] => 6 [day] => 14 [year] => 2010 ) ) ) <input type="hidden" name="build_AGE" value="20" /> <input type="hidden" name="build_OPTIONS" value="Array" />//I don't actually need this but it seems too complicated to avoid, no harm in leaving it <input type="hidden" name="build_DATE" value="6,14,2010" /> <input type="hidden" name="build_HOUR" value="06" /> <input type="hidden" name="build_MIN" value="10" /> Here's my attempt. As you can probably tell I was getting carried away and, I think, over complicating it: if (is_array($form_values)){ foreach ($form_values as $k => $v){ if (!empty($v) && preg_match('/^build/', $k)){ if (is_array($v)){ foreach ($v as $k2 => $v2){ if (is_array($v2)){ $v2 = implode(",", array_values($v2)); print '<input type="hidden" name="'.$k.'" value="'.$v2.'" />'; } else { print '<input type="hidden" name="'.$k.'" value="'.$v2.'" />'; } } } else { print '<input type="hidden" name="'.$k.'" value="'.$v.'" />'; } } } } Thanks, Marc
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.