hi guys,

i have a problem in my coding. i want to get the array keys + their values except for [] and replace them with "". i want to do this because i'm using a function to create a form. Since i'm developing a form generator.i need this help.

Here is the problem,

Array
(
[Teachers_no_id] => Array
(
[text] => Teachers_no_text
[type] => input
)

[Teacher_name_id] => Array
(
[text] => Teacher_name_text
[type] => input
)

)


i want to convert it to,


Array
(
"Teachers_no_id"=> Array
(
"text" => "Teachers_no_text",
"type" => "input"
)

"Teacher_name_id" => Array
(
"text" => "Teacher_name_text",
"type" => "input"
)

);

hope you'll help me out
thank you in advance.

-madawa-

If you use print_r for an array, thats how it prints. You can't do much about it. But you can use foreach to use the array as you want it.
Example,

<?php
$x = array("teacher"=>"10","student"=>"20");
$y = array("teacher"=>"30","student"=>"40");
$z = array("teacher_no_id"=>$x,"teacher_no_name"=>$y);
foreach($z as $key => $value) {
	$output.="Main array index ".$key." -> <br />";
	foreach($value as $newkey => $newvalue) {
		$output.="Sub array index ". $newkey." -> Sub array value ".$newvalue."<br />";
	}
	$output.="<br />";
}
echo $output;
?>

yep mate, i tried the foreach but could not get the "", is there an possibility to covert array in to string and using a regex, replace [] with ""???

Unless you do a print_r, there 'wont' be []. :-/ I don't understand why[ or how] you want to [going to] do it.

itz like this , fistly i wrote the function, hard coded the array which creates the form like this

$formdef = array(
"prd_code" => array(
"text" => "Product Code",
"type" => "input",
"validate" => array(
"required" => "Please enter the product code"
)
),
"prd_brand" => array(
"text" => "Brand",
"type" => "input",
"validate" => array(
"required" => "Please enter the product brand"
)
)


so i want to get the same from the array

I don't think its possible. You have to use foreach, iterate through array elements and take required step. You can't change the 'structure' of an array (ie., replacing [] with " "). Again, I hope I am wrong.

yep i'm also confused...
anyways thanks for the coperation!!!

hey guys,

i found the soloution for this...
its a function that creates strings...

the initial array would be send to this function function output_formdef($ar,$level=0)

{
$newval = "";
$spaces ="    ";
foreach( $ar as $key=>$val){
if( "" != $newval ){
$newval .= ",\n";
}
$newval .= str_repeat($spaces,$level);
$newval .= ('"'.$key.'" => ');


if( is_array($val) ){
$newval .= output_formdef($val,$level+1);
}
else{
$newval .= ('"'.$val.'"');
}


//$newval .= "\n";
}


return "Array\n".str_repeat($spaces,$level)."(\n$newval".str_repeat($spaces,$level)."\n".str_repeat($spaces,$level).")";


}

which would give the result like this ,

Array
(
"Teachers_no_id" => Array
(
"text" => "Teachers_no_text",
"type" => "input",
"Validate" => Array
(
"float" => "abc"
)
),
"Teacher_name_id" => Array
(
"text" => "Teacher_name_text",
"type" => "input"
)
)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.