问题
I have a snippet like,
Assume different length of array like,
length of $name1Array
is 8 and length of $name2Array
is 5
for($i = 0; $i < count ( $name1Array ); $i ++)
{
$finalArray ["something"] [] = array (
"name1" => $name1Array [$i],
"name2" => $name2Array [$i]
);
}
The above snippet is working all fine, but when the lengths of $name1Array and $name2Array are different? I tried 2 for loops for each one like,
for($i = 0; $i < count ( $name1Array ); $i ++)
{
$finalArray ["something"] [] = array (
"name1" => $name1Array [$i]
);
}
for($i = 0; $i < count ( $name2Array ); $i ++)
{
$finalArray ["something"] [] = array (
"name2" => $name2Array [$i]
);
}
This didnt work, is there any work around for it?
回答1:
One Of sollition is to Find Max Count value from both array and loop till max array val.
<?php
$array1 = array("a","b","c","d");
$array2 = array("1","2","3","4","5");
$count1 = count ( $array1 );$count2 = count ( $array2 );
$looptill = max($count1,$count2);
for($i = 0; $i < $looptill ; $i ++)
{
$finalArray ["something"] [] = array (
"name1" => $array1 [$i],
"name2" => $array2 [$i]
);
}
echo "<pre>";
print_r($finalArray);
exit;
回答2:
It's not clear what your expected result should be, so I'm guessing here that you want the longest possible result by filling missing values with null
or another empty value.
Try this:
$c1 = count($name1Array);
$c2 = count($name2Array);
$cmax = max($c1, $c2);
for ($i = 0; $i < $cmax; $i++)
{
$finalArray["something"][] = [
"name1" => $i < $c1 ? $name1Array[$i] : null,
"name2" => $i < $c2 ? $name2Array[$i] : null
];
}
As an alternative, you may want the shortest result by omitting those that are not present in both arrays:
$c1 = count($name1Array);
$c2 = count($name2Array);
$cmin = min($c1, $c2);
for ($i = 0; $i < $cmin; $i++)
{
$finalArray["something"][] = [
"name1" => $name1Array[$i],
"name2" => $name2Array[$i]
];
}
The fact that you didn't specify which of the two (longest/shortest) versions you need is an indication that you didn't think the problem through and probably are doing just homework... anyways, you're just hurting yourself this way.
回答3:
If you wish to push null
elements where missing from the original arrays, array_map()
is a direct one-liner:
$array1 = array(1, 2, 3, 4, 5, 6, 7, 8);
$array2 = array('a', 'b', 'c', 'd', 'e');
$finalArray['something'] = array_map(function($v1, $v2){return ['name1' => $v1, 'name2' => $v2];}, $array1, $array2);
var_export($finalArray);
*array_map()
does you the favor of serving null
values when the input array is expired.
If you don't want to see any null
values, a for()
loop is probably a better choice:
for ($i = 0, $max = max(sizeof($array1), sizeof($array2)); $i < $max; ++$i) {
$tmp = [];
if (isset($array1[$i])) { // this condition is important to avoid Notices
$tmp['name1'] = $array1[$i];
}
if (isset($array2[$i])) { // this condition is important to avoid Notices
$tmp['name2'] = $array2[$i];
}
$final['something'][] = $tmp;
}
var_export($final);
Here is a demonstration.
来源:https://stackoverflow.com/questions/51354958/push-items-to-associative-array-individually-in-php