dtyson2000 Posted February 15, 2009 Share Posted February 15, 2009 Hello. I thought I'd ask here since I've run into a conceptual roadblock. I just need to know if this is possible and to be pointed in the right direction (i.e. is there a specific function I should be looking at, etc.). Two arrays array 1: car, truck, bicycle array 2: first, second, third I'd like to combine these two arrays into the following output. I just don't know how to make second array into variables. $first = "car" $second = "truck" $third = "bicycle" I appreciate any direction you have time to offer. Thanks, again. Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/ Share on other sites More sharing options...
Twister1004 Posted February 15, 2009 Share Posted February 15, 2009 You will have to declare your variables before you make the arrays Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762444 Share on other sites More sharing options...
Stephen68 Posted February 15, 2009 Share Posted February 15, 2009 hmm... not sure what you are looking for something like this maybe? $array1 = ("car","truck",'bike"); $array2 = ("first","second","third"); $arrayCount = count($array1) for ($i=0; $i<= $arrayCount; $i++) { $array2[$i] = $array1[$i]; } Not sure if this would work and both arrays would have to be the same size. I'm just starting out so I maybe be way off here. Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762449 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Hi, I'm confused, do you want to turn 2 arrays into 1 array? Or are you looking at splicing the array items into scalar variables? Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762472 Share on other sites More sharing options...
dtyson2000 Posted February 15, 2009 Author Share Posted February 15, 2009 Hi, I'm confused, do you want to turn 2 arrays into 1 array? Or are you looking at splicing the array items into scalar variables? Yes. I'm looking to splice the items in two (and ultimately many more) into scalar variables. Initially I used multiple cases in a switch to define the variables that I needed but there are now over 10 cases and each holds around 200 variables - each case having the same variables with values that are case dependent. I figured that I would be much more efficient all around if I used arrays and spliced them. Thanks for throwing the term "scalar" out there. I wasn't aware of it before! Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762506 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Yes. I'm looking to splice the items in two (and ultimately many more) into scalar variables. Initially I used multiple cases in a switch to define the variables that I needed but there are now over 10 cases and each holds around 200 variables - each case having the same variables with values that are case dependent. I figured that I would be much more efficient all around if I used arrays and spliced them. Thanks for throwing the term "scalar" out there. I wasn't aware of it before! Ok, well I'm sure this can be done with relatively small amount of lines, I'm thinking just about 10? I am going to have to think this one through though...might not have an answer for you by tonite.. Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762511 Share on other sites More sharing options...
Philip Posted February 15, 2009 Share Posted February 15, 2009 Granted, this would only work if you didn't use keys in your arrays: <?php $array1 = array("car","truck","bike"); $array2 = array("first","second","third"); foreach($array1 as $key => $value) { $$value = $array2[$key]; echo $$value,' => ',$value,'<br />'; // show what the values are } ?> Would print: first => car second => truck third => bike Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762513 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Granted, this would only work if you didn't use keys in your arrays: <?php $array1 = array("car","truck","bike"); $array2 = array("first","second","third"); foreach($array1 as $key => $value) { $$value = $array2[$key]; echo $$value,' => ',$value,'<br />'; // show what the values are } ?> Would print: first => car second => truck third => bike Ahhh you are fast! You beat me to it..I was going to suggest the "variable variables" function... Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762515 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Hey, i figured out how to do this with less code... $array_1 = array('car', 'truck', 'bicycle'); $array_2 = array('first', 'second', 'third'); $array_3 = array_combine($array_2,$array_1); var_dump($array_3); Will output: array(3) { ["first"]=> string(3) "car" ["second"]=> string(5) "truck" ["third"]=> string(7) "bicycle" } Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762541 Share on other sites More sharing options...
Philip Posted February 15, 2009 Share Posted February 15, 2009 Yeah, you can use array_combine, but it would still be an array, not variables like the OP wanted. Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762567 Share on other sites More sharing options...
sasa Posted February 15, 2009 Share Posted February 15, 2009 <?php $array_1 = array('car', 'truck', 'bicycle'); $array_2 = array('first', 'second', 'third'); extract(array_combine($array_2,$array_1)); echo $first,' - ', $third; ?> Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762590 Share on other sites More sharing options...
dtyson2000 Posted February 15, 2009 Author Share Posted February 15, 2009 Thank you, KingPhilip and everybody who is helping out! I just love this stuff and learn so much when I ask questions here (I need to ask more). Thank you all! I had looked at the EXTRACT function but wasn't sure it was what I needed. There's still a need to create actual variables of the items in array1 (which appear to be outputting as text strings) to be passed to another function, but this definitely puts me on the right track and I want to try to figure this out on my own. Have a great day! Quote Link to comment https://forums.phpfreaks.com/topic/145243-two-arrays-one-variable-one-value/#findComment-762681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.