Jump to content
Sign in to follow this  
WQP

Вывод случайного числа php

Recommended Posts

@ethoz, начальный код:

$query_random_news = new WP_Query();

$query_random_all = $query_random_news->query(array('post_type' => 'page','post_parent' => 33));
$query_random_count = count($query_random_all);
				
function getFourRandomNumber ($maxCount){
$numbers = range(0, $maxCount);
			
				
shuffle($numbers);
return array($numbers[0], $numbers[1]);
}

$arr_all = getFourRandomNumber($query_random_count);
$news_1 = $arr_all[1];
$news_2 = $arr_all[0];

@ethoz, ваш код выводит:

Array ( [0] => 0 [1] => 4 )

При:

$query_random_news = new WP_Query();

$query_random_all = $query_random_news->query(array('post_type' => 'page','post_parent' => 33));
$query_random_count = count($query_random_all);
				
function getFourRandomNumber ($maxCount,$unset){
    $numbers = range(0, $maxCount);
    shuffle($numbers);

    unset($numbers[array_search($unset, array_keys($numbers))]);
    $numbers = array_values($numbers);

    return array($numbers[0], $numbers[1]);
}

$arr_all = getFourRandomNumber($query_random_count,0);
$news_1 = $arr_all[1];
$news_2 = $arr_all[0];

Share this post


Link to post
Share on other sites

При таком раскладе:
 

$arr_all = getFourRandomNumber($query_random_count,0);

 

Никак не может быть Array ( [0] => 0 [1] => 4 ), т.к. $unset = 0 и он будет удалён из массива $numbers.

 

 

print_r($numbers);

 

Array
(
    [0] => 6
    [1] => 4
    [2] => 1
    [3] => 9
    [4] => 7
    [5] => 8
    [6] => 3
    [7] => 10
    [8] => 2
)

 

 

print_r(getFourRandomNumber(10,0));

 

Array
(
    [0] => 6
    [1] => 4
)
 

Share this post


Link to post
Share on other sites

ethoz, зачем вы страдаете херней в виде?:


    unset($numbers[array_search($unset, array_keys($numbers))]);
    $numbers = array_values($numbers);

если достаточно сделать unset($numbers[$unset]) до shuffle

Share this post


Link to post
Share on other sites

С этим разобрался. Но есть ещё один вопрос: Как удалить ячейку массива зная её значение. 

 

Пример нужно удалить Arry[1], но известно только ID->2:

Array
(
    [0] => WP_Post Object
        (
            [ID] => 1
            ...
        )
    [1] => WP_Post Object
        (
            [ID] => 2
            ...
        )
....
    [999] => WP_Post Object
        (
            [ID] => 999
            ...
        )
)

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...