如何解决PHP - 使用特定值作为第一个数组值对合并和混洗的数组进行排序
我有 3 个 11:37:27.064 INFO [Blueprint Event Dispatcher: 1] Successfully logged in.
11:37:27.067 WARN [Blueprint Event Dispatcher: 1] The configuration 'specific.avro.reader' was supplied but isn't a known config.
11:37:27.068 INFO [Blueprint Event Dispatcher: 1] Kafka version: 2.6.0
11:37:27.069 INFO [Blueprint Event Dispatcher: 1] Kafka commitId: 62abe01bee039651
11:37:27.069 INFO [Blueprint Event Dispatcher: 1] Kafka startTimeMs: 1624354647068
11:37:27.070 INFO [Blueprint Event Dispatcher: 1] Route: input-broker-route started and consuming from: kafka://test
11:37:27.070 INFO [Camel (integ-norauto-v1-input) thread #75 - KafkaConsumer[test]] Subscribing test-Thread 0 to topic test
11:37:27.071 INFO [Blueprint Event Dispatcher: 1] Total 1 routes,of which 1 are started
11:37:27.073 INFO [Camel (integ-norauto-v1-input) thread #75 - KafkaConsumer[test]] [Consumer clientId=myClientId,groupId=myGroupId] Subscribed to topic(s): test
11:37:27.073 INFO [Blueprint Event Dispatcher: 1] Apache Camel 3.7.4 (integ-norauto-v1-input) started in 38ms
,我使用 nested arrays
来合并它们,以及使用 array_merge
将它们打乱。
这些是我的数组:
shuffle
我想要实现的是在 $array_1 = [
['product_1_1',300,100],['product_1_2',['product_1_3',['product_1_4',['product_1_5',];
$array_2 = [
['product_2_1',250],['product_2_2',['product_2_3',['product_2_4',['product_2_5',];
$array_3 = [
['product_3_1',500],['product_3_2',['product_3_3',['product_3_4',['product_3_5',];
完成后,它总是首先将其中一个值放在 shuffle
中,以便洗牌后的数组始终以$array_1
中的值之一。
解决方法
这是一种方法。从 array_1 中选择一个随机索引并从数组中删除元素。然后合并所有,合并和洗牌,以 array_unshift 结尾,这将使用我们的随机 array_1 值添加最终数组。
<?php
$array_1 = [
['product_1_1',300,100],['product_1_2',['product_1_3',['product_1_4',['product_1_5',];
$array_2 = [
['product_2_1',250],['product_2_2',['product_2_3',['product_2_4',['product_2_5',];
$array_3 = [
['product_3_1',500],['product_3_2',['product_3_3',['product_3_4',['product_3_5',];
$first = rand(0,floor(count($array_1)));
$firstItem = $array_1[$first];
unset($array_1[$first]);
$merged = array_merge($array_1,$array_2,$array_3);
shuffle($merged);
array_unshift($merged,$firstItem);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。