How to get the value and keys of a multidimensional array?

advertisements

I am using a foreach loop to get the values of an multidimenssional array. Loop is as follows:

foreach ($value as $k => $v) {
  print_r($v);
}

Output of this loop is as follows:

Array
(
  [1] => task1admin
  [status] => 0
)
Array
(
  [50] =>  task2pltfrom
  [status] => 0
)

I just want to store keys [1],[50] in a variable also want to store values of key [1]=> its value and [50]=> its value.

Moreover, i also want to get 'status' values.

I am new and confused with multidimensional array. Thanx a ton in advance.


The solution using key and current functions:

$arr = [
    Array( 1 => 'task1admin', 'status' => 0 ),
    Array( 50 => 'task2pltfrom', 'status' => 0 )
];

// base structure
$result = ['uid' => [], 'activity' => [], 'status' => []];
foreach ($arr as $v) {
    $result['status'][] = $v['status'];
    $result['uid'][] = key($v);
    $result['activity'][] = current($v);
}

print_r($result);

The output:

Array
(
    [uid] => Array
        (
            [0] => 1
            [1] => 50
        )

    [activity] => Array
        (
            [0] => task1admin
            [1] => task2pltfrom
        )

    [status] => Array
        (
            [0] => 0
            [1] => 0
        )
)