Jump to content

Two arrays, one variable, one value


dtyson2000

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

 

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..

 

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

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"

}

 

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.