Php 按特定键拆分关联数组
这看起来应该很容易,但我很难找到一个函数来实现这一点 我有一个类似的数组Php 按特定键拆分关联数组,php,arrays,Php,Arrays,这看起来应该很容易,但我很难找到一个函数来实现这一点 我有一个类似的数组 Array ( [2338] => 181684562467 [2550] => 181645662467 [2658] => 182345645567 [2878] => 171756765267 ) 我需要能够手动指定键2658,2878 然后根据我输入的值将数组分为两个数组 Array ( [2338] => 181684562467 [
Array
(
[2338] => 181684562467
[2550] => 181645662467
[2658] => 182345645567
[2878] => 171756765267
)
我需要能够手动指定键2658
,2878
然后根据我输入的值将数组分为两个数组
Array
(
[2338] => 181684562467
[2550] => 181645662467
)
&
最有效的方法是什么
下面是数组最初是如何生成的(通过MySQL查询)
嗨,Brian,你说的手动是指通过用户输入?我说的手动是指我知道要拆分的键
2658
2878
。实际数组是一个100+的大列表,我想指定要拆分的确切密钥。如何插入所需的密钥?@pr1nc3我编辑了原始问题谢谢。调查这件事,试图修改。这并不是按说拆分(就像保留原始数组的密钥一样),而是创建一个新数组。太棒了,谢谢。保留密钥只需修改$myArray[$row]=$data\u array[$row]代码>
Array
(
[2658] => 182345645567
[2878] => 171756765267
)
$itemidquery = "SELECT * FROM wp_postmeta WHERE meta_key = '_itemids';";
$result = mysqli_query($conn, $itemidquery);
$data_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$data_array[$row['post_id']] = $row['meta_value'];
}
<?php
$array=Array
(
2338 => 181684562467,
2550 => 181645662467,
2658 => 182345645567,
2878 => 171756765267
);
//you set the values of the array seperated with comma!!!
$name = $_GET["name"];
if($name!=''){
//get the multiple values that are seperated by comma and create an array of them
$data=explode(',',$name);
$myArray=array();
foreach($data as $row){
//check if the key you insert exists in the array, if it does it inserts the value in the new array
if(array_key_exists ($row,$array)){
$myArray[]=$array[$row];
}
}
//the output based on the keys you insert
print_r($myArray);
}
http://localhost/testing_project.php?name=2658,2338