显示两个数组值,用于比较PHP中的值

时间:2016-10-05 08:07:55

标签: php arrays

我有两个产品php数组,如下面

Array
(
    [0] => Audio Jack=3.5 mm%%3.5 mm
    [1] => Auto Focus=Yes%%Yes
    [2] => Battery Type=Li-Polymer%%Li-Polymer
    [3] => Battery capacity=1715 mAh%%1715 mAh
)

Array
(
    [0] => Audio Jack=1.5 mm%%1.5 mm
    [1] => Battery Type=Li-Polymer%%Li-Polymer
    [2] => Auto Focus=No%%No
    [3] => Battery capacity=3000 mAh%%3000 mAh
)

从这个数组中我想对该数组进行排序并显示那些数组值,比如下面这两个产品规范。

音频插孔 - 3.5 mm - 1.5 mm

自动对焦 - 是 - 否

电池类型 - 锂聚合物 - 锂聚合物

电池容量 - 1715 mAh - 3000 mAh

任何人都可以帮我用php显示。

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。提供数组总是包含相同的项目,并且键总是相同的,这应该可以。

$array1 = array( 'Audio Jack' => '3.5mm', 'Auto Focus' => 'Yes', 'Battery Type' => 'Li-Polymer' ); $array2 = array( 'Audio Jack' => '1.5mm', 'Auto Focus' => 'no', 'Battery Type' => 'Li-Polymer' ); foreach($array1 as $key => $value) { echo $key.' = '.$value.' - '.$array2[$key].'<br>'; }

点击您的更新后尝试此操作。

$array1 = array
(
    'Audio Jack=3.5 mm%%3.5 mm',
    'Auto Focus=Yes%%Yes',
    'Battery Type=Li-Polymer%%Li-Polymer',
    'Battery capacity=1715 mAh%%1715 mAh'
);

$array2 = array
(
   'Audio Jack=1.5 mm%%1.5 mm',
   'Battery Type=Li-Polymer%%Li-Polymer',
   'Auto Focus=No%%No',
   'Battery capacity=3000 mAh%%3000 mAh'
);

foreach($array1 as $key => $value)
{
    $value1 = explode('=', $value);
    $value2 = explode('=', $array2[$key]);

    $result1 = explode('%%', $value1[1]);
    $result2 = explode('%%', $value2[1]);

    echo $value1[0].' = '.$result1[0].' - '.$result2[0];
}

答案 1 :(得分:0)

    $a = [
    'Audio Jack'=>'3.5 mm',
    'Auto Focus'=>'Yes',
    'Battery Type'=>'Li-Polymer',
    'Battery capacity' => '1715 mAh'
];

$b = [
    'Audio Jack'=>'1.5 mm',
    'Auto Focus'=>'No',
    'Battery Type'=>'Li-Polymer',
    'Battery capacity' => '3000 mAh'
];

$combinedArr  = array_merge_recursive($a,$b);

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

    echo $key ." - ". implode(" - ", $value) ."<br />";

}