从大型数组中读取

时间:2016-06-30 07:30:44

标签: php arrays

我想问一下如何从关联数组中读取:

Array
(
    [0] => Array
        (
            [imie;nazwisko;telefon] => 3;email@gmail.com;123456789
        )

    [1] => Array
        (
            [imie;nazwisko;telefon] => 6;email@gmail.com;123456789
        )

    [2] => Array
        (
            [imie;nazwisko;telefon] => 7;email@gmail.com;123456789
        )

    [3] => Array
        (
            [imie;nazwisko;telefon] => 16;email@gmail.com;123456789
        )

    [4] => Array
        (
            [imie;nazwisko;telefon] => 17;email@gmail.com;123456789
        )

    [5] => Array
        (
            [imie;nazwisko;telefon] => 19;email@gmail.com;123456789
        )

    [6] => Array
        (
            [imie;nazwisko;telefon] => 32;email@gmail.com;123456789
        )

    [7] => Array
        (
            [imie;nazwisko;telefon] => 39;email@gmail.com;123456789
        )

    [8] => Array
        (
            [imie;nazwisko;telefon] => 50;email@gmail.com;123456789
        )

    [9] => Array
        (
            [imie;nazwisko;telefon] => 52;email@gmail.com;123456789
        )

    [10] => Array
        (
            [imie;nazwisko;telefon] => 54;email@gmail.com;123456789
        )

    [11] => Array
        (
            [imie;nazwisko;telefon] => 720;email@gmail.com;123456789
        )

    [12] => Array
        (
            [imie;nazwisko;telefon] => 54;email@gmail.com;123456789
        )

)

我使用以下代码创建此数组:

$csv = array_map("str_getcsv", file($path,FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
echo '<pre>';
print_r($csv);
echo '</pre>';

如何循环浏览所有项目,然后从每个项目获取:imie,nazwisko和telefon?

2 个答案:

答案 0 :(得分:2)

使用几个循环和explode函数:

foreach($array as $arrIdx => $subArr){
   foreach($subArr as $keys => $values) {
         $key = explode(";", $keys);
         $value = explode(";", $values);

         for($i=0; $i <= 2; $i++){
            echo $key[$i]." : ".$value[$i];
         }
   }
}

答案 1 :(得分:0)

我会改变你的代码:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let view: UIView = UIView(frame: CGRectMake(0.0, 0.0, 320.0, 50.0))

    if section == 0 {  // for your gradient section. it can be any section that you want

       let gradient: CAGradientLayer = CAGradientLayer()
       gradient.frame = view.bounds
       gradient.colors = [UIColor.whiteColor().CGColor, UIColor.blackColor().CGColor]
       view.layer.insertSublayer(gradient, atIndex: 0)

    } else {
       view.backgroundColor = UIColor.lightGrayColor()

    }
    return view
}

这样您就可以获得正确的关联数组。