【发布时间】:2017-04-11 02:16:28
【问题描述】:
我是 php 中的多维数组的新手,我阅读了这个 SO answer 并尝试创建我的二维数组,但如何输出它?
$nPost = array("orange, table");
$count_values = array("fruit, forniture");
$final_array = array(array($count_values), array($nPost));
输出必须是:
Fruits: orange, Forniture: table
试过
print_r($final_array);
但我明白了
Array ( [0] => Array ( [0] => Array ( [0] => fruit, forniture ) ) [1] => Array ( [0] => Array ( [0] => orange, table ) ) )
0 fruit, forniture
更新
现实生活中的完整代码是(代码 cmets 中的解释):
<?php
$stack = array();
$userID = array();
$nPost = array();
$blogusers = get_users( 'orderby=nicename&role=author' );
foreach ( $blogusers as $user ) {
// get the language list for each user, and push to array
$descTokens = explode(',', $user->user_description);
$stack = array_merge($stack, $descTokens);
// get the ID for each user, and push to the array
// get the number of posts for each user ID and push to array
$the_user_id = $user->ID;
$numPosts = count_user_posts( $the_user_id );
array_push($userID, $the_user_id);
array_push($nPost, $numPosts);
}
// get the count for each language by counting the duplicate strings
$count_values = array();
foreach ($stack as $a) {
@$count_values[$a]++;
}
$total_duplicates = 0;
foreach ($count_values as $a) {
if($count_values[$a]<=1){
unset($count_values[$a]);
} else{
$total_duplicates += $count_values[$a];
}
}
for($i = 0; $i < count($count_values); $i++){
$final_array[$count_values[$i]] = $nPost[$i];
}
foreach($final_array as $label => $item){
echo "$label: $item, ";
}
?>
// This gives me a correct result but not the n. posts
<ul>
<?php
foreach ($count_values as $key=>$count) {
echo '<li>'.$key.' '.$count.'</li>';
}
?>
</ul>
我们正在努力实现的是:
- 1 个法语,2 个帖子
- 3 英语,5 篇文章
【问题讨论】:
-
Yo dawg! 对于初学者:
$final_array = array($count_values, $nPost);这可能仍然不是你想要的。 -
@mkaatman 哈哈@模因。可能不是我想要的,但这是一个尝试,你的镜头是什么? :D
-
尝试类似:
$things['fruit'] = 'orange';$things['furniture'] = 'table';print_r($things); -
你像一个没有键的字符串(“橙色,表格”)一样发送水果和橙色,而 php 给出了默认的数字键,从 0 开始,这就是你得到
[0] => fruit, forniture -
@mkaatman 是的,我们手动为每个值设置了 1 个值,但是如果我们有一个字符串列表(实际上是动态的)呢?
标签: php