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!

*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

php array manipulation

php array manipulation

php array manipulation

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:


						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


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

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


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

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:


...mysqli query
(the following is not part of the code, only what each variable represents in the database)
where $cs=100,200,300


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.


$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;
     $output[$fieldName] = $value;
   $outputs[] = $output;
echo '<pre>';
echo '</pre>'; 

RE: php array manipulation

Thanks again, jpadie.

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


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


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