php - My array is look like as result array and i want it like r1 and r2 -


i have kind of array:-

$result=array([city] => xyz [name] => array( [0] => xyz1 [1] => xyz2 ) [email] => array ( [0] => xyz1@gmail.com [1] => xyz2@gmail.com ) [phone] => array ( [0] => 23423423-1 [1] => 23423423-2 ) [address] => array ( [0] => xyz1 [1] => xyz2 ) [dis] => xyz);  

and want

$r1=array ( [city] => xyz [name] => xyz1 [email] => xyz1@gmail.com [phone] => 23423423-1 [address] => xyz1 [dis] => xyz );   $r2=array ([city] => xyz [name] => xyz2 [email] => xyz2@gmail.com [phone] => 23423423-2 [address] => xyz2 [city] => xyz ); 

this assumes have more 1 array process , sorts elements new, formatted array.

<?php $results = array(     array(         'city' => 'xyz',         'name' => array( 'xyz1', 'xyz2' ),         'email' => array( 'xyz1@gmail.com', 'xyz2@gmail.com' ),         'phone' => array( '23423423-1', '23423423-2' ),         'address' => array( 'xyz1', 'xyz2' ),         'dis' => 'xyz'     ) );  $formatted_results = array();   foreach( $results $result ) {      foreach( array(0,1) $key ) {         $formatted_results[][$key] = array(         'city' => $result['city'],         'name' => $result['name'][$key],         'email' => $result['email'][$key],         'phone' => $result['phone'][$key],         'address' => $result['address'][$key],         'dis' => $result['dis']         );     }  }  echo "<pre>";  print_r($formatted_results); 

this output

array (     [0] => array         (             [0] => array                 (                     [city] => xyz                     [name] => xyz1                     [email] => xyz1@gmail.com                     [phone] => 23423423-1                     [address] => xyz1                     [dis] => xyz                 )          )      [1] => array         (             [1] => array                 (                     [city] => xyz                     [name] => xyz2                     [email] => xyz2@gmail.com                     [phone] => 23423423-2                     [address] => xyz2                     [dis] => xyz                 )          )  ) 

you can access new values this:

foreach( $formatted_results $value ) {     $r1 = $value[0];     $r2 = $value[1]; } 

or if have single array:

$r1 = $formatted_results[0][0]; $r2 = $formatted_results[0][1]; 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -