Solved

Create a new associative array from two associative  arrays in PHP

Posted on 2013-01-21
14
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
14 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38800322
Use array_combine() (http://it1.php.net/array_combine):

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

Author Comment

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

Expert Comment

by:Dave Baldwin
ID: 38800617
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38800645
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
ID: 38800646
@ 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
ID: 38800729
Thanks guys for prompt reply's i had solved it
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38801162
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 83

Expert Comment

by:Dave Baldwin
ID: 38802253
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
ID: 38804160
@Ray_Paseur : my solved solution is similar to your reply...anyway thanks buddy..i got it now
0
 

Author Closing Comment

by:SwiftCoder
ID: 38962706
Got some idea of the prob. Thx
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38962738
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
ID: 38962779
Sorry Ray, the grade was un-intentional, your answer definitely deserves "A"
Can I change the grade now?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38962831
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article discusses how to implement server side field validation and display customized error messages to the client.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

739 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