Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create a new associative array from two associative  arrays in PHP

Posted on 2013-01-21
14
Medium Priority
?
321 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
13 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 84

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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 84

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 111

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 111

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

783 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