WQP 360 Posted June 20, 2014 Author Report Share Posted June 20, 2014 @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]; Quote Link to post Share on other sites
ethoz 9 Posted June 20, 2014 Report Share Posted June 20, 2014 При таком раскладе: $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) Quote Link to post Share on other sites
DeimosFobos 34 Posted June 20, 2014 Report Share Posted June 20, 2014 ethoz, зачем вы страдаете херней в виде?: unset($numbers[array_search($unset, array_keys($numbers))]); $numbers = array_values($numbers); если достаточно сделать unset($numbers[$unset]) до shuffle Quote Link to post Share on other sites
ethoz 9 Posted June 20, 2014 Report Share Posted June 20, 2014 @DeimosFobos, сейчас всмотрелся повнимательней, вы абсолютно правы. Quote Link to post Share on other sites
WQP 360 Posted June 21, 2014 Author Report Share Posted June 21, 2014 С этим разобрался. Но есть ещё один вопрос: Как удалить ячейку массива зная её значение. Пример нужно удалить Arry[1], но известно только ID->2: Array ( [0] => WP_Post Object ( [ID] => 1 ... ) [1] => WP_Post Object ( [ID] => 2 ... ) .... [999] => WP_Post Object ( [ID] => 999 ... ) ) Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.