0

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.

7
  • have you considered array_merge? Commented May 11, 2020 at 12:18
  • How to to that? Commented May 11, 2020 at 12:20
  • php.net/manual/en/function.array-merge.php Commented May 11, 2020 at 12:21
  • I don't think array_merge should work in my condition. Commented May 11, 2020 at 12:22
  • Logic here is unclear to me. Commented May 11, 2020 at 12:24

3 Answers 3

1

Depending on where you're getting this information from (e.g. 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[] = '';
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have to use social_icons_id from first array and match this, with id of second array. If match then print something otherwise blank. But sequence should be the second array
I read your original post, and I can't see a reason why the code I've provided wouldn't do what you're asking. What is different between the output you're wanting to see and the output of the code I've given you? You'll have to tell me more than that it's "not working" for me to help you further.
$link[] = $result[$j]['link']; Here $j is not found in first array. So only second row of first array is not printing.
0

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);

2 Comments

I have to use social_icons_id from first array and match this, with id of second array. If match then print something otherwise blank. But sequence should be the second array
I am stucked whole day. Can we connect with teamviewer please?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.