0

I have the following array imported from a CSV:

Array ( [0] => Array ( [0] => QUARTERLY STATS;Q1/2011;Q2/2011;Q3/2011;Q4/2011;Q1/2012;Q2/2012;Q3/2012;Q4/2012;Q1/2013;Q2/2013;Q3/2013;Q4/2013;Q1/2014 )

[1] => Array
    (
        [0] => January;7500;8800;9500;10000;10500;11000;11500;12000;12500;13000;13420;13820;14200
    )

[2] => Array
    (
        [0] => ;;17
        [1] => 30%;8%;5
        [2] => 30%;5%;4
        [3] => 80%;4
        [4] => 50%;4
        [5] => 30%;4
        [6] => 20%;4%;3
        [7] => 20%;3%;2
        [8] => 70%
    )

[3] => Array
    (
        [0] => TOTAL;7500;8500;9500;11000;12500;11400;11800;13000;12500;13000;13420;13820;14200
    )

[4] => Array
    (
        [0] => ;;17
        [1] => 30%;7
        [2] => 95%;5
        [3] => 26%;5%;4
        [4] => 76%;4
        [5] => 55%;4
        [6] => 35%;4
        [7] => 17%;4%;3
        [8] => 23%;2
        [9] => 98%;2
        [10] => 75%
    )

So,

I would like to get rid of all arrays containing "% and TOTAL".

I thought to loop through and unset the matching case:

$remove ="TOTAL"; foreach ($csv as $key => $value){

if (in_array($remove,$value[$key])){
unset($value[$key]);

} }

This is the error I got: Warning: in_array() expects parameter 2 to be array, null given

My PHP Version 5.3.10

Would you do it that way or would you use the array_filter?

I am browsing since 2 hours the forum but I could not find any hint helping me out.

Cheers.

3 Answers 3

1

You can try by preg_replace for removing TOTAL & %. If you want to remove the element from array then use unset & finally use array_filter for removing null elements.

$newArr = array();
foreach($arr as $key=>$value){
    foreach($value as $k=>$v){
        $newArr[$key][$k] = preg_replace('/(TOTAL)|(%)/', '', $v); //for removing TOTAL & %
        unset($arr[$key][$k]); //for unset the array elements that contains TOTAL & %
    }
}

//Output by replacement
print '<pre>';
print_r($newArr);
print '</pre>';

//output after using unset
print '<pre>';
print_r(array_filter($arr));
print '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

Warning: array_filter() expects parameter 1 to be array, null given in which is in print_r(array_filter($arr)); $newArr = $csv; foreach($arr as $key=>$value){ foreach($value as $k=>$v){ $newArr[$key][$k] = preg_replace('/(TOTAL)|(%)/', '', $v); //for removing TOTAL & % unset($arr[$key][$k]); //for unset the array elements that contains TOTAL & % } } //Output by replacement print '<pre>'; print_r($newArr); print '</pre>'; //output after using unset print '<pre>'; print_r(array_filter($arr)); print '</pre>'; Do I have some PHP Version issue? My Version is 5.3.0
I think you misunderstood. $arr is original array. Assign $arr after reading the CSV file. array_filter() need an array. May be you given a null value.
0

In your case:

foreach ($csv as $subArray)
{
    for ($i = 0, $len = count($subArray); $i < $len; $i++)
    {
        if (strpos($subArray[$i], $remove) !== false)
            unset($subArray[$i])
    }
}

Comments:

  1. strict comparasion for strpos, if we use !=, then 0 position would be equals to false.
  2. inner loop is "for-loop" bevause it's better to avoid changing content of array inside foreach.

Comments

0
$arr[][] = ("QUARTERLY STATS;Q1/2011;Q2/2011;Q3/2011;Q4/2011;Q1/2012;Q2/2012;Q3/2012;Q4/2012;Q1/2013;Q2/2013;Q3/2013;Q4/2013;Q1/2014");
$arr[][] = ("January;7500;8800;9500;10000;10500;11000;11500;12000;12500;13000;13420;13820;14200");
$arr[] = array(";;17","30%;8%;5","30%;5%;4","80%;4","50%;4","30%;4","20%;4%;3","20%;3%;2","70%");
$arr[][] = ("TOTAL;7500;8500;9500;11000;12500;11400;11800;13000;12500;13000;13420;13820;14200");
$arr[] = array("30%;7","95%;5","26%;5%;4","76%;4","55%;4","35%;4","17%;4%;3","23%;2","98%;2","75%");

$newArr = array();
    foreach($arr as $key=>$value) {
        foreach($value as $k=>$v) {
        $newArr[$key][$k] = preg_replace('/(TOTAL)|(%)/', '', $v); //for removing TOTAL & %
        unset($arr[$key][$k]); //for unset the array elements that contains TOTAL & %
    }
    unset($arr[$key]); // IT does not unset ARR[2] ARR[3] and ARR[4] containing TOTAL & %
}

Output by replacement

print '<pre>';
print_r($newArr);
print '</pre>';

Output after using unset

print '<pre>';
print_r(array_filter($arr));
print '</pre>';

I did create exactly the same ARRAY as imported from CSV. unset does not unset ARRAY2,3,4 which is containing TOTAL AND %. unset does not unset ARRAY2,3,4 which is containing TOTAL AND %.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.