在php中从一个数组动态插入一个值到另一个数组

时间:2018-11-11 12:48:29

标签: php arrays dynamic

我在stackoverflow上看到了一些答案,但它不能满足我的需要。 所以我在php中有一个包含所有用户值的主数组,我已给用户选择了他们想选择哪个值的选项,然后从主数组中回显了这些值。 我想要一个数组,它将从主数组中获取这些值,该值将与我用来选择选项的ID相匹配。例如,如果id = 25,则应从id为= 25的主数组行中获取值。

注意:我将id值作为数组.like [16,18,23]可以是15到26之间的任何值。

array(

    array('id' => '15', 'title' => 'product5', 'brand_name' => 'brand5', 'price' => '1233'),
    array('id' => '16', 'title' => 'product4', 'brand_name' => 'brand4', 'price' => '1234'),
    array('id' => '17','title' => 'produc23', 'brand_name' => 'brand3', 'price' => '2222'),
    array('id' => '18','title' => 'produc2', 'brand_name' => 'brand2', 'price' => '455'),
    array('id' => '19', 'title' => "vivek's 1st product", 'brand_name' => 'vivek', 'price' => '1000'),
    array('id' => '20', 'title' => 'Slik Shirt', 'brand_name' => 'Ramraj', 'price' => '599'),
    array('id' => '21', 'title' => 'Bhagalpuri Kota Silk Saree', 'brand_name' => 'Vimalnath Synthetics', 'price' => '1299'),
    array('id' => '22', 'title' => 'fsdf', 'brand_name' => 'fsdf', 'price' => '200',),
    array('id' => '23', 'title' => 'wdw', 'brand_name' => 'qwqewq', 'price' => '2000'),
    array('id' => '24', 'title' => 'productawesome', 'brand_name' => 'awesome', 'price' => '1000'),
    array('id' => '25',  'title' => 'redmi 5A', 'brand_name' => 'redmi', 'price' => '6000'),
    array('id' => '26', 'title' => 'naruto_stickers', 'brand_name' => 'anime', 'price' => '200')
    )

如果有人找到答案,请帮助。

2 个答案:

答案 0 :(得分:0)

假设您的ID为$ id。并且将master数组分配给$ master。您可以循环浏览并检查ID。

$row = -1;
For($x =0; $x < count($master); $x++){
 If($master[$x]['id'] === $id){
   $row = $master[$x];
   break;
 }
}
//if row == -1 no match

答案 1 :(得分:0)

您可以像下面尝试使用array_filter()和in_array()

$master= array(

    array('id' => '15', 'title' => 'product5', 'brand_name' => 'brand5', 'price' => '1233'),
    array('id' => '16', 'title' => 'product4', 'brand_name' => 'brand4', 'price' => '1234'),
    array('id' => '17','title' => 'produc23', 'brand_name' => 'brand3', 'price' => '2222'),
    array('id' => '18','title' => 'produc2', 'brand_name' => 'brand2', 'price' => '455'),
    array('id' => '19', 'title' => "vivek's 1st product", 'brand_name' => 'vivek', 'price' => '1000'),
    array('id' => '20', 'title' => 'Slik Shirt', 'brand_name' => 'Ramraj', 'price' => '599'),
    array('id' => '21', 'title' => 'Bhagalpuri Kota Silk Saree', 'brand_name' => 'Vimalnath Synthetics', 'price' => '1299'),
    array('id' => '22', 'title' => 'fsdf', 'brand_name' => 'fsdf', 'price' => '200',),
    array('id' => '23', 'title' => 'wdw', 'brand_name' => 'qwqewq', 'price' => '2000'),
    array('id' => '24', 'title' => 'productawesome', 'brand_name' => 'awesome', 'price' => '1000'),
    array('id' => '25',  'title' => 'redmi 5A', 'brand_name' => 'redmi', 'price' => '6000'),
    array('id' => '26', 'title' => 'naruto_stickers', 'brand_name' => 'anime', 'price' => '200')
    );

    $selected  = array(15,18);
    print_r(array_filter($master,function($arr) use ($selected){
      return in_array($arr['id'],$selected);
    }));