Solved

Create a new associative array from two associative  arrays in PHP

Posted on 2013-01-21
14
312 Views
Last Modified: 2013-03-07
Hello All,


Can anyone tell me how to create a new associate array from two associate array in PHP

e.g

$a1 = Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 3 [5] => 3 [6] => 3 [7] => 3 ) ;

$a2 = Array ( [0] => sometext1 [1] => sometext2 [2] => sometext3 [3] => sometext4 [4] => sometext5 [5] => sometext6  [6] => sometext7 [7] => sometext8 )

Open in new window


i want values in the 1st array to be used as keys, and the values is the 2nd array to be used as values.

overall the result should look like this

$var = Array ( 
  [1] => sometext1
  [1] => sometext2
  [1] => sometext3
  [2] => sometext4
  [3] => sometext5
  [3] => sometext6
  [3] => sometext7);

Open in new window


I had tried with array_combine and array_merge functions but still its not showing the exact results as i wanted


Kindly Please help me ASAP
0
Comment
Question by:SwiftCoder
  • 6
  • 3
  • 2
  • +1
14 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Use array_combine() (http://it1.php.net/array_combine):

$a3 = array_combine($a1, $a2);
0
 

Author Comment

by:SwiftCoder
Comment Utility
@marqusG : Thanks for reply but its not working as my two arrays are associative arrays..kindly post a new solution
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You can't get the results you have posted because PHP doesn't allow duplicate keys in arrays.  If a key already exists, then the next value for that key replaces (overwrites) the previous value.  It does not create a duplicated key with a new value.

If you do a 'var_dump' on your desired result array, you'll find that you only have 3 values.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Ooops, Dave is right, of course.  You could have an array where each key has as value a subarray with all values which fit that key:

$a1 = Array (1, 1, 1, 2, 3, 3, 3, 3) ;

$a2 = Array ( 'sometext1', 'sometext2', 'sometext3', 'sometext4', 'sometext5', 'sometext6', 'sometext7', 'sometext8' );
                                 
echo "<pre>";
var_dump($a1);
var_dump($a2);

 for ($i = 0; $i < count($a1); $i++){
  echo "$a1[$i] => $a2[$i]<br>";
  $a3[$a1[$i]][] = $a2[$i];
}
 
var_dump($a3);
0
 

Author Comment

by:SwiftCoder
Comment Utility
@ DaveBaldwin : yes, i'm getting only three results...is there any other solution, how come it will be sorted then ??
0
 

Author Comment

by:SwiftCoder
Comment Utility
Thanks guys for prompt reply's i had solved it
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Why not create an array of objects?  You can have the keys and the values stored in the objects, and the object-oriented strategy will let you keep duplicates.

<?php // RAY_temp_swiftcoder.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
echo '<pre>';

// TEST DATA FROM THE POST AT EE
$a1 = Array ( 0 => 1,           1 => 1,           2 => 1,           3 => 2,           4 => 3,           5 => 3,           6 => 3,           7 => 3 ) ;
$a2 = Array ( 0 => 'sometext1', 1 => 'sometext2', 2 => 'sometext3', 3 => 'sometext4', 4 => 'sometext5', 5 => 'sometext6', 6 => 'sometext7', 7 => 'sometext8' );

// DOES IT LOOK RIGHT? (YES)
print_r($a1);
print_r($a2);

// CREATE AN ARRAY OF OBJECTS
foreach ($a1 as $key => $value)
{
    $obj = new StdClass;
    $obj->key = $value;
    $obj->val = $a2[$key];
    $arr[] = $obj;
}

// SHOW THE NEW DATA STRUCTURE
print_r($arr);

Open in new window

Best to all, ~Ray
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Ray usually asks this.  If you tell us what you're trying to do, what the point is, maybe we can offer a better answer.
0
 

Author Comment

by:SwiftCoder
Comment Utility
@Ray_Paseur : my solved solution is similar to your reply...anyway thanks buddy..i got it now
0
 

Author Closing Comment

by:SwiftCoder
Comment Utility
Got some idea of the prob. Thx
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Sheesh!  What was so wrong with the answer that you gave it a bad grade?  You got a tested and working code example that showed exactly how to handle the data!  Here are the grading guidelines.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

I see that out of 4 questions, you've given 3 B's.  The answers here at EE are not that bad, especially if you engage with your fellow experts in a little dialog.  Please read the grading guidelines and get with the program.  You'll be glad you did.
0
 

Author Comment

by:SwiftCoder
Comment Utility
Sorry Ray, the grade was un-intentional, your answer definitely deserves "A"
Can I change the grade now?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You can ask a moderator to help.  Just click the request attention button.  Don't worry about it -- no hard feelings at all, it's a common misunderstanding with new EE users.  I just don't want you to get a rep as someone who gives marked-down grades.  That could make some in the community less than eager to help, and it's usually best to get as many sets of eyes on a problem as possible!

Best regards, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now