Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create a new associative array from two associative  arrays in PHP

Posted on 2013-01-21
14
Medium Priority
?
320 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 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Make the most of your online learning experience.
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.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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