Write into array dynamically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
greg7
Forum Commoner
Posts: 32
Joined: Tue Oct 13, 2009 7:38 am

Write into array dynamically

Post by greg7 »

Hi quys, i use php datagrid free version and i'm trying to customize dynamically the view mode, my question stems from that but i think is general. Specifically there is a protected function that accepts an array as argument, this array has the following format

Code: Select all

//1.
$a = array(
       "field1" =>array("header" =>"x", "type"=>"label",...),
       "field2" =>array("header" =>"y", "type"=>"label",...),
       ........);
 
In my application the fields are set dynamically from an uploaded file, so the above is useless. Because of that i've saved into an array the fields and with the following i was hoping to solve it

Code: Select all

for( $i=0; $i<=$n; $i++)
  { $a[$i] = array("$b[$i]" =>array("header" => "$b[$i]",.........) ); }
 
// passing array as argumnet in function
$dgrid->setColumnsInViewMode($a);

But all i get by this way, is the last column ($b[n]), i placed the call of the function inside the loop before "}" but i get the same. It seems that something i'm doing wrong, i'm stack on it. The last thing i tried is to write into array like the following

Code: Select all

$a = array( "; 
 
 
 
like writing in a variable without loosing the previous value

Code: Select all

$a="abc"; $a.="def"; //prints abcdef
I did that in order to be similar to format which plays (1.) but i didn't make it, so any ideas would be helpful, thanks in advance!
*I 've already cheched SQL statements, all the other operations works fine, the only problem is on display the data.
cdw3423
Forum Newbie
Posts: 3
Joined: Tue Oct 20, 2009 10:50 pm

Re: Write into array dynamically

Post by cdw3423 »

I'm not sure what you are doing in that second code segment. But I think it might help to understand how this works if you rewrite the first one like this. . .

Code: Select all

 
$a['field1']['header'] = 'x';
$a['field1']['type'] = 'label';
$a['field1']['xxxx'] = 'yyyyy';
$a['field2']['header'] = 'y';
$a['field2']['type'] = 'label';
$a['field2']['xxxx'] = 'yyyyy';
 
I think what you are trying to do in the second example is to just use an integer index instead of 'field1', 'field2' ...
I'm not sure where the rest of the data is coming from. Maybe an array of input fields for header and type etc in your form? In that case I think I would do something like this....

Code: Select all

 
$HeaderArray = $_POST['HeaderInputFieldArray'];
$TypeArray = $_POST['TypeInputFieldArray'];
foreach($HeaderArray as $i => $header){
  $type = $TypeArray[$i];
  $a[$i]['header'] = $header;
  $a[$i]['type'] = $type;
}
 
the for loop would work too, but I like foreach when I can use it and then I don't have to know the size of the array a head of time. Of course this assumes that the TypeArray and the HeaderArray and any other Arrays like that all have the same number of elements and that they are all indexed the same way.
greg7
Forum Commoner
Posts: 32
Joined: Tue Oct 13, 2009 7:38 am

Re: Write into array dynamically

Post by greg7 »

Thanks a lot for your reply. I did it :lol: , using the following

Code: Select all

 
 
foreach($HeaderArray as $i => $header){
  $a[$header]['header'] = $header;
  $a[$header]['type'] = "label"; // or whatelse type i want to 
 .......
 }
Post Reply