hey guys, how to get all word in first index in array below and next index get last word in array 2 D??,.. example:

$arr = array( 
  array( 
    '7'=>'repsol kawasaki honda ktm', 
    '8'=>'kawasaki honda ktm bmw', 
    '9'=>'honda ktm bmw ducati', 
    '10'=>'ktm bmw ducati yamaha'
  ) , 
  array( 
    '23'=>'lamborghi ferarri mercedes hyundai', 
    '24'=>'ferarri mercedes hyundai toyota',
    '25'=>'mercedes hyundai toyota nissan',
    '26'=>'hyundai toyota nissan renault'
  ), 
);

I want yield like this:

   Array ( 
     [0] => Array (
       [7] => repsol kawasaki honda ktm 
       [8] => bmw 
       [9] => ducati
       [10] => yamaha 
      ) 
     [1] => Array ( 
       [23] => lamborghi ferarri mercedes hyundai 
       [24] => toyota 
       [25] => nissan  
       [26] => renault 
       ) 
   )

Recommended Answers

All 3 Replies

before thank's so much,i've try make code but the result no yet exactly, trouble with how to get all word in first index, this my code:

$result = array();
foreach($data as $x => $y){
foreach($y as $i => $j){
$result[$x][0] = reset($y);
$end = strrpos($j," ");
$result[$x][] = substr($j,$end);
}
}

Try this:

<?php

$arr = array( 
  array( 
    '7'=>'repsol kawasaki honda ktm', 
    '8'=>'kawasaki honda ktm bmw', 
    '9'=>'honda ktm bmw ducati', 
    '10'=>'ktm bmw ducati yamaha'
  ), 
  array( 
    '23'=>'lamborghi ferarri mercedes hyundai', 
    '24'=>'ferarri mercedes hyundai toyota',
    '25'=>'mercedes hyundai toyota nissan',
    '26'=>'hyundai toyota nissan renault'
  ), 
);

$z = array();
foreach($arr as $k1 => $v1)
{
    $i = 0;

    foreach($v1 as $k2 => $v2)
    {
        if($i == 0)
        {
            $z[$k1][$k2] = $v2;
        }
        else
        {
            $r = explode(' ',$v2);
            $z[$k1][$k2] = end($r); # use end() to get last element in array
        }
        $i++;
    }

}

print_r($z);
?>

It will output as requested:

Array
(
    [0] => Array
        (
            [7] => repsol kawasaki honda ktm
            [8] => bmw
            [9] => ducati
            [10] => yamaha
        )

    [1] => Array
        (
            [23] => lamborghi ferarri mercedes hyundai
            [24] => toyota
            [25] => nissan
            [26] => renault
        )

)

http://www.php.net/manual/en/function.end.php
bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.