Solved

Create a new associative array from two associative  arrays in PHP

Posted on 2013-01-21
14
316 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 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
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.

 
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 109

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 109

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 109

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
PHP Form Calculate Total Price 10 43
Query Syntax 17 36
How to submit record from external php form to a Sharepoint list? 5 35
This is about my first experience with programming Arduino.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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…

803 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