0

Thsi is my array structure.

$arr = array(
    'Tablet'=>array('test1','test2'),
    'Medicine'=>array('abc1'.'abc2'),
    'Dosage'=>array('1','1'),
    'Mode'=>array('testmode1','testmodel2'),
    'Since'=>array('mng','mng')
);

I want to create an array like this.

array
  0 => 
    array
      'Tablet' => string 'test1' (length=5)
      'Medicine' => string 'abc1' (length=4)
      'Dosage' => string '1' (length=1)
      'Mode' => string 'testmode1' (length=9)
      'Since' => string 'mng' (length=3)
  1 => 
    array
      'Tablet' => string 'test2' (length=5)
      'Medicine' => string 'abc1' (length=4)
      'Dosage' => string '1' (length=1)
      'Mode' => string 'testmode2' (length=9)
      'Since' => string 'mng' (length=3)

Can someone pls help?

Sorry for bad english.

Thanks

5 Answers 5

1
$new = [];
foreach ($arr as $key => $sub) {
    $i = 0;
    foreach ($sub as $value) {
        $new[$i][$key] = $value;
        $i++;
    }
}

That should do the trick :)

Sign up to request clarification or add additional context in comments.

Comments

1
$newArr = array();

foreach ( $arr as $key => $subArr ){
   for ($i= 0; $i < count($subArr); $i++){
       if (!array_key_exists($i, $newArr)){
            $newArr[$i] = array();
       }
       $newArr[$i][$key] = $subArr[$i];
   }
}

Comments

1
   $arr = array(
        'Tablet'=>array('test1','test2'),
        'Medicine'=>array('abc1','abc2'),
        'Dosage'=>array('1','1'),
        'Mode'=>array('testmode1','testmodel2'),
        'Since'=>array('mng','mng')
    );
        $na=array();
        foreach($arr as $k=>$v){

          foreach($v as $kk=>$vv){
            $na[$kk][$k]=$vv;
          }

        }

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

Comments

0

Easy you can append each one to normal array like this

$arr1 = array(
    'Tablet'=>array('test1','test2'),
    'Medicine'=>array('abc1'.'abc2'),
    'Dosage'=>array('1','1'),
    'Mode'=>array('testmode1','testmodel2'),
    'Since'=>array('mng','mng')
);

$arr2 = array(
    'Tablet'=>array('test1','test2'),
    'Medicine'=>array('abc1'.'abc2'),
    'Dosage'=>array('1','1'),
    'Mode'=>array('testmode1','testmodel2'),
    'Since'=>array('mng','mng')
);

$arr3 = array(
    'Tablet'=>array('test1','test2'),
    'Medicine'=>array('abc1'.'abc2'),
    'Dosage'=>array('1','1'),
    'Mode'=>array('testmode1','testmodel2'),
    'Since'=>array('mng','mng')
);


$a=array();

array_push($a,arr1,arr2,arr3);

print_r($a);

output will look

array
  0 => 
    array
      'Tablet' => string 'test1' (length=5)
      'Medicine' => string 'abc1' (length=4)
      'Dosage' => string '1' (length=1)
      'Mode' => string 'testmode1' (length=9)
      'Since' => string 'mng' (length=3)
  1 => 
    array
      'Tablet' => string 'test2' (length=5)
      'Medicine' => string 'abc1' (length=4)
      'Dosage' => string '1' (length=1)
      'Mode' => string 'testmode2' (length=9)
      'Since' => string 'mng' (length=3)

Comments

0
$myLength = count($arr['Tablet'])
$newArray = array();
for (i=0; i < $myLength ; i++)
{
    $newArray[i]['Tablet'] = $arr['Tablet'][i];
    $newArray[i]['Medicine'] = $arr['Medicine'][i];
    $newArray[i]['Dosage'] = $arr['Dosage'][i];
    $newArray[i]['Mode'] = $arr['Mode'][i];   
    $newArray[i]['Since'] = $arr['Since'][i];    
}

What do you meant adding the (lentgh = x) in your question?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.