1

i want a key value pair from two different arrays there are two cases. 1) both are equal 2) second array is big than first array

my code is

$array1=array("anu","jinu");
$array2=array("1","2","0");

i want to make a pair like

  anu-1

  jinu-2

if $array1 elements are finished then do not have to pair with $array2 from the example we can ignore the third element in $array2 i tried to apply array_combine,but its not practical with two unequal arrays if anybody knows solution please help me

4
  • @mickmackusa i also saw that,but didnt understood well,thats why asked . :( Commented Nov 30, 2017 at 6:13
  • That's okay. In the future, if you find a duplicate or near duplicate that didn't help you, you should list it in your question. This proves to volunteers that you put effort into researching the question. The good news is this newer page provides a wider range of correct answers than that one. Please update your question to state that $array2 will never be shorter than $array1. Commented Nov 30, 2017 at 6:15
  • @mickmackusa updated Commented Nov 30, 2017 at 6:20
  • After re-reading my duplicate link very carefully, I will admit that that page was a RUBBISH dupe link. These are much better suited: stackoverflow.com/questions/4769213/… & stackoverflow.com/questions/19394980/… If anyone has the superpowers to update my closure with these new links, please do me the favor because I cannot do this. Commented Dec 1, 2017 at 6:30

5 Answers 5

1

If you don't want any loop and simply you have to use array_combine. Try following solution with array_slice

$array1=array("anu","jinu");
$array2=array("1","2","0");
$cnt1 = count($array1);
$cnt2 = count($array2);
if($cnt1>$cnt2)
    $array1=array_slice($array1, 0, $cnt2);
elseif($cnt2>$cnt1)
    $array2=array_slice($array2, 0, $cnt1);

$arr = array_combine($array1,$array2);
print_r($arr);

Note : It will check for both array. and take length of shorter array from both

DEMO

EDIT

If its fix that always array2 length is equal or more than array1 then you can ignore if -elseif statement. For that try following code

$array1=array("anu","jinu");
$array2=array("1","2","0");

$cnt1 = count($array1);
$array2=array_slice($array2, 0, $cnt1);   

$arr = array_combine($array1,$array2);
print_r($arr);
Sign up to request clarification or add additional context in comments.

2 Comments

could you please tell whats the else if statement do with array_slice?
Its for check length of array if array 1 is bigger then it will take only 2nd array's equivalent length array and vice-versa
1

try this

    $array1=array("anu","jinu");
    $array2=array("1","2","0");

    foreach($array1 as $key => $value){

     $newarray[$array2[$key]] = $value;

    }

    print_r($newarray);

7 Comments

How do you know which array is longer? Just guess and hope? Pick the wrong one and you will be trying to access a non-existent element. :(
@user7441072 This answer will cause Notices when the sizes are unbalanced the other way.
@mickmackusa yes,you are right.but in my issue,always second array will be large or both are equal.
@user7441072 that vital information should be in your question. AND if that is true, then you should be awarding the green tick to Sachin, Arun or Erwin
@mickmackusa B Desai solved the issue in every cases and also he gave explanation with demo. thats why i gave green tick to him
|
1

You can use the below code which output like you want

$arr1=array("anu","jinu");
$arr2=array("1","2","0");
$newArr = array();
foreach($arr1 as $key => $value){
   $newArr[$value] = $arr2[$key];
}
echo "<pre>";print_r($newArr);

3 Comments

How do you know which array is longer? Just guess and hope? Pick the wrong one and you will be trying to access a non-existent element. :(
@user7441072 This answer will cause Notices when the sizes are unbalanced the other way.
Here I assume that you always prefer to work with $array1. If this is not the case then you can use count() function of php and according to that use array_slice() function
1

Using array_slice() and array_combine()

// Slice array2 to match count with array1
$slice = array_slice($array2, 0, count($array1)); 
// Combine using array1 as key, sliced array as value
$combine = array_combine($array1, $slice);        

print_r($combine); // Print result

1 Comment

How do you know which array is longer? Just guess and hope? Pick the wrong one and array_combine() will barf at you. :(
1

Code: (Demo)

$array1=array("anu","jinu");
$array2=array("1","2","0");

foreach($array1 as $k=>$v){
    if(!isset($array2[$k])) break;  // avoid "Notice: Undefined offset" and write early exit from loop
    $result[$v]=$array2[$k];
}

var_export($result);

Or

var_export(array_combine(array_intersect_key($array1,$array2),array_intersect_key($array2,$array1)));

Either Output:

array (
  'anu' => '1',
  'jinu' => '2',
)

Edit:

The OP says the second array will always be larger so, these are leaner methods:

foreach($array1 as $k=>$v){
    $result[$v]=$array2[$k];
}    
var_export($result);

and

var_export(array_combine($array1,array_slice($array2,0,sizeof($array1))));

and

var_export(array_combine($array1,array_intersect_key($array2,$array1)));    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.