Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
What have you tried? – Bigood Mar 7 at 17:24
I didn't get your question. Actually I spent more than 1 hour in this. I couldn't get a proper solution. I tried using foreach loop like this. foreach ( $filters as $filter ) { $filterarr[] = "array('label' => '". $filter->name ."' ,'value' => '". $filter->slug ."' )"; } $filter_array = array($filterarr); But its not working properly. Thats why I posted this question. And no i'm not lazy – Giri Mar 7 at 17:27
@Bigood See my above comment. – Giri Mar 7 at 17:28
@Giri Add this on your original post! That's why meouw thought you didn't try something before – Bigood Mar 7 at 17:29

4 Answers

up vote 1 down vote accepted

Try that

$choices = array();    
$tempArray = array();

for($i=0; $i < count(YOUR_ARRAY); $i++)
{
    $tempArray["label"] = array[$i]->name;
    $tempArray["value"] = array[$i]->slug;

    array_push($choices, $tempArray);
}
share|improve this answer
Thanks. It works like a charm. – Giri Mar 7 at 17:37

Simply typecast your object. And perform the operation you wish as below.

$posts = (array) $yourObject;
$choices = array();
foreach($posts as $post){
   $choices[]['label'] = $post['name'];
   $choices[]['value'] = $post['slug'];
}
share|improve this answer
Fatal error: Cannot use object of type stdClass as array :( – Giri Mar 7 at 17:35
@Giri Try the function mention here to convert your obj to array. if-not-true-then-false.com/2009/… – Rikesh Mar 7 at 17:38
hmm thanks. But @bigwood solution worked well. – Giri Mar 7 at 17:42

You can write something like that, but try to find it by yourself next time it's quite simple.

foreach($your_array as $a)
{
    $choices[] = array('label' => $a['label'],
                       'value' => $a['slug']);
}
share|improve this answer

Try;

foreach($array as $object){
   $choices[] = array("label" => $object->name, "value" => $object->slug);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.