3

I am try to set one value to another array. I have this type of two arrays.

Array
(
    [0] => test1
    [1] => test2
)
Array
(
    [0] => 351
    [1] => 352
    [2] => 353
    [3] => 354
    [4] => 355
    [5] => 356
)

Now I want to do something like set the first three values of the second array on test1, and set another three values from the second array to test2.

test1 = 351,352,353
test2 = 354,355,356

Is it possible?

1
  • wat if there are 6 items in second array..or will it always contain 5 items? Commented May 24, 2013 at 5:57

1 Answer 1

4

Try this :

$var  = array(0=> "test1",1=> "test2");
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);

$res  = array_combine($var,array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3)));

echo "<pre>";
print_r($res);

Output :

Array
(
    [test1] => 351,352,353
    [test2] => 354,355,356
)

EDIT : As per comment "this type output i need Array ( [0] => 351,352,353 [1] => 354,355,356 )"

$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);

$res  = array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3));

echo "<pre>";
print_r($res);

Output:

Array
(
    [0] => 351,352,353
    [1] => 354,355,356
)
Sign up to request clarification or add additional context in comments.

4 Comments

Edited the answer please check it.
both help full... thanks for this. and its possible array([test1],[test2]) key to [0] or [1] like auto key
this type output i need Array ( [0] => 351,352,353 [1] => 354,355,356 )
@JackPhp : Do you want 0,1 instead of test1, test2 ?...Yes it is possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.