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

时间:2016-03-30 14:33:30

标签: php arrays wordpress function

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

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

这个函数随机播放数字,

`

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

`

我需要一个条件来打印或不在帖子上的数字,@ leglesslizard用户帮助了我。它工作得很好,最大的问题是每次新访问帖子,或刷新木棉,都会产生一个新的数字/值,而且这种情况不可能发生,我需要永久地写下这些帖子及其下面的值解决方案但不能应用于数组!

`

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

`

但是单词会随着条件不存在而改变。每个新的访问词都被更改,它们实际上并没有写在帖子中

1 个答案:

答案 0 :(得分:0)

这是用户@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;
}