1

Hi this is my array where I want to get the single dimensional array as result as shown below please help anyone

 Array
(
[Maruti Suzuki] => Array
    (
        [0] => Swift
        [1] => Ritz
        [2] => Omni
        [3] => New Swift DZire
        [4] => Ertiga
        [5] => Eeco
    )

[Chevrolet] => Array
    (
        [0] => Cruze
    )

)

This is my array I want result of this array like this

array(
       [Maruti Suzuki] => Swift
       [Maruti Suzuki] => Ritz
       [Maruti Suzuki] => Omini
       [Maruti Suzuki] => Eeco
       [Chevrolet] => Cruze

     )
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54

3 Answers3

0

Simply use call_user_func_array as

$result = call_user_func_array('array_merge', $your_array);

Edited :

$arr = Array('Maruti Suzuki' => Array('Swift', 'Ritz', 'Omni', 'New Swift DZire', 'Ertiga', 'Eeco'), 'Chevrolet' => Array('Cruze'));
$result = array();
foreach($arr as $key => $value){
    $result[$key] = implode(',',$value);
}
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • how can get the key with the Maruti Suzuki and Chevrolet – vamshi goli Jul 21 '15 at 06:34
  • Sorry but I think that's not your question or I'm wrong at reading it – Narendrasingh Sisodia Jul 21 '15 at 06:45
  • can you please check the question again i have edited – vamshi goli Jul 21 '15 at 07:26
  • The array that you have posted as an expected output is not possible [Reason why its not possible](http://stackoverflow.com/questions/30636179/how-does-php-index-associative-arrays/30636430#30636430) but you can have another option you can separate you can have comma separated array values as `array([Maruti Suzuki] => Swift,Ritz,Omini,Eeco [Chevrolet] => Cruze)` I'll add an answer for the same too – Narendrasingh Sisodia Jul 21 '15 at 08:40
  • Sorry, I didn't get that – vamshi goli Jul 21 '15 at 13:36
  • When two identical index are defined, the last overwrite the first. Means as you have posted within your expected answer it'll generate an array as `array([Maruti Suzuki] => Eeco [Chevrolet] => Cruze)` – Narendrasingh Sisodia Jul 21 '15 at 13:51
0

edit To build your own new array [by hand]

  $result[] = $your_array['Maruti Suzuki'][0];
  $result[] = $your_array['Maruti Suzuki'][1];
  $result[] = $your_array['Maruti Suzuki'][2];
  $result[] = $your_array['Maruti Suzuki'][5];
  $result[] = $your_array['Chevrolet'][0];

To random your array use

 <?php
   $res  = call_user_func_array('array_merge', $your_array);
   $new = array_rand($res);
   $result = array();
   foreach($new as $key) $result[] = $res[$key];

To random your array and limit it to 5 use

 <?php
   $res  = call_user_func_array('array_merge', $your_array);
   $new = array_rand($res,5);
   $result = array();
   foreach($new as $key) $result[] = $res[$key];

In $result is your random new array in this case with new keys!

donald123
  • 5,638
  • 3
  • 26
  • 23
0

Try this, I have did what you want.

//sample array
$car = array
(
    'Maruti Suzuki' => array('Swift', 'Ritz', 'Omni', 'New Swift DZire', 'Ertiga', 'Eeco'),
    'Chevrolet' => array('Cruze')
);

$j = 0;
foreach ($car as $key => $value) 
{
    $count = count($value);
    for($i = 0; $i <$count;$i++)
    {
        $allCar[$key."~".$value[$i]] = $value[$i];
        $j++;   
    }
}
echo "<pre>";
print_r($allCar);

?>

Now, you can easily get the value with the key Maruti Suzuki and Chevrolet.

Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40
  • its not possible to get all the value using this two index, for the same index it can swap the value of key at the end you only have two values which is [Maruti Suzuki] => Eeco, [Chevrolet] => Cruze, if you can see my example you can easily retrieve the index as well as the vaule. – Ankur Tiwari Jul 21 '15 at 07:49