كيف احول هذه المصفوفة إلى مصفوفة عادية فيها ايدهات فقط

  • Mohamed__99

استخدم هذا الكود لاظهار ايدهات المقالات في وردبريس

<?
function get_popular_posts_ids($range = 'last7days', $limit = 10)
{
  $post_IDs = array();
    $query = new \WordPressPopularPosts\Query([
      'range' => $range,
      'limit' => $limit
    ]);
    $popular_posts = $query->get_posts();
   
    // Popular posts found, get their IDs
    if ( is_array($popular_posts) && ! empty($popular_posts) ) {
      foreach($popular_posts as $popular_post) {
        $post_IDs[] = $popular_post->id;
      }
    }

  return $post_IDs;
}
?>
<?php
var_dump(get_popular_posts_ids('last24hours'));
?>

تظهر النتيجة هكذا

array(10) {
 [0]=>
 string(3) "323"
 [1]=>
 string(3) "154"
 [2]=>
 string(3) "277"
 [3]=>
 string(3) "289"
 [4]=>
 string(2) "92"
 [5]=>
 string(2) "54"
 [6]=>
 string(2) "97"
 [7]=>
 string(3) "140"
 [8]=>
 string(3) "236"
 [9]=>
 string(3) "302"
}

كيف يمكنني عرض المصفوفة لتكون بهذا الشكل

[140,302,402]
يرجى الدخول لحسابك أو تسجيل حساب لتستطيع إضافة تعليق
حساب جديد دخول

التعليقات

حاول الطباعة بالطريقة:

نحول المصفوفة لصيغة json بالاستعانة بالدالة json_encode لتصبح المصفوفة مثل جافاسكربت

[140,302,402]

كالتالي

$resultArray = get_popular_posts_ids('last24hours'));
echo json_encode($resultArray);

من فضلك ، عندما استخدم التحقق مما اذا كان post id ضمن المصفوفة

$last30days = json_encode(get_popular_posts_ids('last30days', 3));
$result = in_array('154', $last30days);
echo $result;

يظهر لي خطأ

Warning: in_array() expects parameter 2 to be array, string given in
json_encode

لا تعتبر مصفوفة PHP فهي تعيد string .

استخدم ما تعيده الدالة get_popular_posts_ids مباشرة

بارك الله في علمك

شكرا