note 85323 modified in function.array-combine by thiago

From: Date: Tue, 26 Aug 2008 12:49:06 +0000
Subject: note 85323 modified in function.array-combine by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143835@lists.php.net to get a copy of this message
<?php // If they are not of same size, here is solution: $abbreviations = array("AL", "AK", "AZ", "AR", "TX", "CA"); $states = array("Alabama", "Alaska", "Arizona", "Arkansas"); function combine_arr($a, $b) { $acount = count($a); $bcount = count($b); $size = ($acount > $bcount) ? $bcount : $acount; $a = array_slice($a, 0, $size); $b = array_slice($b, 0, $size); return array_combine($a, $b); } $combined = combine_arr($abbreviations, $states); print_r($combined); // Output // Array ( [AL] => Alabama [AK] => Alaska [AZ] => Arizona // [AR] => Arkansas ) ?> --was-- // IF THEY ARE NOT OF SAME SIZE , HERE IS A SOLUTION : $abbreviations = array("AL", "AK", "AZ", "AR", "TX", "CA"); $states = array("Alabama", "Alaska", "Arizona", "Arkansas"); function combine_arr($a, $b) { $acount = count($a); $bcount = count($b); $size = ($acount > $bcount) ? $bcount : $acount; $a = array_slice($a, 0, $size); $b = array_slice($b, 0, $size); return array_combine($a, $b); } $combined = combine_arr($abbreviations, $states); print_r($combined); // Output // Array ( [AL] => Alabama [AK] => Alaska [AZ] => Arizona // [AR] => Arkansas ) http://php.net/manual/en/function.array-combine.php

« previous php.notes (#143835) next »