0

My array is like: (Below is the dump extract)

array(3) { 
    [0]=> array(2) { 
        [0]=> string(4) "0013" 
        [1]=> float(28.936563322435) 
    } 
    [1]=> array(2) { 
        [0]=> string(4) "0003"
        [1]=> float(35.514521271921) 
    } 
    [2]=> array(2) { 
        [0]=> string(4) "0007" 
        [1]=> float(47.577230340278) 
    } 

I would like to extract its 1st value like 0013 or 0007 etc into a variable say $order such that the final result is something like this

$order= "0013,0003,0007";

I tried to do something like this:

foreach($array as $x){
    $order = x[0].",";
    }

BUT it only extracts the first element

stud91
  • 1,854
  • 6
  • 31
  • 56
  • And what is your question? How to do so? Then please research this yourself. Otherwise it would be great if you showed previous attempts so we can help you for that specific problem – kero Jul 17 '14 at 14:26
  • @kingkero I have added my code – stud91 Jul 17 '14 at 14:30

4 Answers4

3

To achieve what you desire you can use

$order = implode(',', array_column($array, 0));
t3chguy
  • 1,018
  • 7
  • 17
3

You can do it using array_map() and implode():

$order = implode(',', array_map(function($a) { return $a[0]; }, $array));
// string '0013,0003,0007' (length=14)

Or, if you're using PHP 5.5 or above:

$order = implode(',', array_column($array, 0));
billyonecan
  • 20,090
  • 8
  • 42
  • 64
0
for($i=0; $i<count($array); $i++){
    $res[$i]=$array[$i][0];
}
$order=implode(",", $res);

This will give you a string with the first values of your nested arrays, all comma-separated.

g.carvalho97
  • 332
  • 1
  • 9
0
$arr = array();
foreach($a as $k=>$v){
    $arr[] = $v[0];
}
$order = implode(',', $arr);
MH2K9
  • 11,951
  • 7
  • 32
  • 49