0

I am new to PHP, willing to learn. I recently encountered an issue while completing an exercise. Here`s what I have to do:

I need to define, first, an multi-dimensional array witch contains the producer and the model of certain mobile phones

EG:

$model = array( 
    "manufacurer" => array ("model1", "model2", ...),
    "manufacurer2" => "array( "model3", model4", ...),

);  

Next task: Starting from the above array $model I have to generate another multi-dimensional array, let's call it $shop. It should look like this:

$shop = array("model1"=>array("manufacurer"=> "manufacurer1", 
                              "caractheristics" => array("lenght"=>...
                                 "wide"=>...,
                                 "weight"=>...)
            ), 
          "model2"=>array("manufacurer"=>...etc 

Here's my code:

 <?php 

$modele = array( 
            "Nokia" => array ("3310", "n8", "1100"),
            "Samsung" => array( "Galaxy S7", "Bean", "e220"),
            "Sony" => array("Xperia", "K750", "W810")
); 
print_r($modele);
// it has stored my values
echo "<br>";
$magazin = array(
            '$model["Nokia"][0]' => array(
                                                'manufacturer' => '$modele[2]'
// How do I use the values from the $model array to $shop array? If i print_r($model["Nokia"][0]) it returnes 3310, witch is ok, but when I print_r($magazin) it returns: Array ( [$modele["Nokia"][0]] => Array ( [producator] => $modele[2] ) )
)
);
print_r($magazin);

 ?>

4 Answers 4

1

Remove the single quotes

$magazin = array( $model["Nokia"][0] => array( 'manufacturer' => $modele[2] ) );

Also, modele is an associative array so you should use the key rather than the index in case you add/remove stuff at the beginning of the array:

$magazin = array( $model["Nokia"][0] => array( 'manufacturer' => $modele["Sony"] ) );

..also I'm guessing for manufacturer you were looking for the word "Sony" rather then the array it holds at that key.. which in this case you either just type "Sony" or you get the key at position 2

$magazin = array( $model["Nokia"][0] => array( 'manufacturer' => array_keys($modele)[2] ) );

Sign up to request clarification or add additional context in comments.

Comments

0

omit the ' from variable like:

$magazin = array($model["Nokia"][0] => array('manufacturer' => $modele[2]));

look also on this link

Comments

0
$magazin = array();
$magazin[$model['Nokia'][0]] = array(
  "manufacurer"=> "manufacurer1", 
  "caractheristics" => array(
    "lenght" => ...
    "wide" => ...,
    "weight" => ...
  )
);

Comments

0

when you quote it's always a string, so if you remove the quotes then your code would already work.
Here's an example with also added code to automate it;

<?php

$modelsByManufacturer = array(
    "Nokia" => array("3310", "n8", "1100"),
    "Samsung" => array("Galaxy S7", "Bean", "e220"),
    "Sony" => array("Xperia", "K750", "W810")
);

echo "<hr />";
print_r($modelsByManufacturer);

// if you'd hardcode it it would look like this:
$magazin = array(
    $modelsByManufacturer["Nokia"][0] => array(
        'manufacturer' => $modelsByManufacturer["Nokia"]
    )
);

echo "<hr />";
print_r($magazin);

// if you'd automate it it would look like this:

// create empty array to fill
$magazin = array();

// loop over the data source, use 'as $key => $value' syntax to get both the key and the value (which is the list of models)
foreach ($modelsByManufacturer as $manufacturer => $models) {
    // loop over the child array, the models to add them
    foreach ($models as $model) {
        $magazin[$model] = array(
            'manufacturer' => $manufacturer,
            'model'        => $model,
        );
    }
}

echo "<hr />";
print_r($magazin);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.