-2

I have this query on mysql, and i'm getting this array with print_r():

$data = Array ( [2] => Array ( [0] => Array ( [sale_order_value_id] => 3 [sale_order_id] => 2 [name] => Comprobante Fiscal [value] => Consumidor Final [price] => 0.0000 ) [1] => Array ( [sale_order_value_id] => 4 [sale_order_id] => 2 [name] => RNC / Cédula [value] => 00111936266 [price] => 0.0000 ) ) )

I want to extract just the [name] from $data for each input, i have tried explode() to separate the values but that does not help.

3 Answers3

0
echo implode (',',array_column($data[2], 'name'));

array_column gets each name in an array then i implode them as you asked for.

Demo:http://ideone.com/PfX7HA

0

Here is a proper way to do it :

By Using array_column() :

Example :

<?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

$first_names = array_column($records, 'first_name');
print_r($first_names);
?>

OUTPUT :

Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)

And Yes in your case it can be done like this way too as it is easy :

YOUR CODE:

<?php
$data = array( 2 => array( 0 => array('name' => "John")));
//You can simply print the name column value like this
echo $data[2][0]["name"];
?>

OUTPUT OF YOUR CODE :

John 

Live Working Link Of Your Code : https://eval.in/565707

Umair Shah
  • 2,305
  • 2
  • 25
  • 50
0

You can use the following to get your exact output...

$data =  array ( array ('sale_order_value_id' => 3,
                             'sale_order_id' => 2,
                             'name' => 'Comprobante Fiscal',
                             'value' => 'Consumidor Final',                 
                             'price' => 0.0000 ),
                array ( 'sale_order_value_id' => 4,
                             'sale_order_id' => 2,
                             'name' => 'RNC / Cédula',
                             'value' => 00111936266,
                             'price' => 0.0000 )
              ) ;

$names = array_column($data, 'name');

Aparna
  • 255
  • 1
  • 8