Jump to content

Array problem


drisate

Recommended Posts

Hey guys i have an array that looks like this

Array
(
   [0] => Array
    (
	    [Marque] => AUDI
	    [Famille] => A4 Avant N1
    )
   [1] => Array
    (
	    [Marque] => AUDI
	    [Famille] => A3
    )
   [2] => Array
    (
	    [Marque] => AUDI
	    [Famille] => A5
    )
   [3] => Array
    (
	    [Marque] => AUDI
	    [Famille] => A4
    )
)

 

i need an array that looks like this:

 

Array

(

[AUDI] => Array

(

[0] => A4 Avant N1

[1] => A3

[2] => A5

[3] => A4

)

 

)

 

so fare i got this going:

 

if (isset($_GET['debug'])){

$stri=array();

$i=0;

foreach($csv_array as $car){

if ( !in_array("$car[Marque]", $stri) ) {

$stri[] = "$car[Marque]";

}

if ( !in_array("$car[Famille]", $stri) ) {

$stri[$i][$car[Marque]][] = "$car[Famille]";

}

$i++;

}

 

echo "<pre>";

print_r($stri);

echo "</pre>";

 

but that gives me

 

Fatal error: Cannot use string offset as an array

Link to comment
Share on other sites

<?php
$arr = array (
    0 => Array
		    (
				    'Marque' => 'AUDI',
				    'Famille' => 'A4 Avant N1'
		    ),
    1 => Array
		    (
				    'Marque' => 'AUDI',
				    'Famille' => 'A3'
		    ),
    2 => Array
		    (
				    'Marque' => 'AUDI',
				    'Famille' => 'A5'
		    ),
    3 => Array
		    (
				    'Marque' => 'AUDI',
				    'Famille' => 'A4'
		    ),
    4 => Array
		    (
				    'Marque' => 'MERC',
				    'Famille' => 'SL'
		    ),
    5 => Array
		    (
				    'Marque' => 'MERC',
				    'Famille' => 'CLK'
		    )
    );

$new = array();
foreach ($arr as $car) {
   if (!isset($new[$car['Marque']])) {
    $new[$car['Marque']] = array();
   }
   $new[$car['Marque']][] = $car['Famille'];
}

echo '<pre>',print_r($new, true),'</pre>';

?>

RESULTS

Array
(
   [AUDI] => Array
    (
	    [0] => A4 Avant N1
	    [1] => A3
	    [2] => A5
	    [3] => A4
    )

   [MERC] => Array
    (
	    [0] => SL
	    [1] => CLK
    )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.