从数组中获取特定元素

时间:2016-01-23 13:41:26

标签: php arrays

我有一个如下数组,我想从这个数组中获取任何特定元素:数组存储在变量中。

   $results = 

        Array
           (
        [0] => stdClass Object
                         (
        [id] => 1
        [calendar] => 1
        [time] => 2016-01-22 17:25:28
        [booked_time] => 01/22/2016 11:00
        [booked_time_unformatted] => 2016-01-22 11:00:00
        [name] => Shoaib
        [email] => shoaib@techleadz.com
        [phone] => 0987654321
        [question] => 123
        [quantity] => 1
        [buffered_date] => a:9:{s:5:"PRICE";s:2:"25";s:10:"COUPONCODE";s:0:"";s:8:"QUANTITY";N;s:4:"NAME";s:6:"Shoaib";s:5:"EMAIL";s:20:"shoaib@techleadz.com";s:5:"PHONE";s:10:"0987654321";s:8:"COMMENTS";s:3:"123";s:4:"DATE";s:10:"01/22/2016";s:4:"TIME";s:5:"11:00";}
        [submitted] => 1
    )

[1] => stdClass Object
    (
        [id] => 2
        [calendar] => 1
        [time] => 2016-01-23 10:39:30
        [booked_time] => 01/25/2016 15:00
        [booked_time_unformatted] => 2016-01-25 15:00:00
        [name] => Shoaib
        [email] => shoaib@techleadz.com
        [phone] => 0987654321
        [question] => dsadasd
        [quantity] => 1
        [buffered_date] => a:9:{s:5:"PRICE";s:2:"25";s:10:"COUPONCODE";s:0:"";s:8:"QUANTITY";N;s:4:"NAME";s:6:"Shoaib";s:5:"EMAIL";s:20:"shoaib@techleadz.com";s:5:"PHONE";s:10:"0987654321";s:8:"COMMENTS";s:7:"dsadasd";s:4:"DATE";s:10:"01/25/2016";s:4:"TIME";s:5:"15:00";}
        [submitted] => 1
    )

[2] => stdClass Object
    (
        [id] => 3
        [calendar] => 1
        [time] => 2016-01-23 11:27:22
        [booked_time] => 01/25/2016 11:00
        [booked_time_unformatted] => 2016-01-25 11:00:00
        [name] => test
        [email] => shoaib@techleadz.com
        [phone] => 0987654321
        [question] => asfas
        [quantity] => 1
        [buffered_date] => a:9:{s:5:"PRICE";s:2:"25";s:10:"COUPONCODE";s:0:"";s:8:"QUANTITY";N;s:4:"NAME";s:4:"test";s:5:"EMAIL";s:20:"shoaib@techleadz.com";s:5:"PHONE";s:10:"0987654321";s:8:"COMMENTS";s:5:"asfas";s:4:"DATE";s:10:"01/25/2016";s:4:"TIME";s:5:"11:00";}
        [submitted] => 0
    )

            )

现在我有一个这样的数组,所以我怎么能得到这个数组中[submitted] foreach元素的值。

2 个答案:

答案 0 :(得分:1)

这不是一个数组。这是StdClass对象

访问stdClass中的信息

@Path("/name")
public class AuditoriumService {

  @GET
  @Produces(MediaType.APPLICATION_XML)
  public List<String> getNames() throws UnrecoverableException {
    List<String> nameList = getNameList();
    
    log.info("Response: Status Code: "+200);
    return nameList;
   }
}

答案 1 :(得分:1)

要随机访问特定字段,请使用

echo $results[0]->submitted;
echo $results[0]->quantity;
echo $results[1]->submitted;
echo $results[1]->quantity;

使用foreach循环

foreach ( $results as $result ) {
    echo $result->submitted . '<br>';  

    echo $result->booked_time . '<br>';
    echo $result->name . '<br>';
    echo $result->time . '<br>';
    echo $result->email . '<br>';
}