0

I have create array like

Array ( [0] => Array ( [14] => Array ( [selected] => selected ) ) [1] => Array ( [15] => Array ( [selected] => selected ) ) ) 

how to get the output like

Array ( [14] => Array ( [selected] => selected ) [15] => Array ( [selected] => selected ) ) 

PHP

$select_pic=$connection->createCommand("select * from sdmatts_collection_relation where furniture_id ='$fid'");
$multi_collection= $select_pic->queryAll();
$selectvalue= array();
foreach ($multi_collection as $fcol){
    $fmulti_collection[]   = array(
    $fcol['collection_id'] => array('selected' => 'selected'),);
}


I have create listbox dynamic select value in update time on yii1.
I use this code in yii1 listbox update time.

I use the static code 

 $selected   = array(
      '102' => array('selected' => 'selected'),
      '103' => array('selected' => 'selected'),
    );


also work listbox value are selected but i create dynamically that time not work.

my dynamic array is.

Array ( [0] => Array ( [14] => Array ( [selected] => selected ) ) [1] => Array ( [15] => Array ( [selected] => selected ) ) )

but still not working my listbox in select value.

so have can i create dynamic listbox in yii1 and add/edit time select the value.

thanks

3 Answers3

2

Try the following code:

<?php
$select_pic = $connection->createCommand("select * from sdmatts_collection_relation where furniture_id ='$fid'");
$multi_collection = $select_pic->queryAll();
$selectvalue = array();
foreach ($multi_collection as $fcol){
    $fmulti_collection[$fcol['collection_id']]   = array('selected' => 'selected');
}
mith
  • 1,680
  • 1
  • 10
  • 12
1

Use iterator_to_array. This is faster than others.

$result = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($yourArry)), 0);
print_r($result); //Display your expected result.

PHP CODE:

$yourArry = Array ( Array ( "14" => Array ( "selected" => "selected" ) ), Array ( "15" => Array ( "selected" => "selected" ) ) );
$result = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($yourArry)), 0);
print_r($result);

Result:

   Array ( [0] => selected [1] => selected ) 
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
0

When you're creating that array, use the collection_id variable as a key instead of []:

Current code:

$fmulti_collection[]   = array(
    $fcol['collection_id'] => array('selected' => 'selected'),);

Change to:

$fmulti_collection[$fcol['collection_id']] =
     => array('selected' => 'selected'),);

Notice: Keep in mind that $fcol['collection_id'] should be "unique", so if you have more than one element with the same collection_id value - it would be overwritten and then you'll have to use the first method and deal with that array's structure.

Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39