管理Php数组

时间:2015-09-18 05:43:14

标签: php arrays

我是php的新手。我在php中学习了几乎所有的东西。我正在开发php中的示例应用程序。在我的应用程序中,我有两个数组,它看起来像:

阵 (     [0] => 1800     [5] => 1500     [6] => 4545 )

阵     (         [0] =>一个         [1] => b         [2] => C         [3] => d         [4] => Ë         [5] => F         [6] => G         )

我希望输出如下: 排列 (

d3.select("#link")
  .attr("href", d.url)
  .text(d.url);

) 请帮忙。

4 个答案:

答案 0 :(得分:1)

为您的问题找到解决方案。

<?php
$arr1 = array(
    '0' => '1800',
    '5' => '1500',
    '6' => '4545'
    );

$arr2 = array(
    '0' => 'a',
    '1' => 'b',
    '2' => 'c',
    '3' => 'd',
    '4' => 'e',
    '5' => 'f',
    '6' => 'g',
    );

$arr3 = array();
foreach($arr2 as $key => $value){
    if(!empty($arr1[$key])){
        $arr3[$key][$value] = $arr1[$key];
    }
    else{
        $arr3[$key][$value] = 0;
    }
}

print '<pre>';
print_r($arr3);
print'</pre>';
?>

答案 1 :(得分:0)

$arr1 = array(0 => 1800, 5 => 1500, 6 => 4545,);
$arr2 = array('a', 'b', 'c', 'd', 'e', 'f', 'g',);
$arrNeeded = array();

foreach ($arr2 AS $k => $v) {
    $arrNeeded[$k] = array($v => isset($arr1[$k]) ? $arr1[$k] : 0); 
}

echo '<pre>' . print_r($arrNeeded, true) . '</pre>';

答案 2 :(得分:0)

请尝试使用以下代码

$arr1 = array( 0 => 1800 ,5 => 1500 ,6=> 4545 );

$arr2 = array( 0 => 'a' ,1 => 'b',  2=> 'c',3 => 'd' ,4 => 'e' ,5 => 'f' ,6 => 'g' );
$newarr = "";

if(count($arr2) >0){

    foreach($arr2 as $key2 => $val2){
        $newVal = isset($arr1[$key2])?$arr1[$key2] : 0;
        $newarr[$key2] = array($val2 => $newVal);

    }
}
echo "<pre>";
print_r($newarr);

答案 3 :(得分:0)

这可能对您有所帮助

<?php 
$array1 = Array( "0"=>1800,"5" => 1500, "6" => 4545 ) ; 

$array2 = Array( "0" => 'a', '1' => 'b', '2' => 'c', '3' => 'd', '4' => 'e', '5' => 'f', '6' => 'g' );

$resultArray = array();

foreach ($array2 as $key2 => $value2 ) {
    $found = false;
    foreach ($array1 as $key1 => $value1) {

        if($key1 == $key2){

            $resultArray[$value2] = $value1;
            $found = true;
            continue;
        }
        if(!$found)
        {
            $resultArray[$value2] = 0;
        }
    }
}

print_r($resultArray);
?>