0

I want to delete the element containing the value 2000 Outside (for example, Eg1,Eg2 and Eg3), This might help: each value loop can have many values.
After remove "2000 Outside" only top record data will display. Eg1 2000 Monday, Eg3 2000 Friday. I'm looking for the simplest function to perform this task please.

Eg1
Postcode Day
2000    Monday
2000   Outside
2000   Friday
2000   Sunday
2000   wednesday

Eg2
Postcode Day
2000    Outside
2000   Monday
2000   Friday
2000   Sunday
2000   wednesday

Eg3
Postcode Day
2000    Outside
2000   Outside
2000   Friday
2000   Sunday
2000   wednesday

foreach($Items as $item)
{

echo $item->PostCode. " ".$item->Day.'<br/>'."\n";

}
Toumash
  • 1,077
  • 11
  • 27
Rob
  • 193
  • 5
  • 18

3 Answers3

2

I think you can use this :

foreach($Items as $key=>$item)
{
    if($item->Postcode == 2000 && $item->Day == "Outside") {
          array_splice($Items,$key,1);
    }
}

echo $Items[0]->Postcode . " " . $Items[0]->Day;
dlegall
  • 412
  • 2
  • 5
  • This one not working, all empty – Rob Jul 03 '15 at 10:58
  • Strange, I just tested this and it seems to work. Are you sure $Items is empty after passing to the loop ? – dlegall Jul 03 '15 at 11:07
  • If I add echo $Items[0]->Postcode . " " . $Items[0]->Day; it'll come Fatal error: Cannot use object of type stdClass as array in – Rob Jul 03 '15 at 11:09
  • Can you print_r($Items) and print it here as a comment or in your initial question? I don't get why $Items isn't an array there. – dlegall Jul 03 '15 at 11:13
  • Only single record will display post code 2029 has only one data record. Array ( [0] => stdClass Object ( [SubName] => ROSE BAY [PostCode] => 2029 [DeliveryDay] => Tuesday [PeriodType] => weekly [AuState] => NSW – Rob Jul 03 '15 at 11:21
  • Post code 2000 has multi records. not print anything print_r($Items) – Rob Jul 03 '15 at 11:27
  • I forgotten comment break point. array_splice($Locations,$key,1); //break; But now display all records list. I want only display one record. Eg3 2000 Friday. – Rob Jul 03 '15 at 11:56
  • I imagine you're putting `echo $Items[0]->Postcode . " " . $Items[0]->Day;` inside the `foreach` loop. It needs to be after the loop has been closed. – JoeP Jul 03 '15 at 12:08
  • Any why would you need to break? That will just remove the first instance which it matched, so in Eg3 you would still have 2000 Outside as the first result unless you ran the `foreach` twice – JoeP Jul 03 '15 at 12:15
  • I have already removed break. but still display all list. What do I have change your code? – Rob Jul 03 '15 at 13:14
  • Can you tell me, this line number 1 without hardcode, how can I add dynamic value? array_splice($Locations,$key,1); If you convert dynamic value it'll work. – Rob Jul 04 '15 at 00:41
  • What do you mean by adding dynamic value? – dlegall Jul 04 '15 at 00:43
  • because number 1 hardcode, if array has 2nd valves in 'Outside' it'll display only "Outside". Are there any way to loop array and found "Outside" (maybe one or more "Outside") after display first record value day(Monday or Friday). Therefore no need to display "Outside". For example some postcode has many records. 1 to 6 value is "Outside", 7 valve is Monday. When I change number 7, array_splice($Locations,$key,7); it'll display Monday – Rob Jul 04 '15 at 06:18
  • It's not working like you think it is. This code loop on your array, and each time it match an Outside, it deletes it. The 1 is only here to delete only 1 element, so each time there is an Outside value in your array, it deletes it. – dlegall Jul 05 '15 at 09:45
  • Yes, that is what I need. but your code multiple record in array 'Outside' not delete only delete first record. If array has second value 'Outside' it won't delete. only delete array has first record value came 'Outside; – Rob Jul 06 '15 at 11:47
  • Can you tell me your php code, first 3 valves are 'Outside' 4th valve is on Monday. Why value is display using $key,1? If I used your code it'll display 'Outside' If change your code $key,3. It will display on Monday. Why was that? ' – Rob Jul 06 '15 at 12:19
  • Did you let the "break;" I removed some edit before? Using the actual code, every occurences of "2000 Outside" are removed. – dlegall Jul 06 '15 at 14:48
  • This is print array data 2000 Outside Area 2000 Outside Area 2000 Outside Area 2000 Tuesday If I change to your code number 3, array_splice($Items,$key,3); will display Tuesday. Why was that? – Rob Jul 07 '15 at 00:27
  • What array_splice is returned is not important. array_splice delete an element from an array. What you asked is to delete all elements that match 2000 Outside from the array, and display the first element after the deletion. array_splice is deleting each elements that is 2000 Outside. Then you have just to print the first element of the array after the loop. – dlegall Jul 07 '15 at 21:19
1

A slight adjustment to dlegall's answer as I was having a bit of trouble getting it to work on my local server. I've included the array used too.

$Items[] = array("Postcode" => 2000, "Day" => "Outside");
$Items[] = array("Postcode" => 2000, "Day" => "Monday");
$Items[] = array("Postcode" => 2000, "Day" => "Friday");

foreach($Items as $key => $item)
    {
        if($item["Postcode"] == 2000 && $item["Day"] == "Outside") {
              array_splice($Items,$key,1);
        }
    }

echo $Items[0]["Postcode"] . " " . $Items[0]["Day"];
JoeP
  • 856
  • 4
  • 15
  • 29
  • This is my input and output details $Items[] = array("Postcode" => 2000, "Day" => "Outside","Day" => "Monday","Day" => "Friday",,"Day" => "Monday"); Result should display Postcode 2000 display first record Monday $Items[] = array("Postcode" => 2002, "Day" => "Outside","Day" => "Outside","Day" => "Friday",,"Day" => "Monday"); Result should display Postcode 2002 display second record Friday – Rob Jul 04 '15 at 00:24
  • Can you tell me, this line number 1 without hardcode, how can I add dynamic value? array_splice($Locations,$key,1); If you convert dynamic value it'll work. – Rob Jul 04 '15 at 00:40
  • what do you mean by dynamic value? his code is already dynamic enough, you can add any value to `$Items` by calling `$Items[] = array('whatever value you want', 'with whatever format');` – am05mhz Jul 04 '15 at 01:29
  • I want only number 1 convert to dynamic valve. array_splice($Items,$key,1); for example a=0; [a] – Rob Jul 04 '15 at 02:58
  • because number 1 hardcode, if array has 2nd valves in 'Outside' it'll display only "Outside". Are there any way to loop array and found "Outside" (maybe one or more "Outside") after display first record value day(Monday or Friday). Therefore no need to display "Outside". – Rob Jul 04 '15 at 03:05
  • For example some postcode has many records. 1 to 6 value is "Outside", 7 valve is Monday. When I change number 7, array_splice($Locations,$key,7); it'll display Monday – Rob Jul 04 '15 at 03:11
  • I'm sorry but I think there is a language barrier here. Please edit your code and show us EXACTLY how you are making your array, and EXACTLY what you want to do with it. – JoeP Jul 06 '15 at 09:25
  • Can you tell me your php code, first 3 valves are 'Outside' 4th and fourth valve is on Monday. Which value is display using $key,1? If I used your code it'll display 'Outside' If change your code $key,3. It will display on Monday. Why was that? – Rob Jul 06 '15 at 12:34
  • Eg. $Items[] = array("Postcode" => 2000, "Day" => "Outside", "Day" => "Outside", "Day" => "Outside", "Day" => "Monday", "Day" => "Friday"); I need only display first day array value is on Monday. – Rob Jul 06 '15 at 12:38
0

Can't we use the below code ?

foreach($array as $element){
 if(($element->postcode == '2000') && ($element->variable == 'Outside')){
  unset($element);
 }
}
Tismon Varghese
  • 849
  • 1
  • 6
  • 17
  • variable == '2000' ? or variable == 'Outside'? – Rob Jul 03 '15 at 10:42
  • After remove "2000 Outside" how do I display only top record data . Eg1 2000 Monday, Eg3 2000 Friday – Rob Jul 03 '15 at 10:50
  • 1
    Just to remind, unset() doesn't reorganize indexes of your array, if the array isn't associative. It can lead to errors if that's not specified. – dlegall Jul 03 '15 at 10:57