简体   繁体   中英

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.

I did a simple 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.

$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 )

<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();

After, I was getting my number of participation, but on the second for() I was using sizeof(). But this function only apply to arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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