我想从此为网站创建一些epg。由于我没有经验,不幸的是我很难
我有两个数组
Array
(
[0] => Array
(
num] => 1
[name] => name 1
[epg_channel_id] => ch111
[added] => 1505435915
)
[1] => Array
(
num] => 2
[name] => name 2
[epg_channel_id] => ch222
[added] => 1505435915
)
[2] => Array
(
[num] => 3
[name] => name 3
[epg_channel_id] => ch333
[added] => 1505435915
)
[3] => Array
(
[num] => 4
[name] => name 4
[epg_channel_id] => ch444
[added] => 1505435915
)
第二个数组
Array
(
[0] => Array
(
[ch000] => Um9jayBJbiBSaW8=
)
[1] => Array
(
[ch111] => Um9jayBJbiBSaW8=
)
[2] => Array
(
[ch222] => Um9jayBJbiBSaW8=
)
[3] => Array
(
[ch333] => Um9jayBJbiBSaW8=
)
[4] => Array
(
[ch444] => Um9jayBJbiBSaW8=
)
我想输出这样的东西,使用epg_channel_id将第二个数组值推入第一个数组
Array
(
[0] => Array
(
[num] => 1
[name] => name 1
[epg_channel_id] => ch111
[added] => 1505435915
[ch111] => Um9jayBJbiBSaW8=
)
我试过array_recursive,数组合并并没有用。 我
答案 0 :(得分:2)
如果两个数组中的相应索引保证具有相同的通道ID,则这将非常有效。例如,如果$ array1 [0]保证与$ array2 [0]具有相同的通道ID,则此解决方案将很好地工作:
$combined = [];
foreach($array1 as $key=>$val){
$combined[$key] = $val + $array2[$key];
}
但是,如果不保证相应的索引具有相同的通道ID,则此解决方案将不起作用,并且您需要使用其他发布的答案之一。
如果您使用此方法,最后一个注意事项是,如果数组的大小不同,您将希望最大的数组为$ array1。所以,只需进行比较,看看哪个元素最多。
答案 1 :(得分:1)
您必须遍历两个数组才能获得所需的结果:因为您必须将第一个数组的$arr1 = Array
(
0 => Array
(
"num" => 1,
"name" => "name 1",
"epg_channel_id" => "ch111",
"added" => "1505435915",
),
1 => Array
(
"num" => 2,
"name" => "name 2",
"epg_channel_id" => "ch222",
"added" => "1505435915",
),
2 => Array
(
"num" => 3,
"name" => "name 3",
"epg_channel_id" => "ch333",
"added" => "1505435915",
),
3 => Array
(
"num" => 4,
"name" => "name 4",
"epg_channel_id" => "ch444",
"added" => "1505435915",
),
);
$arr2 = Array
(
0 => Array
(
"ch000" => "Um9jayBJbiBSaW8="
),
1 => Array
(
"ch111" => "Um9jayBJbiBSaW8="
),
2 => Array
(
"ch222" => "Um9jayBJbiBSaW8="
),
3 => Array
(
"ch333" => "Um9jayBJbiBSaW8="
),
4 => Array
(
"ch444" => "Um9jayBJbiBSaW8="
),
);
$new_array = array();
foreach($arr1 as $key=>$value)
{
foreach($arr2 as $key1=>$value1)
{
foreach($value1 as $key2=>$value2)
{
if($key2 == $value['epg_channel_id'])
{
$value[$key2]=$value2;
}
}
}
$new_array[$key]=$value;
}
print_r($new_array);
与第二个数组内部键匹配
{{1}}
答案 2 :(得分:1)
您可以使用第二个数组中的array_key_exists键存在或不存在,然后将其添加到新数组
$array = Array
(
Array
(
'num' => 1,
'name' => 'name 1',
'epg_channel_id' => 'ch111',
'added' => '1505435915',
),
Array
(
'num' => 2,
'name' => 'name 2',
'epg_channel_id' => 'ch222',
'added' => '1505435915',
),
Array
(
'num' => 3,
'name' => 'name 3',
'epg_channel_id' => 'ch333',
'added' => '1505435915',
),
Array
(
'num' => 4,
'name' => 'name 4',
'epg_channel_id' => 'ch444',
'added' => '1505435915'
)
);
$array2 = Array
(
Array
(
'ch000' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch111' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch222' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch333' => 'Um9jayBJbiBSaW8='
),
Array
(
'ch444' => 'Um9jayBJbiBSaW8='
)
);
$newArray =[];
foreach ($array as $key => $value) {
foreach ($array2 as $key2 => $value2) {
if (array_key_exists($value['epg_channel_id'], $value2)) {
$value[$value['epg_channel_id']] = $value2[$value['epg_channel_id']];
}
}
$newArray[] = $value;
}
echo "<pre>";
print_r($newArray);
答案 3 :(得分:0)
尝试阅读&#39; epg_channel_id&#39;的值来自array1 并通过获取&#39; ch111&#39;将其插入到array1本身来自array2
$ch_name = $array1[$i]['epg_channel_id'];
$id = $array1[$i]['num'];
$ch_value = $array2[$id]['$ch_name'];
$array1[$i]["$ch_name"] = $ch_value;
尝试为每个数组添加foreach
答案 4 :(得分:0)
array_merge_recursive适用于键为字符串的关联数组。将附加数字键。来自php.net
如果输入数组具有相同的字符串键,则这些键的值将合并到一个数组中,并且这是递归完成的,因此如果其中一个值是数组本身,则该函数将合并它另一个数组中的相应条目。但是,如果数组具有相同的数字键,则后面的值不会覆盖原始值,但会被追加。
您必须将数组转换为字符串键,或者使用一个循环逐个合并子数组。
答案 5 :(得分:0)
试试这个。我希望它能解决你的问题。我测试了它。
foreach ($array1 as $key => $value){
// echo $key;
foreach ($array2 as $i =>$item){
foreach ($item as $j=>$subitem){
if($value['epg_channel_id'] == $j){
$array1[$key][$j] = $subitem;
}
}
}
}
print_r($array1);
答案 6 :(得分:0)
ActiveSheet.Range("$A$1:$M$138").AutoFilter Field:=2, Criteria1:=Array("=*MY 18*", "=*MY18*") _
, Operator:=xlAnd, Criteria2:="<>*discussion*"