将数据从一个数组使用到另一个数组



我是PHP新手,愿意学习。我最近在完成一个练习时遇到了一个问题。下面是我要做的:

首先,我需要定义一个多维数组,其中包含某些手机的生产者和型号

,

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

下一个任务:从上面的数组$model开始,我必须生成另一个多维数组,我们称之为$shop。它应该看起来像这样:

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

下面是我的代码:

 <?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);
 ?>

去掉单引号

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

此外,modele是一个关联数组,所以如果你在数组的开头添加/删除东西,你应该使用键而不是索引:

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

. .另外,我猜对于制造商,你想找的是"索尼"这个词,而不是它在那个键上的阵列。在这个例子中,你可以输入"Sony"或者在位置2

处输入键

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

从变量中省略':

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

查看此链接

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

当你加引号时,它总是一个字符串,所以如果你去掉引号,那么你的代码就可以工作了。
下面是一个示例,其中还添加了代码来自动执行;

<?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);

相关内容

  • 没有找到相关文章

最新更新