PHP array_combine()

📔 : PHP 🔗

PHP में array_combine() method , दो Indexed Array Elements को आपस में key => value pair में Associate करके एक नया Associative Array बनाता है।


इस method में पहले pass किये गए Array values को keys बनता है , और बाद में pass किये गए Array की values को उन keys के साथ Associate करता है।

PHP array_combine() Syntax

array_combine ( array $keys , array $values );

Parameters
  1. array $keys | required : यह वो Array variable है जिसकी values को key बनाया जाता है।

  2. array $values | required : इस Array variable की values को as a value use किया जाएगा।

  3. Return Value : एक नया Associative Array मिलता है। अगर दोनों Arrays की value same नहीं हुई तो false return होता है।

PHP array_combine() Example

File : php_array_combine.php

Copy Fullscreen Close Fullscreen
<?php 
  $a = array('green', 'red', 'yellow');
  $b = array('avocado', 'apple', 'banana');
  $c = array_combine($a, $b);
  print_r($c);
?>
Output
Array
(
    [green] => avocado
    [red] => apple
    [yellow] => banana
) 

Important

अगर pass किये गए Array में key same हुई तो value हमेशा बाद वाली key के साथ ही associate होती है।
For Example :

 print_r( 
   array_combine(
     ['name', 'name', 'age'], 
     ['Rahul Kumar', 'Rahul Rajput', 23] 
   )
);

Ouput
Array ( [name] => Rahul Rajput [age] => 23 )

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook