0

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 
       ) 
   )
3
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by cereal
0

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);
}
}
1

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!

Edited by cereal

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.