dragon2025 2014-11-22 17:41
浏览 47

循环和清除数组

I have arrays within arrays, all with varying amounts of information. My CSV table currently has the fields Name, Email, and Phone Number. Below is my array;

Array
(
[0] => Array
    (
        [0] => Name
        [1] => Email
        [2] => Phone Number
    )

[1] => Array
    (
        [0] => Mick
        [1] => mick@mick.com
        [2] => 01234 324234
    )

[2] => Array
    (
        [0] => james
        [1] => james@james.com
        [2] => 
    )

[3] => Array
    (
        [0] => reg
        [1] => reg@reg.com
        [2] => 10293 467289
    )

)

I wish to loop through and remove these null values and combine the Email and Phone Number into Info end up with an array which resembles

Array
(
[0] => Array
    (
        [0] => Name
        [1] => Info

    )

[1] => Array
    (
        [0] => Mick
        [1] => mick@mick.com + 01234 324234
    )

[2] => Array
    (
        [0] => james
        [1] => james@james.com
    )

[3] => Array
    (
        [0] => reg
        [1] => reg@reg.com + 10293 467289
    )

)

Here is my current script, I am recienving the error;

<b>Warning</b>:  array_filter() expects parameter 2 to be a valid callback, no array or string given in <b>C:\Users\Lydia\Documents\XAMPP\htdocs\CSV.php</b> on line <b>21</b><br />

every time that I loop through the changeRow() function, any help greatly appreciated

index.php

<?php
include 'CSV.php';
header('Content-type: text/plain');

$file = read_csv('Book1.csv');

$input = changeRow($file);

CSV.php .... ....

    function changeRow($rows){

    $len = count($rows);

    for($i = 0; $i < $len; $i++){

        $rows = array_filter($rows[0],0);



    }


}
  • 写回答

2条回答 默认 最新

  • dongzhong2018 2014-11-22 17:43
    关注

    Can use array_map() instead of foreach(). Example:

    $file = read_csv('Book1.csv');
    $input = array_map(function($v){
        $phone = (isset($v[2]) && $v[2]) ? ' + '. $v[2] : '';
        return array($v[0], $phone);
    },$file );
    if(isset($result[0][1])) $result[0][1] = 'Info';
    
    print '<pre>';
    print_r($input);
    print '</pre>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line