Jump to content

Aliasing an Array, per se


Go to solution Solved by requinix,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/276452-aliasing-an-array-per-se/
Share on other sites

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];
}
}

 

$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?

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".

  • Solution

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 by requinix

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 by Semsem
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.