简体   繁体   中英

How to merge these two arrays

I have a $distNames array which contain district names.

$distNames = array(0 => "North",1 => "West",2 => "South",3 => "East");

I have another array named $rdata which contain a count for a district.

$rdata = array("North" => array("rural" => 2, "urban" => 0), 
   "East" => array("rural" => 1, "urban" => 0));

I have another array named $udata which contain a count for a district.

$rdata = array("South" => array("rural" => 0, "urban" => 1), 
    "East" => array("rural" => 0, "urban" => 2));

I have to merge $rdata and $udata according to district array. Expected result will be like this.

$data = array(
      'North' => array('rural' =>2,'urban' => 0,'total' => 3),
      'South' => array('rural' =>0,'urban' => 1,'total' => 1),
      'West' => array('rural' =>0,'urban' => 0,'total' => 0),
      'East' => array('rural' =>1,'urban' => 2,'total' => 3)
    );

The total value should be the sum of rural and urban. Any help appreciated.

$data = array(); 
for ($i = 0; $i < count($distNames); $i++) {
    $tempArr = array();
    $direction = $distNames[$i];
    $data[$direction] = array(
        'rural' => isset($rdata[$direction]['rural']) ? $rdata[$direction]['rural'] : 0,
        'urban' => isset($udata[$direction]['urban']) ? $udata[$direction]['urban'] : 0,
        'total' => $rdata[$direction]['rural'] + $udata[$direction]['urban']
    ); 
}

echo "<pre>";
print_r($data);

This will work with respect to the names of arrays defined by you ie $data, $distNames, $rdata and $udata

Hii try to use this.

<?php
$distNames = array(0 => "North",1 => "West",2 => "South",3 => "East");
$rdata = array("North" => array("rural" => 2,"urban" => 0),"East" => array("rural" => 1,"urban" => 0));
$udata = array("South" => array("rural" => 0,"urban" => 1),"East" => array("rural" => 0,"urban" => 2));

    $data = array();
foreach($distNames as $drname){
   $data[$drname]['rural'] = ($rdata[$drname]['rural'] + $udata[$drname]['rural']);
   $data[$drname]['urban'] = ($rdata[$drname]['urban'] + $udata[$drname]['urban']);
   $data[$drname]['total'] = ($new[$drname]['urban'] + $new[$drname]['rural']);

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

i have done it like this

$data = array();
    $total = 0;
    for ($i = 0; $i < count($distNames); $i++) {
      $direction = $distNames[$i];
      if(isset($tlRuralNw[$direction]) && isset($tlUrbanNw[$direction])) {
        $total = $tlRuralNw[$direction] + $tlUrbanNw[$direction];
      }
      elseif(isset($tlRuralNw[$direction]) && !isset($tlUrbanNw[$direction])) {
        $total = $tlRuralNw[$direction];
      }
      elseif(!isset($tlRuralNw[$direction]) && isset($tlUrbanNw[$direction])) {
        $total = $tlUrbanNw[$direction];
      }
      elseif(!isset($tlRuralNw[$direction]) && !isset($tlUrbanNw[$direction])) {
        $total = 0;
      }
      $data[$direction] = array(
        'rural' => isset($tlRuralNw[$direction])?$tlRuralNw[$direction]:0,
        'urban' => isset($tlUrbanNw[$direction])?$tlUrbanNw[$direction]:0,
        'total' => $total
      );
    }

Thanks guys for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM