简体   繁体   中英

merge two array in one array

I have two array, $result and $social_result. I have to merge both tables. social_icons_id of $result matches id of $social_result. If match then show link of $result array otherwise blank.

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. 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.

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 . 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 .

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.

$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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM