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";

}
8
  • Just remove they key. But that's an object you have there. Still point stands. Commented Jul 3, 2015 at 10:33
  • Duplication of stackoverflow.com/questions/2008866/… Commented Jul 3, 2015 at 10:38
  • I am not clear "remove they key" Commented Jul 3, 2015 at 10:40
  • how is it example3 result of 2000 Friday, as it is like example2 where 2000 outside is at the top, based on your criteria, example3 should be empty Commented Jul 3, 2015 at 10:53
  • @am05mhz he just wan't to retrieve the first value of array after deleting 2000 Outside element. His examples are correct, no? Commented Jul 3, 2015 at 10:58

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

19 Comments

This one not working, all empty
Strange, I just tested this and it seems to work. Are you sure $Items is empty after passing to the loop ?
If I add echo $Items[0]->Postcode . " " . $Items[0]->Day; it'll come Fatal error: Cannot use object of type stdClass as array in
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.
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
|
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"];

9 Comments

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
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.
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');
I want only number 1 convert to dynamic valve. array_splice($Items,$key,1); for example a=0; [a]
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".
|
0

Can't we use the below code ?

foreach($array as $element){
 if(($element->postcode == '2000') && ($element->variable == 'Outside')){
  unset($element);
 }
}

3 Comments

variable == '2000' ? or variable == 'Outside'?
After remove "2000 Outside" how do I display only top record data . Eg1 2000 Monday, Eg3 2000 Friday
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.