2

This is my array and i want 60 , 20, 39, 70,12, 29,31,72,59 in a single array. one-dimensional array.

$marks = array(
    "abc" => array(
    "a" => 60,
    "b" => 20,
    "c" => 39
    ),
    "def" => array(
    "a" => 70,
    "b" => 12,
    "c" => 29
    ),
    "xyz" => array(
    "a" => 31,
    "b" => 72,
    "c" => 59
    )
 );

my try was

foreach($marks as $name=>$score)
{
   foreach($score as $subject=>$number)
     { 
$array[]= $number;
    } 

}

But when i am printing this array it again generate three array. on print_r($array); its showing this output.

Array ( [0] => 60 [1] => 20 [2] => 39 ) Array ( [0] => 60 [1] => 20 [2] => 39 [3] => 70 [4] => 12 [5] => 29 ) Array ( [0] => 60 [1] => 20 [2] => 39 [3] => 70 [4] => 12 [5] => 29 [6] => 31 [7] => 72 [8] => 59 )

is there any method to get only last array from the above array.or any other solution.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Aarush Sen
  • 39
  • 6

5 Answers5

1

Try this to flatten the array:

<?php

$marks = array(
    "abc" => array(
    "a" => 60,
    "b" => 20,
    "c" => 39
    ),
    "def" => array(
    "a" => 70,
    "b" => 12,
    "c" => 29
    ),
    "xyz" => array(
    "a" => 31,
    "b" => 72,
    "c" => 59
    )
 );

function array_values_recursive($array)
{
    $arrayValues = array();

    foreach ($array as $value)
    {
        if (is_scalar($value) OR is_resource($value))
        {
             $arrayValues[] = $value;
        }
        elseif (is_array($value))
        {
             $arrayValues = array_merge($arrayValues, array_values_recursive($value));
        }
    }

    return $arrayValues;
}

 var_dump(array_values_recursive($marks));

Output:

array(9) { [0]=> int(60) [1]=> int(20) [2]=> int(39) [3]=> int(70) [4]=> int(12) [5]=> int(29) [6]=> int(31) [7]=> int(72) [8]=> int(59) }

This custom function was taken from: http://php.net/manual/en/function.array-values.php

Rubén Cougil
  • 53
  • 1
  • 10
1

you can do in either traditional way i.e. foreach loop and also can use iterator. have a look on below solution:

1) using foreach loop and array_merge function

$marks = array(
    "abc" => array(
        "a" => 60,
        "b" => 20,
        "c" => 39
    ),
    "def" => array(
        "a" => 70,
        "b" => 12,
        "c" => 29
    ),
    "xyz" => array(
        "a" => 31,
        "b" => 72,
        "c" => 59
    )
);

$new_array = array();
foreach ($marks as $mark) {
    $new_array = array_merge($new_array, array_values($mark));
}

print_r($new_array);

2) using ArrayIterator:

$new_array = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($marks));
foreach ($iterator as $key => $value) {
    $new_array[] = $value;
}

print_r($new_array);

In both solution Output will be:

Array
(
    [0] => 60
    [1] => 20
    [2] => 39
    [3] => 70
    [4] => 12
    [5] => 29
    [6] => 31
    [7] => 72
    [8] => 59
)
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
0

Because you're dumping $array inside the first foreach loop :)

Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
0

Your sound like you may be declare your $array in for loop ,if you do so you will get three arrays.So declare outside of for loop will be get single array

$array = array(); //correct
foreach($marks as $name=>$score)
{
//$array = array(); //incorrect
 foreach($score as $subject=>$number)
 { 
   $array[]= $number;
 }
}
var_dump($array);
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
0
foreach($marks as $name=>$score)
{
    foreach($score as $subject=>$number)
    { 
        $array[]= $number;
    }
}
Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
J.K
  • 1,382
  • 1
  • 11
  • 27