×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

php array manipulation

php array manipulation

php array manipulation

(OP)
I have variables pulled from mysql, ex $cs & $counter. $cs has contains 3 numbers, ex 100,200,300 and $counter has 2 names, ex john,bob. In actuality, there are a total of 7 variables with between 2 and 3 data terms inside each variable. I'm trying to explode each variable in the array, attach a letter to each exploded variable (A,B,C) that corresponds with each data point within the variable, and use the specific variable ($csA) in future calculations and display in a form. So far, this is my code:

CODE

$combine_array=array(
							array('$cs',"$cs"),
							array('$dsd_purch',"$dsd_purch"),
							array('$wh_purch',"$wh_purch"),
							array('$dsd_cred',"$dsd_cred"),
							array('$sales',"$sales"),
							array('$counter',"$counter"),
							array('$inv_time',"$inv_time")
						);
			$letter_array=array("A","B","C");
			for($ix=0;$ix<count($combine_array);$ix++){
				if($combine_array[$ix][1]!=NULL){
					$combine_array[$ix][1]=explode(",",$combine_array[$ix][1]);
					for($ix_l=0;$ix_l<count($combine_array[$ix][1]);$ix_l++){
						//$combine_array[$ix][0]."".$letter_array[$ix_l]=$combine_array[$ix][1][$ix_l];
						echo $combine_array[$ix][0]."".$letter_array[$ix_l]." ".$combine_array[$ix][1][$ix_l]."<br>";
					}
					
				}
			} 

In the first array, $combine_array, the first variable is what I want to manipulate (add a letter A or B or C), the second variable contains the data.

The last 2 lines of code are my problem. The commented section

CODE

//$combine_array[$ix][0]."".$letter_array[$ix_l]=$combine_array[$ix][1][$ix_l]; 
is what I want to do, but is obviously not right. The last line gives me exactly what I want in display terms, but does not set $csA=100, the first of the 3 numbers (100,200,300). Obviously, I'm not a professional and there is probably a better way, but this is where my thought process has led me. Any help would be appreciated.

I have this done in long form: pull the data from mysql, set each field equal to a variable, explode each individual variable, set each individual variable ($csA, $csB, $csC) to the corresponding exploded value, then move on to $dsd_purch and do it all over again. I'm trying to condense the code and learn something new. Thanks.

RE: php array manipulation

the commented line is not right. the left hand side resolves to a string, the right to a variable. you cannot assign a variable to a string. you can only assign a string to a variable. in php assignments are right to left.

RE: php array manipulation

(OP)
Thanks, jpadie. I see that now, and I knew that wasn't working. It's not the answer I'm really looking for. I guess I'm asking if there is a way to create a fixed variable from 2 different arrays,or use an array of the variables I want, and set it equal to a dynamically generated variable to be used in later calculations without having to set each variable individually

CODE

$csA=no[1];$csB=no[2];$dsd_purchA=no[3] 
or using the dynamically generated code in my form: $combine_array[$ix][1][$ix_l].
I can do it the long way, but I'm looking to make my code more concise, and learn something new.

RE: php array manipulation

I'm really struggling to see the logic behind what you are trying to achieve. perhaps if yu took a step back and explained:

1. what each element is, in real language (i.e. what is the intent and purpose of each variable
2. what is the aim behind concatenating the variables

we may be able to suggest a simpler model.

put simply, however, if the echo'd OUTPUT is what you are looking for AND you want instead to put that in a variable then replace the word echo with "$myVariable = "

RE: php array manipulation

(OP)
Sorry, I knew my question was very convoluted. I'll try again. I have a form with inputted user data, $csA, $csB, $csC, $dsd_purchA, $dsd_purchB, etc. This data is put into a database with fields cs, dsd_purch, etc. $csA, $csB, and $csC are collapsed into comma-separated values (100,200,300) and recorded into mysql field cs. Each field (dsd_purch,wh_purch,dsd_cred, etc) all have comma-separated values. When redisplaying the form, I want to read the data back from the database as concisely as I can, exploding the values, and setting them equal their corresponding variables without walking through each variable as I already have:

CODE

...mysqli query
                   
$cs=$row['cs'];
$dsd_purch=$row['dsd_purch'];
$wh_purch=$row['wh_purch'];
etc.. 
(the following is not part of the code, only what each variable represents in the database)
where $cs=100,200,300
$dsd_purch=500,600
$wh_purch=300,400
etc...

CODE

if($cs!=NULL){
		                $cs=explode(",",$cs);
				$csA=$cs[0];
				$csB=$cs[1];
				$csC=$cs[2];
			}
			if($dsd_purch!=NULL){
				$dsd_purch=explode(",",$dsd_purch);
				$dsd_purchA=$dsd_purch[0];
				$dsd_purchB=$dsd_purch[1];
			}
			if($wh_purch!=NULL){
				$wh_purch=explode(",",$wh_purch);
				$wh_purchA=$wh_purch[0];
				$wh_purchB=$wh_purch[1];
			}
			if($dsd_cred!=NULL){
				$dsd_cred=explode(",",$dsd_cred);
				$dsd_credA=$dsd_cred[0];
				$dsd_credB=$dsd_cred[1];
			}
			if($sales!=NULL){
				$sales=explode(",",$sales);
				$salesA=$sales[0];
				$salesB=$sales[1];
			}
			if($counter!=NULL){
				$counter=explode(",",$counter);
				$counterA=$counter[0];
				$counterB=$counter[1];
			}
			if($inv_time!=NULL){
				$inv_time=explode(",",$inv_time);
				$timeA=$inv_time[0];
				$timeB=$inv_time[1];
			} 
I know this works, I just want to clean it up and make it more concise and learn a better way. Thanks again for the consideration and any better ideas you have.

RE: php array manipulation

I see.

let us say that your database fields are as follows (those that are delimited). if you need more of them to be exploded and modified just add them to the list of $fields.

big warnings:

1. untested. but this is not likely to do any harm;
2. if you need to manipulate your data like this then 95%+ likely you have your storage schema wrong.

CODE

$fields = array('dsd_purch','wh_purch','dsd_cred');
...
$outputs = array();
while($row = mysql_fetch_assoc($result)):
 $output  = array();
 foreach ($row as $fieldName => $value):
   if(in_array($fieldName, $fields)):
     $counter = 65;
     foreach(explode(",",$value) as $item):
       $output[$fieldName . chr($counter)] = $item;
       $counter++;
     endforeach;
   else:
     $output[$fieldName] = $value;
   endif;
   $outputs[] = $output;
 endforeach;
endwhile;
echo '<pre>';
print_r($outputs);
echo '</pre>'; 

RE: php array manipulation

(OP)
Thanks again, jpadie.

After working more on my own, this is what I came up with:

CODE

$combine_array=array("$cs","$dsd_purch","$wh_purch","$dsd_cred","$sales","$counter","$inv_time");
$combine_array_2=array('csA','csB','csC','dsd_purchA','dsd_purchB','wh_purchA','wh_purchB','dsd_credA','dsd_credB','salesA','salesB','counterA','counterB','timeA','timeB');
$combine_array_no=0;
for($ix=0;$ix<count($combine_array);$ix++){
     if($combine_array[$ix]!=NULL){
	$combine_array[$ix]=explode(",",$combine_array[$ix]);
	for($ix_l=0;$ix_l<count($combine_array[$ix]);$ix_l++){
		${$combine_array_2[$combine_array_no]}=$combine_array[$ix][$ix_l];
		$combine_array_no++;
	}
     }
} 
This works and is cleaner than my original code. Yours looks very promising and is more of what I had in mind; I just couldn't get there. I'll study it and try to implement it into my code. As you can see, I'm very weak on the OOP side. Thanks again.

RE: php array manipulation

umm - there's no OOP in that code. nor in mine.

instead of storing values as comma delimited consider instead storing a serialized array. or if you will need to search ever on those values then you should take another look at your schema and create a table that looks something like this

CODE

create table myValues(
  associatedRecordID INT(11),
  valueName varchar(255) NOT NULL,
  value LONGTEXT
);
create unique index myVIndex on myValues (associatedRecordID, valueName); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close