在for或foreach循环中回显多维数组字段

时间:2021-06-24 21:31:29

I built a multidimensional array to store some values, and I was able to remove duplicates.

我构建了一个多维数组来存储一些值,我能够删除重复项。

What I want to do now is build a for or foreach loop to echo the subarray values by key, like [0], [1], etc because it seems to have put them in nice rows for me. Print_r shows the data exactly as I need it, but I need to iterate through to build some form buttons for each set.

我现在要做的是构建一个for或foreach循环来按键回放子数组值,如[0],[1]等,因为它似乎已经把它们放在了很好的行中。 Print_r完全按照我的需要显示数据,但我需要迭代为每个集合构建一些表单按钮。

I use a function I found to generate unique entries from multidimensional arrays, as follows:

我使用我发现的函数从多维数组生成唯一条目,如下所示:

function multi_unique($array) {
    foreach ($array as $k=>$na)
        $new[$k] = serialize($na);
    $uniq = array_unique($new);
    foreach($uniq as $k=>$ser)
        $new1[$k] = unserialize($ser);
    return ($new1);
}

Initializing the array:

初始化数组:

$unique_array = array(
    'username'=>array(),
    'user_id'=>array(),
    'weeknumber'=>array()
    );

Building the array from within a while loop:

从while循环中构建数组:

while($row = mysql_fetch_array($query))
{
$unique_array[] = array('username'=>$row['username'], 'user_id'=>$row['user_id'], 'weeknumber'=>$row['weeknumber']);
}

And finally, I need to make sure the array values are unique (there are duplicates entries as a result of database and query limitations), after the while loop I have:

最后,我需要确保数组值是唯一的(由于数据库和查询限制存在重复条目),在while循环后我有:

print_r(multi_unique($unique_array));

Which produces output like this on the page with print_r, generated dynamically from the database with duplicate entries removed:

这会在带有print_r的页面上生成这样的输出,这是从数据库动态生成的,删除了重复的条目:

Array ( [username] => Array ( ) [0] => Array ( [username] => username1 [user_id] => 58 [weeknumber] => 1 ) [1] => Array ( [username] => username2 [user_id] => 21 [weeknumber] => 1 ) [3] => Array ( [username] => username3 [user_id] => 24 [weeknumber] => 1 )

Like I said, everything goes into the array swimmingly, I just need a good way to pull everything out once again, I need the username, user_id, and weeknumber to echo within a loop.

就像我说的那样,一切都在游戏中进入阵列,我只需要一个很好的方法再次拉出所有东西,我需要用户名,user_id和weeknumber来循环回调。

Any help is appreciated!

任何帮助表示赞赏!

And yes, I know mysql is deprecated, but it's too late in the game to change that now + I'm working off existing code by someone else.

是的,我知道mysql已经被弃用了,但是现在改变它已经太晚了+我正在使用别人的现有代码。

2 个解决方案

#1


2  

You can just loop through the array and each iteration of the loop will be the next array in the unique_array

您可以循环遍历数组,循环的每次迭代都将成为unique_array中的下一个数组

foreach($unique_array as $row){
   // Format this as desired
   echo $row['username'] . $row['user_id'] . $row['weeknumber'];
}

#2


0  

Your initialization code is incorrect, it should be:

您的初始化代码不正确,它应该是:

$unique_array = array();

To pull everything out, use:

为了解决所有问题,请使用:

foreach ($unique_array as $user) {
  echo "Username: " . $user['username'] . "\n";
  echo "UserID: " . $user['user_id'] . "\n";
  echo "Week Number: " . $user['weeknumber'] . "\n";
  echo "\n";
}

#1


2  

You can just loop through the array and each iteration of the loop will be the next array in the unique_array

您可以循环遍历数组,循环的每次迭代都将成为unique_array中的下一个数组

foreach($unique_array as $row){
   // Format this as desired
   echo $row['username'] . $row['user_id'] . $row['weeknumber'];
}

#2


0  

Your initialization code is incorrect, it should be:

您的初始化代码不正确,它应该是:

$unique_array = array();

To pull everything out, use:

为了解决所有问题,请使用:

foreach ($unique_array as $user) {
  echo "Username: " . $user['username'] . "\n";
  echo "UserID: " . $user['user_id'] . "\n";
  echo "Week Number: " . $user['weeknumber'] . "\n";
  echo "\n";
}