Semsem Posted April 3, 2013 Share Posted April 3, 2013 So, what I'm trying to do is rather simple. I have some input fields, and upon POST, I want to tell whether the user has entered values or not, and if not, to echo the values later on in code. The code that works: function format_list( $items ) { if ( count( $items ) == 0 ) return ''; if ( count( $items ) == 1 ) return $items[0]; $last_item = array_pop( $items ); $list_text = join( ', ', $items ) . ' and ' . $last_item; return $list_text; } $items = array(); $keys = array( 'A', 'B', 'C' ); foreach ( $keys as $key ) { if ( empty( $_POST[$key] ) ) $items[] = $key; } echo format_list( $items ); But, what I want to do is instead of echoing "A" or "B" or "C", I want it to echo something more meaningful, something the user later in the code, or me later down the road, can understand better. So, I have tried to basically alias the array, and am trying to get the values in the alias to be the values assigned in $items My code that doesn't exactly work: function format_list( $items ) { if ( count( $items ) == 0 ) return ''; if ( count( $items ) == 1 ) return $items[0]; $last_item = array_pop( $items ); $list_text = join( ', ', $items ) . ' and ' . $last_item; return $list_text; } $items = array(); $keys = array( 'A', 'B', 'C' ); $keys2 = array( 'ValueA', 'ValueB', 'ValueC' ); $keys3 = array_combine($keys, $keys2); foreach ( $keys3 as $key ) { if ( empty( $_POST[array_keys($key)] ) ) $items[] = array_values($key); } echo format_list( $items ); I know the Values aren't really any clearer to anyone, I'll have to edit it later, I just want to get it working first. What that does is tell me: Warning: array_keys() expects parameter 1 to be array, string given in X.php on line 53 Warning: array_values() expects parameter 1 to be array, string given in X.php on line 54 Warning: array_keys() expects parameter 1 to be array, string given in X.php on line 53 Warning: array_values() expects parameter 1 to be array, string given in X.php on line 54 Warning: array_keys() expects parameter 1 to be array, string given in X.php on line 53 Warning: array_values() expects parameter 1 to be array, string given in X.php on line 54 And echo's ", and" I cannot see what I'm doing wrong. I create a new array, combine it with the first (to "alias" is), and then want to split it apart...and then it fails to work. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/ Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 Have you considered using both the key and the value in that foreach? You were so close. // $_POST = array("A" => "", "B" => "Original B"); $defaults = array("A" => "ValueA", "B" => "ValueB", "C" => "ValueC"); $items = array(); foreach ($defaults as $key => $value) { if (empty($_POST[$key])) { $items[$key] = $value; } else { $items[$key] = $_POST[$key]; } }Or you can just use one array.$items = array("A" => "ValueA", "B" => "ValueB", "C" => "ValueC"); foreach (array_keys($items) as $key) { if (!empty($_POST[$key])) { $items[$key] = $_POST[$key]; } } Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/#findComment-1422556 Share on other sites More sharing options...
Semsem Posted April 3, 2013 Author Share Posted April 3, 2013 $items = array("A" => "ValueA", "B" => "ValueB", "C" => "ValueC"); foreach (array_keys($items) as $key) { if (!empty($_POST[$key])) { $items[$key] = $_POST[$key]; } } Both of those examples do the same thing, I prefer the second one to be honest, it's simpler! It does echo ValueA, ValueB, and ValueC, but the way I had it before, if none of the input fields had any text entered in them, it'd echo "A, B and C" but if the input field for A, for instance, had something typed into it, upon submit, once the function was called, it'd echo "B and C". Your edit, however, echo's "ValueA, ValueB and ValueC", but when something is typed into the input field for A (let's say "Value for Input A", it then echo's "Value in Input A, ValueB and ValueC" instead of "ValueB and ValueC" like I'd want it to. I've tried editing your code, and none of the following work: $items = array("customName" => "ValueA", "B" => "ValueB", "C" => "ValueC"); foreach (array_keys($items) as $key) { if (!empty($_POST[$key])) { $items[] = $key; } } It echo's "ValueA, ValueB, ValueC and A" And: $items = array("customName" => "ValueA", "B" => "ValueB", "C" => "ValueC"); foreach (array_keys($items) as $key) { if (!empty($_POST[$key])) { $items[$key] = $key; } } It echo's "A, ValueB and ValueC" Any ideas? If you understand what I'm looking to do? Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/#findComment-1422562 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 So... you're not asking about how to provide a "default" value for something in case one wasn't provided in the form? Because that's really what it sounded like. Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/#findComment-1422565 Share on other sites More sharing options...
Semsem Posted April 3, 2013 Author Share Posted April 3, 2013 I don't think that's exactly what I meant. Let me try to explain it a bit more: I have a form, and I want the user to be able to input data into it, and upon submit, have it echo somewhere (not quite sure where I want it echoed yet) which input fields the user did not enter data, and have it be echoed as something meaningful rather than the name of the input tag. The way my code originally was written (below) would allow me to echo the names of the input tags, and if one of them contained user inputted data, it would skip echoing that one and echo the ones that didn't contain user inputted data. function format_list( $items ) { if ( count( $items ) == 0 ) return ''; if ( count( $items ) == 1 ) return $items[0]; $last_item = array_pop( $items ); $list_text = join( ', ', $items ) . ' and ' . $last_item; return $list_text; } $items = array(); $keys = array( 'A', 'B', 'C' ); foreach ( $keys as $key ) { if ( empty( $_POST[$key] ) ) $items[] = $key; } So, what I want to be able to do is create an alias, per se, of the input names, so that if input name=A is left blank, it would echo somewhere as some other name than "A". Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/#findComment-1422566 Share on other sites More sharing options...
Solution requinix Posted April 3, 2013 Solution Share Posted April 3, 2013 (edited) Ah, okay, it makes sense now. If you can safely array_filter $_POST, which will automatically prune out empty array values, then it's as simple as $required = array("A" => "Field A", "B" => "Field B", "C" => "Field C"); $diff = array_diff_key($required, array_filter($_POST)); Otherwise $required = array("A" => "Field A", "B" => "Field B", "C" => "Field C"); $missing = array(); foreach ($required as $name => $label) { if (empty($_POST[$name])) { $missing[] = $label; } } (assuming I understand you correctly this time) [edit] #$^(*@#, code tags are killing my indentation again Edited April 3, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/#findComment-1422568 Share on other sites More sharing options...
Semsem Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) Both work. Thank you for your assistance. However, the second one needs to be written as: $required = array("A" => "Field A", "B" => "Field B", "C" => "Field C"); $missing = array(); foreach ($required as $name => $label) { if (empty($_POST[$name])) { $items[] = $label; } } With $items[], so that it works properly with the format_list function from the above. Otherwise it worked just fine. Thanks again. Edited April 3, 2013 by Semsem Quote Link to comment https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/#findComment-1422672 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.