-1

I am having this array :

array(
    'name'=> 'abc',
    'age'=> 30,
    'sex'=> 'male'
)

and i want to make this array in this way :

array(
    0 => 'name',
    1 => 'abc',
    2 => 'age',
    3 => 30,
    4 => 'sex',
    5 => 'male'
)
Matthew
  • 1,630
  • 1
  • 14
  • 19
haoliu
  • 33
  • 1
  • 2
    Oh wait, [that was](https://stackoverflow.com/questions/2815162/is-there-a-php-function-like-pythons-zip) the opposite (been mislead by your undescriptive question "title"). Anyway [`array_keys`](http://php.net/array_keys), [`array_values`](http://php.net/array_values), [`foreach`](http://php.net/foreach) should do. – mario Feb 26 '19 at 02:33
  • 1
    I can't understand why you want an array like that result. What is it good for? – Barmar Feb 26 '19 at 02:37
  • I'm sure you have some valid reason for wanting to do this. The problem with this question is that this is a pretty easy problem to solve with just a few Google searches. – Russ J Feb 26 '19 at 03:13

2 Answers2

2

here it is

$array1 = array(
'name'=> 'abc',
'age'=> 30,
'sex'=> 'male') ;
$array2 = [];
foreach ($array1 as $key => $value) {
array_push($array2,$key,$value); }
Zack Heisenberg
  • 499
  • 6
  • 12
1

You could do something like this:

$mArr = [ 
          'name'=> 'abc',
          'age'=> 30,
          'sex'=> 'male'
        ];
$oArr = [];
foreach($mArr as $k => $v) {
        $oArr[] = $k;
        $oArr[] = $v;
}
var_dump($oArr);
/*
array(6) {
  [0]=>
  string(4) "name"
  [1]=>
  string(3) "abc"
  [2]=>
  string(3) "age"
  [3]=>
  int(30)
  [4]=>
  string(3) "sex"
  [5]=>
  string(4) "male"
}
*/

Hope it helps.

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34