Implode Method Not Working

im trying to implode an array to a string, this is my code




foreach ($model->decreto as $decreto) 

{

	$decs =  $decreto->ndecreto."\n";

}

echo "decs is: '".implode("','",$decs)."'<br>";



i have tried other methods but no clue, any hints please?

Why it’s not working? You get some error?

$decs doesn’t look like an array.

i set it as an array but it just give me 1 element, ill show the examples…

only echo on foreach




foreach ($model->decreto as $decreto) 

{

	echo $decreto->ndecreto."\n";

}



returns:

76578 786 123415 2452 1254146

echo on $decs array in foreach




foreach ($model->decreto as $decreto) 

{

	$decs =  array($decreto->ndecreto);

}

echo "decs is: '".implode("','",$decs)."'<br>";



returns:

decs is: ‘1254146’.

as you can see… it only takes 1 element and i dont know why.

this is my view.




<?php




/* @var $this CaracterizacionController */

/* @var $model Caracterizacion */


$this->breadcrumbs=array(

	'Caracterizacions'=>array('index'),

	$model->id,

);


$this->menu=array(

	array('label'=>'List Caracterizacion', 'url'=>array('index')),

	array('label'=>'Create Caracterizacion', 'url'=>array('create')),

	array('label'=>'Update Caracterizacion', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Caracterizacion', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Caracterizacion', 'url'=>array('admin')),

);

?>


<h1>View Caracterizacion #<?php echo $model->id; ?></h1>


<?php 

$this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'parametro',

	),

));

}


//copiar el la variable model de caracterizacion a otra variable para evitar errores.

$car = $model;


$this->renderPartial('//decretocaracterizacion/_form_', array('dec'=>$dec, 'car'=>$car));


foreach ($model->decreto as $decreto) 

{

	$decs =  array($decreto->ndecreto);

}

echo "decs is: '".implode("','",$decs)."'<br>";

?>



and this is my controller:




<?php


class CaracterizacionController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

			'postOnly + delete', // we only allow deletion via POST request

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

		$dec=new DecretoCaracterizacion;


		if(isset($_POST['DecretoCaracterizacion']))

		{

			

			$dec->attributes=$_POST['DecretoCaracterizacion'];

			if($dec->save())

				$this->redirect(array('view', 'id'=>$id));

		}




		$this->render('view',array(

			'model'=>$this->loadModel($id), 'dec'=>$dec

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Caracterizacion;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Caracterizacion']))

		{

			$model->attributes=$_POST['Caracterizacion'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

			if($model->save() == false)

				var_dump($model->errors);

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Caracterizacion']))

		{

			$model->attributes=$_POST['Caracterizacion'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		$this->loadModel($id)->delete();


		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

		if(!isset($_GET['ajax']))

			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Caracterizacion');

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Caracterizacion('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Caracterizacion']))

			$model->attributes=$_GET['Caracterizacion'];


		$this->render('admin',array(

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer $id the ID of the model to be loaded

	 * @return Caracterizacion the loaded model

	 * @throws CHttpException

	 */

	public function loadModel($id)

	{

		$model=Caracterizacion::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param Caracterizacion $model the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='caracterizacion-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}



Try this?




$decs = array();

foreach ($model->decreto as $decreto) 

    $decs[] =  $decreto->ndecreto;

echo "decs is: '".implode("','",$decs)."'<br>";



that made the trick, thanks for your help, i just have 1 question…

how do i make the echo to make a new line for each element?.. i tried with "\n" and even with the <br>, but it just add spaces between then

This should work:




echo implode("<br>", $decs);