Hướng dẫn how to get array object value in php - cách lấy giá trị đối tượng mảng trong php

Đây là chức năng của tôi thu thập dữ liệu từ trang. Dữ liệu được lưu trữ vào mảng và cuối cùng tôi lặp lại nó bằng var_dump ().

Dữ liệu được nối thành mảng có độ dài 40. Tôi đã cố gắng để có được mỗi giá trị mảng seperatelly.

Kết quả mảng trông như thế này khi tôi sử dụng var_dump($root):

object(stdClass)[1]
  public 'items' => 
    array (size=40)
      0 => 
        object(stdClass)[12983]
          public 'comment' => string 'Locanda is great ! Maybe a bit overrated... But I definitely enjoyed my first dinner here! 

Trippa alla romana is so good! My first time eating tripe and the texture wasn't bad at all plus the sauce was fantastic!

The leg of lamb was nice, a bit too rare for me but I didn't specify how I wanted it so next time I will.

The hen wrapped in pancetta was so delicious and juicy! Would recommend that over the lamb.

We also had a nice pasta dish.. Orecchiette which was of cour'... (length=738) public 'rating' => string '4.0' (length=3) public 'date' => string '2014-04-03' (length=10) 1 => object(stdClass)[12984] public 'comment' => string 'Made reservations last friday night here for a fun special dinner, had a pretty good time but was a bit underwhelmed.

We arrived early for our reservation, about 15 minutes and were going to go to the bar for a cocktail but the hostess told us that she could sit us early and to not to go to the bar and then we were sat 10 minutes after our reservation time. A bit annoying but I get it, it's Friday night. We got a great table in the back half or the restaurant so we weren't by the door and awk'... (length=1676) public 'rating' => string '3.0' (length=3) public 'date' => string '2014-04-21' (length=10) 2 => object(stdClass)[12985] public 'comment' => string 'Thinking to have something special for dinner on Sunday, we ordered takeout from Locanda through Postmates. Â Words cannot describe how sad we were when the food came. Â The portions are TINY TINY TINY and slopped into large paper takeout containers (no, not even the wax lined ones) that make them look even smaller. Â Miniscule, beyond small portions. Â The sauce soaked into the containers, so icky. Â Food has never looked so pitiful.

We paid $70 for three dishes that would all fit on a single plate '... (length=2081) public 'rating' => string '2.0' (length=3) public 'date' => string '2014-03-31' (length=10) 3 => object(stdClass)[12986] public 'comment' => string 'I can't say enough good things about locanda. Finally reviewing after eating dinner there as opposed to just visiting the bar.

Dinner was fantastic - warm bread and brussel sprouts to start. Pasta with broccoli rabe and sausage was so wonderful (and so hard to find in sf!)

Now for the bar. Bartenders are so knowledgeable and friendly. Â I love how they'll listen to your feedback and then get creative on your next drink. Most recently tried the steam shandy - excellent! Â Oh, and old f'... (length=530) public 'rating' => string '5.0' (length=3) public 'date' => string '2014-03-30' (length=10) 4 => object(stdClass)[12987] public 'comment' => string 'The wait for 2 on a Saturday night at 6:15pm was only 20 minutes. Cocktails were delicious, though the ginger cocktail was too gingery. We got the whole grilled fish. It was pretty good but a bit too salty. The atmosphere is great.' (length=231) public 'rating' => string '4.0' (length=3) public 'date' => string '2014-04-20' (length=10)

và như thế

Mã đưa ra kết quả này.

  function Getdata($url){
    print("$url\n");
    $root = new stdClass();
    $items = array();
    $html = file_get_html($url);
    if($html){
      $containers = $html->find('div.review-list div.review div.review-wrapper');
      foreach($containers as $container){
        $comments = $container->find('div.review-content p.review_comment');
        $item = new stdClass();
        foreach($comments as $comment){
          $comment_html = $comment->innertext();
          $item->comment = $comment_html;
        }
        $metas = $container->find('div.review-content meta');
        foreach($metas as $meta){
          $itemprop = $meta->itemprop;
          $content = $meta->content;
          if($itemprop == 'ratingValue') $key = 'rating';
          else $key = 'date';
          $item->$key = $content;
        }
        $items[] = $item;
      }
    }
    $root->items = $items;
    if($html){
      $html->clear();
      unset($html);
    }
    var_dump $root;

  }

Ở đây tôi đã cố gắng để có được từng comment, rating, date riêng biệt

  for($i = 0 ; $i <=80 ; $i = $i + 40)
    {
          $url = 'http://www.testioco.com/biz?start='.$i.'';
          $root = yelp($url);
            for($j = 0; $j < sizeof($root); $j++)
                {
                    echo $root[$j]['comment']."
"; echo $root[$j]['rating']."
"; echo $root[$j]['date']."
"; } //var_dump($root); flush(); ob_flush(); }

Tôi muốn nhận từng bình luận, tỷ lệ, ngày riêng biệt để lưu trữ trong db. Vì vậy, xin vui lòng giúp tôi cho phù hợp.

Làm thế nào để bạn chọn một mảng các đối tượng trong PHP?

Nó khá đơn giản - bạn chỉ cần đi theo hệ thống phân cấp từng bước một lần tham chiếu chỉ mục bạn cần.Điều này sẽ chỉ vào dữ liệu bạn đang tìm kiếm.N.B.Cú pháp -> Index được sử dụng để có các thuộc tính của một đối tượng.Cú pháp ["index"] được sử dụng để có các thuộc tính của một mảng.go down the hierarchy one step at a time referencing the index you need. which will point to the data you're looking for. N.B. The ->index syntax is used to get properties of an object. the ["index"] syntax is used to get properties of an array.

Làm thế nào để bạn truy cập giá trị của một mảng?

Bạn có thể truy cập một phần tử mảng bằng cách tham khảo số chỉ mục của nó.Các chỉ mục trong các mảng numpy bắt đầu bằng 0, nghĩa là phần tử thứ nhất có chỉ mục 0 và phần thứ hai có chỉ số 1, v.v.by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

Làm thế nào có được khóa của một đối tượng trong PHP?

Để chỉ hiển thị các khóa từ một đối tượng, hãy sử dụng Array_Keys () trong PHP.use array_keys() in PHP.

Làm thế nào để bạn truy cập các thuộc tính của một đối tượng trong PHP?

Cách tiếp cận thực tế nhất chỉ đơn giản là chọn đối tượng mà bạn quan tâm trở lại vào một mảng, điều này sẽ cho phép bạn truy cập các thuộc tính: $ a = mảng ('123' => '123', '123foo' => '123foo');$ o = (đối tượng) $ a;$ a = (mảng) $ o;echo $ o-> {'123'};// lỗi!cast the object you are interested in back into an array, which will allow you to access the properties: $a = array('123' => '123', '123foo' => '123foo'); $o = (object)$a; $a = (array)$o; echo $o->{'123'}; // error!