创建数组不能得到完美的输出

时间:2017-01-30 06:24:55

标签: php yii yii2

我创建了像

这样的数组
Array ( [0] => Array ( [14] => Array ( [selected] => selected ) ) [1] => Array ( [15] => Array ( [selected] => selected ) ) ) 

如何获得输出

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.

所以我可以在yii1中创建动态列表框并添加/编辑时间选择值。

感谢

3 个答案:

答案 0 :(得分:2)

请尝试以下代码:

<?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');
}

答案 1 :(得分:1)

使用iterator_to_array。这比其他人快。

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

PHP代码:

$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);

<强>结果:

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

答案 2 :(得分:0)

当您创建该数组时,请使用collection_id变量作为键而不是[]

当前代码:

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

更改为:

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

注意:请记住$fcol['collection_id']应该是“唯一的”,所以如果你有多个具有相同collection_id值的元素 - 它会被覆盖然后你会必须使用第一种方法并处理该数组的结构。