[英]merge two array in one array
I have two array, $result and $social_result.我有两个数组,$result 和 $social_result。 I have to merge both tables.
我必须合并两个表。 social_icons_id of $result matches id of $social_result.
$result 的 social_icons_id 匹配 $social_result 的 id。 If match then show link of $result array otherwise blank.
如果匹配则显示 $result 数组的链接,否则为空白。
I have an array我有一个数组
Array
(
[0] => Array
(
[social_icons_id] => 14
[link] => www.instagram.com
[edittemplate_id] => 218
[name] => Email
[image] => email.png
)
[1] => Array
(
[social_icons_id] => 16
[link] => www.instagram.com
[edittemplate_id] => 218
[name] => Blogger
[image] => blogger.png
)
)
Another is:另一个是:
Array
(
[0] => Array
(
[id] => 13
[name] => Address
[image] => address.png
)
[1] => Array
(
[id] => 14
[name] => Email
[image] => email.png
)
[2] => Array
(
[id] => 15
[name] => Fax
[image] => fax.png
)
[3] => Array
(
[id] => 16
[name] => Text
[image] => text.png
)
[4] => Array
(
[id] => 17
[name] => Website
[image] => Website.png
)
)
Now I have to merge both table in one table like:现在我必须将两个表合并到一个表中,例如:
Array
(
[0] =>
[1] => www.instagram.com
[2] =>
[3] =>
[4] =>
[5] => www.instagram.com
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
)
id of both tables matches and make one table.两个表的 id 匹配并制作一张表。 I tried-
我试过了-
$result = $obj->select_social_ids($id); // for first table
$social_result = $obj->show_social_icons(); // for second table
for($j=0;$j<count($social_result);$j++)
{
if(in_array($social_result[$j]['id'], $result)) { // search value in the array
$link[] = $result[$j]['link'];
}
else
{
$link[] = '';
}
}
But not working.但不工作。
Depending on where you're getting this information from (eg a database table), doing this operation in SQL may make more sense.根据您从何处获取此信息(例如数据库表),在 SQL 中执行此操作可能更有意义。
That said, given the data and code you've provided, I think your in_array()
check is incorrect, as it will only check the top level of $result
.也就是说,鉴于您提供的数据和代码,我认为您的
in_array()
检查不正确,因为它只会检查$result
的顶层。 The 'social_icon_id'
value that you seem to want to compare to $social_results[$j]['id']
is contained in a nested array within $result
.您似乎想要与
$social_results[$j]['id']
进行比较的'social_icon_id'
值包含在$result
内的嵌套数组中。
You could do something like this:你可以这样做:
<?php
$results = $obj->select_social_ids($id);
$results_ids = array_map(
function ($result) { return $result['id']; },
$results
);
$results = array_combine($results_ids, $results);
$social_results = $obj->show_social_icons();
foreach ($social_results as $social_result) {
$id = $social_result['id'];
if (isset($results[$id])) {
$link[] = $results[$id]['link'];
}
else
{
$link[] = '';
}
}
If I understand your question correctly, you want to loop thru $social_result and compare ID to those keys in $result, maybe something like this will work.如果我正确理解了你的问题,你想通过 $social_result 循环并将 ID 与 $result 中的那些键进行比较,也许这样的事情会起作用。
$link = array();
foreach($social_result as $social){
$key = array_search($social['id'], array_column($result, 'social_icons_id'));
if($key != ''){
$link[] = $result[$key]['link'];
}else{
$link[] = '';
}
}
I tested this code and it works to do what I beliueve you are trying to accomplish我测试了这段代码,它可以做我相信你想要完成的事情
$a = array('social_icons_id' => '14','link' => 'www.instagram14.com','edittemplate_id' => '218','name' => 'Email','image' => 'email.png');
$b = array('social_icons_id' => '16','link' => 'www.instagram16.com','edittemplate_id' => '218','name' => 'Blogger','image' => 'blogger.png');
$result = array($a,$b);
$social_result = array(array('id'=>'14','name'=>'address0','image'=>'adress.png'),array('id'=>'15','name'=>'address1','image'=>'adress.png'), array('id'=>'16','name'=>'address2','image'=>'adress.png'),array('id'=>'17','name'=>'address3','image'=>'adress.png'),array('id'=>'18','name'=>'address4','image'=>'adress.png'),array('id'=>'19','name'=>'address5','image'=>'adress.png'));
$link = array();
foreach($social_result as $social){
$key = array_search($social['id'], array_column($result, 'social_icons_id'));
echo "<p> k ".$key;
if($key != ''){
$link[] = $result[$key]['link'];
}else{
$link[] = '';
}
}
print_r($link);
Simply you can create a simple left join query to generate a flat array containing social image links.您只需创建一个简单的左连接查询来生成包含社交图像链接的平面数组。
select social_image.link
from social_icons
left join social_image
on social_icons.id = social_image.social_icons_id
order by social_icons.id
But be carefully with array size limitation on php, therefore that needs a proper result limitation.但请注意 php 的数组大小限制,因此需要适当的结果限制。
select social_image.link
from social_icons
left join social_image
on social_icons.id = social_image.social_icons_id
order by social_icons.id
limit 1000
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.