I have an array. Here is the var_dump
of that array.
array (size=2)
0 =>
object(stdClass)[266]
public 'term_id' => string '4' (length=1)
public 'name' => string 'Test' (length=4)
public 'slug' => string 'test' (length=4)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '4' (length=1)
public 'taxonomy' => string 'filter' (length=6)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '0' (length=1)
1 =>
object(stdClass)[277]
public 'term_id' => string '5' (length=1)
public 'name' => string 'test2' (length=5)
public 'slug' => string 'test2' (length=5)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '5' (length=1)
public 'taxonomy' => string 'filter' (length=6)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '0' (length=1)
Now I would like to convert that array like this.
$choices = array(
array('label' => 'Test','value' => 'test'),
array('label' => 'test2','value' => 'test2'),
)
Please note: I mapped keys like this in the choices
array
name key as label
slug key as value
Can someone tell me how to achieve this?
Update:
This is what I tried so far.
foreach ( $filters as $filter ) {
$filterarr[] = "array('label' => '". $filter->name ."' ,'value' => '". $filter->slug ."' )";
}
$choices = array($filterarr);
But its not working as expected.