如何为数组的打印值应用条件


How to apply condition for print values of an array

如何为打印值应用条件​​阵列

你好工作人员S.O.F我再次需要你的帮助,

该功能打乱数字,

`

function gen_num()
{
$caracteres = "012345678910111213141516171819";
$mistura = substr(str_shuffle($caracteres),0,15);
print $mistura;

`

我需要一个条件来打印或不打印帖子中的数字,@leglessrazard用户帮助了我。它运行得很好,最大的问题是每次新访问帖子或刷新木棉时,都会生成一个新的数字/值,但这不可能发生,我需要在这些值上永久写入​​在帖子和下面的解决方案,但不能应用于数组!

`

function gen_num()
{
global $post;
$mistura = get_post_meta( $post->ID, 'my_custom_meta', true );
if ( '' == $mistura ) {
$caracteres = "012345678910111213141516171819";
$mistura = substr(str_shuffle($caracteres),0,10);
update_post_meta( $post->ID, 'my_custom_meta', $mistura );
}
print $mistura;
}

`

我现在正在尝试制作数组,但我做不到!

`

function shuflenames()
{
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "'n";
echo $input[$rand_keys[1]] . "'n";

`

我尝试用数组应用条件失败:

`

function shuflenames()
{
global $post;
$mixnames = get_post_meta( $post->ID, 'my_custom_meta', true );
if ( '' == $mixnames) {
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
update_post_meta( $post->ID, 'my_custom_meta', $mixnames );
}
echo $input[$rand_keys[0]] . "'n";
echo $input[$rand_keys[1]] . "'n";
}

`

但由于条件不存在,单词会发生变化。每一个新的访问字都会被更改,它们实际上并没有写在

的帖子中

这是用户@bcworkz在wordpress dot org中响应并帮助我的解决方案:

function gen_num()
{
  global $post;
  $mixnames = get_post_meta( $post->ID, 'fieldnames', true );
  if ( '' == $mixnames ) {
    $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $rand_keys = array_rand($input, 2);
    $mixnames = $input[$rand_keys[1]];
    update_post_meta( $post->ID, 'fieldnames', $mixnames );
  }
  // DISPLAYS THE OUTCOME
  print $mixnames;
}