I have two arrays
Array 1:
array (size=3)
0 =>
array (size=7)
'id' => string '3' (length=1)
'name' => string 'Copy of Carnegie Page' (length=21)
'keywords' => string 'Master Of Business Administration - Professional' (length=48)
'description' => string 'Master Of Business Administration - Professional' (length=48)
'metadata_id' => string '3' (length=1)
'page_id' => string '2' (length=1)
'url' => string 'ucomm-test-site/programs/carnegie-copy' (length=38)
1 =>
array (size=7)
'id' => string '4' (length=1)
'name' => string 'Display Name Used in Link Text' (length=30)
'keywords' => string 'SEO Food' (length=8)
'description' => string 'SEO TIPS: Meta descriptions should be no more than 150-160 characters. Include the important keywords at the front of the meta description. Do not duplicate meta descriptions for each of your pages. These show up in Search Engine Results Pages and should include a CTA to help visitors choose your page to get the information they are seeking. ' (length=344)
'metadata_id' => string '4' (length=1)
'page_id' => string '3' (length=1)
'url' => string 'ucomm-test-site/programs/program-template2' (length=42)
2 =>
array (size=7)
'id' => string '5' (length=1)
'name' => string 'program' (length=7)
'keywords' => string 'blkah, blahj, blah' (length=18)
'description' => string 'blah blah blah' (length=14)
'metadata_id' => string '5' (length=1)
'page_id' => string '4' (length=1)
'url' => string 'ucomm-test-site/programs/program' (length=32)
Array 2:
0 =>
array (size=2)
'page_id' => string '2' (length=1)
'value' => string '100% Online' (length=11)
1 =>
array (size=2)
'page_id' => string '2' (length=1)
'value' => string 'Undergraduate' (length=13)
2 =>
array (size=2)
'page_id' => string '3' (length=1)
'value' => string 'Undergraduate' (length=13)
3 =>
array (size=2)
'page_id' => string '3' (length=1)
'value' => string 'Micro-credentials' (length=17)
4 =>
array (size=2)
'page_id' => string '4' (length=1)
'value' => string 'Certificates' (length=12)
5 =>
array (size=2)
'page_id' => string '4' (length=1)
'value' => string 'Undergraduate' (length=13)
I am trying to get the “value” of the second array when the “page_id” matches in both arrays
I have tried nested for loops with if statements to try and filter while using array_push and array_merge/ array_merge_recursive
foreach ($metadata_custom as $meta)
{
$testarray[] = array( $meta['page_id'] => $meta['value'] );
}
foreach ($metadata as $data)
{
$i = $data["page_id"];
foreach ($testarray as $array)
{
if( array_key_exists($i, $array))
{
$newArray[] = array_merge_recursive(array("value" => $array[$i]), $data);
}
}
}
The output i get is close, but instead of adding the “values” key it will just duplicate the element like so.
array (size=6)
0 =>
array (size=8)
'value' => string '100% Online' (length=11)
'id' => string '3' (length=1)
'name' => string 'Copy of Carnegie Page' (length=21)
'keywords' => string 'Master Of Business Administration - Professional' (length=48)
'description' => string 'Master Of Business Administration - Professional' (length=48)
'metadata_id' => string '3' (length=1)
'page_id' => string '2' (length=1)
'url' => string 'ucomm-test-site/programs/carnegie-copy' (length=38)
1 =>
array (size=8)
'value' => string 'Undergraduate' (length=13)
'id' => string '3' (length=1)
'name' => string 'Copy of Carnegie Page' (length=21)
'keywords' => string 'Master Of Business Administration - Professional' (length=48)
'description' => string 'Master Of Business Administration - Professional' (length=48)
'metadata_id' => string '3' (length=1)
'page_id' => string '2' (length=1)
'url' => string 'ucomm-test-site/programs/carnegie-copy' (length=38)
2 =>
array (size=8)
'value' => string 'Undergraduate' (length=13)
'id' => string '4' (length=1)
'name' => string 'Display Name Used in Link Text' (length=30)
'keywords' => string 'SEO Food' (length=8)
'description' => string 'SEO TIPS: Meta descriptions should be no more than 150-160 characters. Include the important keywords at the front of the meta description. Do not duplicate meta descriptions for each of your pages. These show up in Search Engine Results Pages and should include a CTA to help visitors choose your page to get the information they are seeking. ' (length=344)
'metadata_id' => string '4' (length=1)
'page_id' => string '3' (length=1)
'url' => string 'ucomm-test-site/programs/program-template2' (length=42)
3 =>
array (size=8)
'value' => string 'Micro-credentials' (length=17)
'id' => string '4' (length=1)
'name' => string 'Display Name Used in Link Text' (length=30)
'keywords' => string 'SEO Food' (length=8)
'description' => string 'SEO TIPS: Meta descriptions should be no more than 150-160 characters. Include the important keywords at the front of the meta description. Do not duplicate meta descriptions for each of your pages. These show up in Search Engine Results Pages and should include a CTA to help visitors choose your page to get the information they are seeking. ' (length=344)
'metadata_id' => string '4' (length=1)
'page_id' => string '3' (length=1)
'url' => string 'ucomm-test-site/programs/program-template2' (length=42)
4 =>
array (size=8)
'value' => string 'Certificates' (length=12)
'id' => string '5' (length=1)
'name' => string 'program' (length=7)
'keywords' => string 'blkah, blahj, blah' (length=18)
'description' => string 'blah blah blah' (length=14)
'metadata_id' => string '5' (length=1)
'page_id' => string '4' (length=1)
'url' => string 'ucomm-test-site/programs/program' (length=32)
5 =>
array (size=8)
'value' => string 'Undergraduate' (length=13)
'id' => string '5' (length=1)
'name' => string 'program' (length=7)
'keywords' => string 'blkah, blahj, blah' (length=18)
'description' => string 'blah blah blah' (length=14)
'metadata_id' => string '5' (length=1)
'page_id' => string '4' (length=1)
'url' => string 'ucomm-test-site/programs/program' (length=32)
What I want it to look like is:
array (size=6)
0 =>
array (size=8)
'value' =>
[0] =>string '100% Online' (length=11)
[1] =>string 'Undergraduate' (length=13)
'id' => string '3' (length=1)
'name' => string 'Copy of Carnegie Page' (length=21)
'keywords' => string 'Master Of Business Administration - Professional' (length=48)
'description' => string 'Master Of Business Administration - Professional' (length=48)
'metadata_id' => string '3' (length=1)
'page_id' => string '2' (length=1)
'url' => string 'ucomm-test-site/programs/carnegie-copy' (length=38)
Any help would be greatly appreciated!!
New contributor
Hunter Sweat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.