简体   繁体   English

从数组PHP导出数据

[英]Exporting datas from array PHP

I have a quite simple question for you. 我有一个很简单的问题要问你。

I have an array that looks like this. 我有一个看起来像这样的数组。

array(
(int) 0 => array(
    'id' => '14',
    'user_id' => '39',
    'price_id' => '6',
    'count' => '3',
    'created' => '2015-11-10 16:42:26'
),
(int) 1 => array(
    'id' => '20',
    'user_id' => '47',
    'price_id' => '6',
    'count' => '1',
    'created' => '2015-11-10 18:00:44'
),
(int) 2 => array(
    'id' => '22',
    'user_id' => '45',
    'price_id' => '6',
    'count' => '1',
    'created' => '2015-11-10 18:16:22'
),
(int) 3 => array(
    'id' => '24',
    'user_id' => '35',
    'price_id' => '6',
    'count' => '2',
    'created' => '2015-11-10 18:19:32'
)
);

I need to export this as a new array where I am able to see for exemple 3 times the first one since 'count' is at 3. 我需要将其导出为新数组,因为“ count”为3,因此我可以看到第一个数组的3倍。

I did a simple SELECT * FROM my_table where price_id = 6 ; 我做了一个简单的SELECT * FROM my_table where price_id = 6 ;

But where it start to get messy, is when I try to create 3 times the same array. 但是,当我尝试创建相同数组的3倍时,它开始变得混乱。

$participants = array();

        for ($i=0; $i < sizeof($giveaway) ; $i++) { 

            $participants[] = $giveaway[$i]['Winners'];

            $nbreparticipations = $giveaway[$i]['Winners']['count'];

            for ($j=0; $j < sizeof($nbreparticipations); $j++) {

                $participants[$i][] = $j;
            }

        }

In final, it shouls looks like this ( jsFiddle ) 最后,它应该像这样( jsFiddle

<table border="1">

<tr>
  <td>id</td>
  <td>user_id</td>
</tr>

<tr>
  <td>1</td>
  <td>39</td>
</tr>
<tr>
  <td>2</td>
  <td>39</td>
</tr>
<tr>
  <td>3</td>
  <td>39</td>
</tr>
<tr>
  <td>4</td>
  <td>47</td>
</tr>
<tr>
  <td>5</td>
  <td>45</td>
</tr>
<tr>
  <td>6</td>
  <td>35</td>
</tr>
<tr>
  <td>7</td>
  <td>35</td>
</tr>
</table>

I just saw my error. 我刚刚看到我的错误。

Here is my new code. 这是我的新代码。

$participants = array();
$new_participations = array();

    for ($i=0; $i < sizeof($giveaway) ; $i++) { 

        $participants[] = $giveaway[$i]['Winner'];

        $nbreparticipations = $giveaway[$i]['Winner']['count'];
        $user_id = $giveaway[$i]['Winner']['user_id'];

        for ($j=0; $j < $nbreparticipations; $j++) {

            $new_participations[] = $giveaway[$i]['Winner'];
        }

    }

I started by creating a new array() for the second for(); 我从为第二个for()创建一个新的array()开始;

After, I was getting my number of participation, but on the second for() I was using sizeof(). 之后,我得到了参与人数,但是在第二个for()上,我使用了sizeof()。 But this function only apply to arrays. 但是此功能仅适用于数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM