php如何将数组推送到数组数组

时间:2016-06-24 19:39:46

标签: php arrays

使用以下代码:

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var lbResult: UILabel!
    @IBOutlet weak var tfWrite: UITextField!

    let dict = ["cocktail":"bar"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tfWrite.delegate = self;
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
    {
        let range2 = Range<String.CharacterView.Index>(start: textField.text!.startIndex.advancedBy(range.location),end: textField.text!.startIndex.advancedBy(range.location + range.length))
        let candidateString = textField.text?.stringByReplacingCharactersInRange(range2, withString: string)
        //let bb = Range<String.CharacterView.Index>(start: range.location,end: range.location + range.length)

        var finalText = ""

        let components = candidateString?.componentsSeparatedByString(" ")
        for word in components! {
            if(dict[word] != nil)
            {
                finalText += " " + dict[word]!;
            }else
            {
                finalText += " " + word;
            }
        }


        self.lbResult.text = finalText

        return true;
    }


}

和$ incY [$ i] =

 $masterInclude[$cnt]  = $incX[$j];           
 array_push($masterInclude[$cnt],$incY[$i]);

$cnt = 63;
$incX[$j]=

[0] => Array
        (
            [date] => 2016-04-28
            [usr] => 1
        )
    [1] => Array
        (
            [date] => 2016-05-15
            [usr] => 1
        )
    [2] => Array
        (
            [date] => 2016-06-20
            [usr] => 1
        ) 

我得到以下输出:

[0] => Array
                (
                    [date] => 2016-05-15
                    [usr] => 90
                )      

但想得到:

[63] => Array
    (
        [0] => Array
            (
                [date] => 2016-04-28
                [usr] => 1
            )
        [1] => Array
            (
                [date] => 2016-05-15
                [usr] => 1
            )
        [2] => Array
            (
                [date] => 2016-06-20
                [usr] => 1
            ) 
                [0] => Array
                    (
                        [date] => 2016-05-15
                        [usr] => 90
                    )                   
            )

    )

    )

Anant建议使用:

[63] => Array
(
    [0] => Array
        (
            [date] => 2016-04-28
            [usr] => 1
        )
    [1] => Array
        (
            [date] => 2016-05-15
            [usr] => 1
        )
    [2] => Array
        (
            [date] => 2016-06-20
            [usr] => 1
        ) 
    [3] => Array
        (
            [date] => 2016-05-15
            [usr] => 90
        )    


  )
 )

但这会产生:

$incX[$j][]=$incY[$i];

2 个答案:

答案 0 :(得分:1)

使用array_merge

array_push($masterInclude,$incY[$i]);

在没有其余代码循环的情况下,无法确定。

答案 1 :(得分:-1)

你的$ incX [$ j]是一个数组数组: 阵列(   [0] =&gt;排列         (             [date] =&gt; 2016年4月28日             [usr] =&gt; 1         )   [1] =&gt;排列         (             [date] =&gt; 2016年5月15日             [usr] =&gt; 1         )   [2] =&gt;排列         (             [date] =&gt; 2016年6月20日             [usr] =&gt; 1         ) )

所以我的建议是使用array_push对抗$ incX [$ j],如下所示: array_push($ incX [$ j],$ incY [$ i])