enter Array in usermeta issue

Hello. I'm having difficulty with manipulating arrays in usermeta.

When a user gets created, I add the following array to the database doing:

$myarray = array(
	'10'=>'10',
	'108'=>'108',
	'130'=>'130',
	'281'=>'281',
	'308'=>'308',
	'541'=>'541'
	);
add_user_meta( $user_id, 'mymetafield', $myarray);

That works fine. When I need to check if a value exists as a key in the array keys, I use a function:

function key_compare($keytocheck, $user_id)
{

	$myarray = get_user_meta($user_id, 'metafield');

	if (array_key_exists($keytocheck,$myarray[0])) {

		return true;
	}
	else{
			return false;
	}
}

This also works fine.

However, the issue starts when I try to add a value to the array.

I'd like to add

'102'=>'175'

at the end of the array.

Currently I have:

function update_myarray($key,$value,$user_id)
{
	$myarray = get_user_meta($user_id, 'metafield');

	if(array_key_exists($key,$myarray))
	{
		unset($myarray[$key]);
	}

	$myarray[0][$key] = $key;

	update_user_meta($user_id,'metafield',$myarray); 

}

Unfortunately, that messes up my array because when I use the function key_compare, it doesn't work anymore.

Everything works if I don't use the Update function.

The string value on the database before the udpate is:

a:6:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";}

the string value after two updates it becomes:

a:1:{i:0;a:2:{i:0;a:7:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";i:293;s:3:"630";}i:157;s:3:"631";}}

I hope someone can help!

Thank you

Giovanni

  • aecnu
    • WP Unicorn

    Greetings Giovanni,

    Thank you for being a WPMU Dev member!

    I'll see if I can get the our lead developer in here to assist / give you some advice regarding your custom code.

    Thanks again and it is great to have you as a WPMU Dev member, looking forward to getting this worked out!

    Cheers, Joe :slight_smile:

  • Arnold
    • El Macho WP

    Too much error checking. Just do:

    $myarray = get_user_meta($user_id, 'metafield');
    $myarray[$key]=$value;
    update_user_meta($user_id,'metafield',$myarray);

    If [$key] already exists it will be overwritten. If not it will be added. To remove it unset( $myarray[$key])

    But it's just that simple.

  • Giovanni
    • The Incredible Code Injector

    Thank you for your response however that did not work.

    If I do that, the array in the meta value goes from:

    a:6:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";}

    to

    a:2:{i:0;a:6:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";}i:293;s:3:"642";}

    If I have the following syntax:

    $myarray = get_user_meta($user_id, 'metafield');
    $myarray[0][$key]=$value;
    update_user_meta($user_id,'metafield',$myarray);

    It also doesn't work, it gives me this:

    a:1:{i:0;a:7:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";i:293;s:3:"643";}}

    The end result should be:

    a:7:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";i:KEY;s:3:VAL}

    But can't get that to work...

  • Arnold
    • El Macho WP

    Stop using the [0]

    What your doing is nesting a new array in the array.
    You started with

    $myarray = array(
    	'10'=>'10',
    	'108'=>'108',
    	'130'=>'130',
    	'281'=>'281',
    	'308'=>'308',
    	'541'=>'541'
    	);

    and the line
    $myarray[0][$key] = $key;
    is doing this

    $myarray = array(
    	'10'=>'10',
    	'108'=>'108',
    	'130'=>'130',
    	'281'=>'281',
    	'308'=>'308',
    	'541'=>'541',
                    0 => array($key => $key),
    	);

    and remember [10] is not the same as ['10'] so make sure your passing strings as keys and not numbers.

  • Giovanni
    • The Incredible Code Injector

    Thanks again!

    The thing is that if I don't use the [0] then I cannot get access to the array.

    For some reason, when I debug, to read the key/value relationships I have to use [0] to read the variable from get_user_meta function:

    $myarray = get_user_meta($user_id, 'metafield')
            foreach ($myarray[0] as $key=>$value)
    	{
    		echo "Key = $key <br>";
    		echo "Value = $value <br>";
    	}

    If I don't do that, I get:

    Key=0
    Value = Array

    The way I add the value to the meta field is:

    $myarray = array(
    	'10'=>'10',
    	'108'=>'108',
    	'130'=>'130',
    	'281'=>'281',
    	'308'=>'308',
    	'541'=>'541'
    	);
    add_user_meta( $user_id, 'mymetafield', $myarray);

    HOWEVER, I think the error is solved... I ended up using [0] in the update function:

    $myarray = get_user_meta($user_id, 'metafield');
    $myarray[0][$key]=$value;
    update_user_meta($user_id,'metafield',$myarray[0]);

    This seems to work ... the new database value:

    a:7:{i:10;s:2:"10";i:108;s:3:"108";i:130;s:3:"130";i:281;s:3:"281";i:308;s:3:"308";i:541;s:3:"541";i:293;s:3:"646";}

    Now, I don't know why this is ... and why I have to use [0]. But I would love an explanation if you have one.

    Thank you again for your help!

  • Arnold
    • El Macho WP

    Ah, your missing an argument.

    Usage

    <?php get_user_meta($user_id, $key, $single); ?>

    try
    $myarray = get_user_meta($user_id, 'metafield', true);

    By default get_user_meta always returns an array, even if it's just one number. The true tells it to return without wrapping in an array.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.