views:

96

answers:

9

$array = array('item1', 'item2', 'item3', 'item4', 'item5');

here i want to extract only the first three items in the array, and then

$implodes = implode(';', $array);

echo $implodes;

which should output

item1;item2;item3


$i=0;
$new = array();
foreach($array as $arr)
{
  $i++;
  if($i <= 3)
  {
    $new[] = $arr;
  }
}

doesn't look pretty tho

A: 

use

array_slice


$output = array_slice($input, 0, 3);

http://php.net/manual/en/function.array-slice.php

Haim Evgi
A: 

You can use array_slice

$array = array('item1', 'item2', 'item3', 'item4', 'item5');
$implodes = implode(';', array_slice($array,0,3));
echo $implodes;
codaddict
+6  A: 

Use array_slice:

$output = implode(';', array_slice($array, 0, 3));

fabrik
uh nice, thanks!!thanks to you all, actually!;)
You're welcome :)
fabrik
A: 

$array = array_slice( $array , 0 , 3 );

killer_PL
+1  A: 
$array = array('item1', 'item2', 'item3', 'item4', 'item5');
$array = array_slice($array, 0, 3);
$implodes = implode(';', $array);
echo $implodes;
BoltClock
A: 
$array = array('item1', 'item2', 'item3', 'item4', 'item5');
$output = array_slice($array, 0, 3);
$implodes = implode(';', $array);
echo $implodes;
Loveleen Kaur
Why I didn't spot this earlier I have no idea, but nice job on copying my answer.
BoltClock
A: 

it can be done also without of any syntax sugar knowledge, but with basic programming skills

$num = 3;
$result='';
for ($i=0,$i<$num,$i++) {
  $result = $result.$array[$i];
  if ($i < $num-1) $result = $result.';';
}
Col. Shrapnel
Why would you want to do it that way or encourage others to do it that way though? Isn't the point of SO to convey programming knowledge (like syntax sugar) to others? Even if you want to do it manually for practice, there's no advantage to writing `$result = $result.$array[$i]` over `$result .= $array[$i]`.
Lèse majesté
@Lèse because it is often costs you less time to do it familiar way, rather than look for a fancy one. All this syntax sugar can be learned eventually, not a big deal. There is no advantage but it uses most basic constructs. And don't tell me of SO ways. Greed and ignorance are they.
Col. Shrapnel
And, you know... programming knowledge IS NOT about having some set of rules mindlessly learned by heart. I'd call it programming ignorance instead. You can see legions of such "programmers" here. they do not understand both question and theor answer, but just repeat mechanically some prayers they learned.
Col. Shrapnel
I strongly disagree. It would definitely cost the OP more time to have him continue to do it the hard way rather than actually informing him on how to do it the _right_ way. The extra 5 minutes it takes him to figure out what `array_slice()` or `.=` does will save him a lot more time in the long run, and these are very basic pieces of PHP knowledge that he will need to learn anyway. It's not like you're asking him to learn RegEx while learning PHP.
Lèse majesté
Lol. You are talking of coding knowledge, not programming one. In that case you are right. Good fast mindless coders are always valued
Col. Shrapnel
Good programmer will invent his own function for the long run. Not, as I have said, a big deal. But a coder will need such a crutches of course. But I'd prefer to have a coder who is able to do manual lookups without side assistance.
Col. Shrapnel
What does being mindless have to do with it? And mastering a programming language and knowing not to reinvent the wheel makes you less of a programmer? How does writing code like `$i = $i + 1` improve your theoretical knowledge?
Lèse majesté
It does not improve. It is just use of my theoretical knowledge. I can do it without knowing particular language specialties. And concentrate on more important things.
Col. Shrapnel
+3  A: 

Why not just

$string = sprintf('%s;%s;%s', $array[0], $array[1], $array[2]);
Gordon
or even `$string = "$array[0];$array[1];$array[2]";` but obviously it's not flexible solution, cannot change number of sliced items dynamically. but still excellent because of ease.
Col. Shrapnel
A: 

And here is one solution using iterators:

echo implode(';', iterator_to_array(
    new LimitIterator(
        new ArrayIterator(
            array('item1', 'item2', 'item3', 'item4', 'item5')), 0, 3)));

It's not in the spirit of keeping it simple, but it's working. I'd still go with my previous answer though.

Gordon