如何在php中选择具有值的多维数组中的数组

如何在php中选择具有值的多维数组中的数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,为了正确起见,让我们假设我有一个数组存储- array:3 [▼ 0 => "store3" 1 => "store" 2 => "store2" ] 这是保存值的数组 其他阵列产品保存所有数据:- array:2 [▼ 0 => array:2 [▼ "store" => "store" "product" => "1" ] 1 => array:2 [▼ "store" => "store"

为了正确起见,让我们假设我有一个数组
存储
-

array:3 [▼
  0 => "store3"
  1 => "store"
  2 => "store2"
]
这是保存值的数组

其他阵列
产品
保存所有数据:-

array:2 [▼
  0 => array:2 [▼
    "store" => "store"
    "product" => "1"
  ]
  1 => array:2 [▼
    "store" => "store"
    "product" => "4"
  ]
  2 => array:2 [▼
    "store" => "store2"
    "product" => "2"
  ]
  3 => array:2 [▼
    "store" => "store2"
    "product" => "3"
  ]
  4 => array:2 [▼
    "store" => "store3"
    "product" => "7"
  ]
  5 => array:2 [▼
    "store" => "store3"
    "product" => "11"
  ]
]
我想要的是,从
stores
数组中选择一个值,例如
store3
,然后将其与
products
数组进行比较,然后搜索并提取
products
数组中具有
store
store3
的所有数组,并将其存储在另一个名为
store3

我已经试过了,但它是非常错误的,我的意思是它没有工作!如果有人这么说,我会贴出来,但有人能做到吗

我的工作:-

$temp = array();
  for($i=0; $i<count($stores); $i++)
  {
    //$stores[$i] = array();

    foreach($products as $p)
    {
      if(session($stores[$i]) == $p['store'])
      {
        if(count(session($stores[$i])) == 0)
        {
          $temp['product'] = $p['product'];
          session($stores[$i])->push($temp['product']);
        }
        else if(!in_array($p['product'],$stores[$i]))
        {
          $temp['product'] = $p['product'];
          session($stores[$i])->push($temp['product']);
        }
      }
    }
  }
$temp=array();
对于($i=0;$ipush($temp['product']);
}
else if(!in_数组($p['product'],$stores[$i]))
{
$temp['product']=$p['product'];
会话($stores[$i])->推送($temp['product']);
}
}
}
}
按如下方式操作:-

$final_array = array();
foreach($array1 as $arr){
  foreach($array2 as $arr2){
     if($arr == $arr2['store']){
       $final_array[$arr]['product'][] = $arr2['product'];
     }
  }
}
echo "<pre/>";print_r($final_array);
$final_array=array();
foreach($array1作为$arr){
foreach($array2作为$arr2){
如果($arr==$arr2['store'])){
$final_数组[$arr]['product'][=$arr2['product'];
}
}
}
echo”“;print_r($final_数组);
输出:-

按如下操作:-

$final_array = array();
foreach($array1 as $arr){
  foreach($array2 as $arr2){
     if($arr == $arr2['store']){
       $final_array[$arr]['product'][] = $arr2['product'];
     }
  }
}
echo "<pre/>";print_r($final_array);
$final_array=array();
foreach($array1作为$arr){
foreach($array2作为$arr2){
如果($arr==$arr2['store'])){
$final_数组[$arr]['product'][=$arr2['product'];
}
}
}
echo”“;print_r($final_数组);
输出:-

使用

活样本:

使用

实时样本:

使用阵列过滤器

$store = 'store1';

$products = array_filter($products, function($product) use($store) {
    return (isset($product['store']) and $product['store'] == $store);
});

var_dump($products);
使用阵列过滤器

$store = 'store1';

$products = array_filter($products, function($product) use($store) {
    return (isset($product['store']) and $product['store'] == $store);
});

var_dump($products);

我制作了一个简单的函数来查找商店中的产品

function searchProduct($products,$storeName){
    $results =array();
    foreach($products as $product){
        if(in_array($storeName,array_values($product)))
           $results[] = $product;    
    }
    return $results;
 }
 print_r(searchProduct($products,'store3'));

我做了一个简单的函数来查找商店中的产品

function searchProduct($products,$storeName){
    $results =array();
    foreach($products as $product){
        if(in_array($storeName,array_values($product)))
           $results[] = $product;    
    }
    return $results;
 }
 print_r(searchProduct($products,'store3'));

array\u filter
似乎适合您的需要,但您也可以使用
foreach
循环来完成。
array\u filter
似乎适合您的需要,但您也可以使用
foreach
循环来完成。这是一个正确的想法!但问题是我希望为每个商店动态创建单独的阵列。我实际李在放完球后就做了:Pcomment@Alen很好,你自己做的。很高兴帮助你。干杯:):)对不起,我以为我可以把另一个问题标记为正确的!我只是想看看这是不是有个好主意!但问题是我想为每个存储动态创建单独的数组comment@Alen很好,你自己做的。很高兴帮助你。干杯:)::)对不起,我以为我可以把另一个问题标记为正确的!我只是检查一下