user2127403 user2127403 - 1 month ago 7
PHP Question

Variable Doesn't Work In Array

I am trying to print this wordpress query, but the variable doesn't work in that array (these numbers are just example). It will just take first number 53 and not the other numbers.

$catfeaturenumbers = '53,57,4294,71';
$categoryfeatured = array($catfeaturenumbers);
$featured = array(
'post__in' => $categoryfeatured
);

Answer

I think you want something like below:-

<?php

$catfeaturenumbers = '53,57,4294,71';

// create an array through explode

$categoryfeatured = explode(',',$catfeaturenumbers);

echo "<pre/>";print_r($categoryfeatured); // print array to check
$featured = array(
 'post__in' => $categoryfeatured
);

Note:- if you want to use your original string into an IN QUERY(example SELECT * FROM <table> WHERE id IN (your original string data)) then do like below:-

$categoryfeatured = "'".implode("','",explode(',',$catfeaturenumbers))."'";

echo  $categoryfeatured;

Both code Output link:-https://eval.in/662140

Comments