dongyi7513 2015-01-12 10:40 采纳率: 100%
浏览 9

将新数组附加到现有数组

I am using Codeigniter Sessions to build a "Betslip".

I am adding the team name and odds to the bet slip and then plan to loop through each "bet" element to create a betslip.

The ideal array needs to look like :

 [betslip] => Array
        (
            [bet] => Array
                (
                    [team] => Rayo Vallecano
                    [odds] => 67/100
                )
            [bet] => Array
                (
                    [team] => Elche
                    [odds] => 1/100
                )
        )

However in my code, I just seem to be overwriting what is already there.

My current PHP code is as follows :

// Get Team Name
    $teamname = $this->uri->segment(3);

    // Get Odds
    $odds1 = $this->uri->segment(4);
    $odds2 = $this->uri->segment(5);

    $odds = $odds1;
    $odds .= "/";
    $odds .= $odds2;

    // Build An array titled Bet
    $bet = array(
        'bet' => array(
            'team'  =>  urldecode($teamname),
            'odds'  =>  $odds
        )
    );

    $betslip = $this->session->userdata('betslip');

    // Create The Betslip For The First Time...
    if(empty($betslip))
    {
        $this->session->set_userdata('betslip', $bet);
    }
    else
    {
        // Add To The Betslip Array...
        $betslip['bet'] = array(
            'team'  =>  urldecode($teamname),
            'odds'  =>  $odds
        );

        $this->session->set_userdata('betslip', $betslip);
    }

How do I just append a bet to the existing bet slip array?

Is it possible to have multiple array keys with the same name too?

Thanks in advance..

  • 写回答

1条回答 默认 最新

  • dsbfbz75185 2015-01-12 10:46
    关注

    This is not an ideal array

    [betslip] => Array
            (
                [bet] => Array
                    (
                        [team] => Rayo Vallecano
                        [odds] => 67/100
                    )
                [bet] => Array
                    (
                        [team] => Elche
                        [odds] => 1/100
                    )
            )
    

    it should be like

    [betslip] => Array
            (
                [0] => Array
                    (
                        [team] => Rayo Vallecano
                        [odds] => 67/100
                    )
                [1] => Array
                    (
                        [team] => Elche
                        [odds] => 1/100
                    )
            )
    

    <?php
    $item = array();
    
    $item2 = array(
        'team' => 1,
        'odds' => "1/100"
    );
    
    
    
    for ($x = 0; $x <= 10; $x++) {
        $item[] = $item2;
    }
    
    print_r($item);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?