Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I stuff the results of this for loop into an array?

Here's my array:

$my_array=array ( 
	0 => array ( 'id' => '1', 'column_name' => 'screeningsubperiod', 'column_data' => 'screeningsubperiod_data', ), 
	1 => array ( 'id' => '2', 'column_name' => 'screeningperiodid', 'column_data' => 'screeningperiodid_data', ), 
	2 => array ( 'id' => '3', 'column_name' => 'screeningtime', 'column_data' => 'screeningtime_data', ), 
	3 => array ( 'id' => '4', 'column_name' => 'bfull', 'column_data' => 'bfull_data', ), 
);

Open in new window


When I run this code...

<?php
$num=count($my_array);
$numberfields=count($my_array[0]);

$i=0;
foreach($my_array as $row)
{
	
	foreach($row as $key=>$value)
	{		
		$fieldname="";
		$fieldvalue="";
		$fieldname=$key;
		$fieldvalue=$value;
		
		//$data[$fieldname] = $fieldvalue;
		$data[$i][$fieldname]=[$fieldvalue];
		$i++;
	}
}
var_dump($data);


?>

Open in new window


...I get this:

User generated image
I need to "dig" into the array like this:

if($data['column_name']=="screeningsubperiod")
{
echo "yes";
}

I keep getting an error that says I'm using an undefined index and I understand WHY I'm getting that error, I just don't know how to build my array in a manner that allows me to check specific values.

Ideally, I need the array to look like this:

0 => array ( 'id' => '1', 'column_name' => 'screeningsubperiod', 'column_data' => 'screeningsubperiod_data', ),

Right now, I'm getting a separate array for every column heading. How can I fix that and then how can I drill down into that array and check for specific values?
Capture.PNG
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Thanks! Got it!