簡體   English   中英

使用 php 在多維數組元中調用一個值時是否可能只得到 3 個結果?

[英]Is it possible to get only 3 results when calling a value within a multidimensional array meta with php?

我正在開發評論系統,我只想顯示 3 個結果,然后我將在另一側加載 rest 但在這里我想不出如何只獲得 3 個。

這些值來自一個表單,我將它們保存在一個創建數組的后期目標中。

我展示了如何獲得評論:


$datacomments = array_reverse(get_post_meta($product_id, 'propina5', false)); 

foreach ($datacomments as $infocalif){

$comment = $infocalif['comment'];

echo $comment;
}

數組:

echo print_r($datacomments);

Array ( [0] => Array ( [date] => 01-12-2021 01:37 [id] => 2 [rating] => 4 [comment] => bla bla bla [perce] => 0 ) 
[1] => Array ( [date] => 01-12-2021 01:37 [id] => 2 [rating] => 4 [comment] => bla bla bla [perce] => 0 ) 
[2] => Array ( [date] => 01-12-2021 01:37 [id] => 2 [rating] => 4 [comment] => bla bla bla [perce] => 0 ) 
[3] => Array ( [date] => 01-12-2021 01:37 [id] => 2 [rating] => 4 [comment] => bla bla bla [perce] => 0 ) 
[4] => Array ( [date] => 01-12-2021 01:37 [id] => 2 [rating] => 4 [comment] => bla bla bla [perce] => 0 ) ) 1

默認情況下,這是我獲取保存在元數據中的所有評論的方式,但我只想獲取 3 個,由於 array_reverse,它將是最后 3 個。

然后我會將所有內容加載到不同的模板中,以免延遲初始加載

有什么建議嗎?

使用計數器

$cnt = 2;
foreach ($datacomments as $infocalif){

$comment = $infocalif['comment'];

echo $comment;
if (!$cnt--) break;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM