N
N
Nikita Ok2018-01-28 20:26:10
PHP
Nikita Ok, 2018-01-28 20:26:10

How to pass multiple numbers to array through a variable?

there is a code:

foreach ($ids as $id){
  $top_term = get_top_term( 'cat' , $id );
  $toptermids_cars .= $top_term->term_id.', ';
}
echo $toptermids_cars; // выведет 11, 55, 97, 27, 25,
        
$array_toptermids_cars = array( $toptermids_cars );  // но в array передается как строка

and it turns out that $array_toptermids_cars outputs via var_dump():
array(1) { [0]=> string(16) "11, 55, 97, 27, 25, " }
and you need:
Array ( [0] => 11 [1] => 55 [2] => 97 [3] => 25 )
Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2018-01-28
@id_15000000

$array_toptermids_cars = array();

foreach ($ids as $id) {
  $top_term = get_top_term('cat', $id);
  $array_toptermids_cars[] = $top_term->term_id;  
}

var_dump($array_toptermids_cars);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question