0

I have an array that looks like this:

array
  0 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'CAPITL' (length=6)
      'PTID' => string '61757' (length=5)
      'LBMP ($/MWHr)' => string '48.24' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '1.13' (length=4)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
  1 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'CENTRL' (length=6)
      'PTID' => string '61754' (length=5)
      'LBMP ($/MWHr)' => string '47.30' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '0.19' (length=4)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
  2 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'DUNWOD' (length=6)
      'PTID' => string '61760' (length=5)
      'LBMP ($/MWHr)' => string '49.46' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '2.36' (length=4)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
  3 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'GENESE' (length=6)
      'PTID' => string '61753' (length=5)
      'LBMP ($/MWHr)' => string '46.50' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '-0.61' (length=5)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
  4 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'H Q' (length=3)
      'PTID' => string '61844' (length=5)
      'LBMP ($/MWHr)' => string '42.00' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '-0.04' (length=5)
      'Marginal Cost Congestion ($/MWHr)' => string '0.31' (length=4)
  5 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'HUD VL' (length=6)
      'PTID' => string '61758' (length=5)
      'LBMP ($/MWHr)' => string '49.46' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '2.36' (length=4)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
  6 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'LONGIL' (length=6)
      'PTID' => string '61762' (length=5)
      'LBMP ($/MWHr)' => string '50.88' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '3.77' (length=4)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
  7 => 
    array
      'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
      'Name' => string 'MHK VL' (length=6)
      'PTID' => string '61756' (length=5)
      'LBMP ($/MWHr)' => string '48.66' (length=5)
      'Marginal Cost Losses ($/MWHr)' => string '1.55' (length=4)
      'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)

It comes from a CSV that has around 3000 lines, the thing is i only need parts of this array.
What i ultimately want to do is to write a function to remove parts of array by value, such as removing all Array's containing the Name 'MHK VL' as shown array[7][name].

If someone could point me in the correct direction in terms of what functions i should be using to do this, that would be great.

Thank you,

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by cereal
0

The simplest way, for me, is to use array_filter and preg_match, set null on the array you want to remove and then filter the array, at the end reindex, otherwise you get discontinuous keys:

<?php
$a = array(
	array('name' => 'MHK VL', 'PTID' => 'bab'),
	array('name' => 'nbn', 'PTID' => 'bbb'),
	array('name' => 'ncn', 'PTID' => 'bcb'),
	array('name' => 'MHK VL', 'PTID' => 'bab'),
	array('name' => 'MHKVL', 'PTID' => 'bbb'),
	array('name' => 'ncn', 'PTID' => 'bcb'),
);

$c = count($a);
for($i = 0; $i < $c; $i++)
{
    if(preg_match('/^MHK VL$/i',$a[$i]['name']))
    {
	$a[$i] = null;
    }
}

$filtered = array_filter($a);
#print_r($filtered); # uncomment to check discontinuous keys

$result = array();
foreach($filtered as $key => $value)
{
	$result[] = $value;
}

print_r($result); # reindexed array
?>

bye :)

Edited by cereal: n/a

0

Cereal,

You sir are amazing, you have saved me countless time. I've searched hours looking for a solution to this issue.

now my array appears as:

array
0 =>
array
'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
'Name' => string 'DUNWOD' (length=6)
'PTID' => string '61760' (length=5)
'LBMP ($/MWHr)' => string '49.46' (length=5)
'Marginal Cost Losses ($/MWHr)' => string '2.36' (length=4)
'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
1 =>
array
'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
'Name' => string 'HUD VL' (length=6)
'PTID' => string '61758' (length=5)
'LBMP ($/MWHr)' => string '49.46' (length=5)
'Marginal Cost Losses ($/MWHr)' => string '2.36' (length=4)
'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
2 =>
array
'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
'Name' => string 'MILLWD' (length=6)
'PTID' => string '61759' (length=5)
'LBMP ($/MWHr)' => string '49.46' (length=5)
'Marginal Cost Losses ($/MWHr)' => string '2.36' (length=4)
'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
3 =>
array
'Time Stamp' => string '11/22/2011 00:05:00' (length=19)
'Name' => string 'N.Y.C.' (length=6)
'PTID' => string '61761' (length=5)
'LBMP ($/MWHr)' => string '49.56' (length=5)
'Marginal Cost Losses ($/MWHr)' => string '2.45' (length=4)
'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
4 =>
array
'Time Stamp' => string '11/22/2011 00:10:00' (length=19)
'Name' => string 'DUNWOD' (length=6)
'PTID' => string '61760' (length=5)
'LBMP ($/MWHr)' => string '50.12' (length=5)
'Marginal Cost Losses ($/MWHr)' => string '2.20' (length=4)
'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
5 =>
array
'Time Stamp' => string '11/22/2011 00:10:00' (length=19)
'Name' => string 'HUD VL' (length=6)
'PTID' => string '61758' (length=5)
'LBMP ($/MWHr)' => string '50.26' (length=5)
'Marginal Cost Losses ($/MWHr)' => string '2.35' (length=4)
'Marginal Cost Congestion ($/MWHr)' => string '0.00' (length=4)
by preg matching all desired values:

if (preg_match("/(CAPITL|CENTRL|GENESE|H Q|LONG|MHK VL|NORTH|NPX|O H|PJM|WEST)/i",$data[$i]['Name']))

Anyway, just wanted to say thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.