1

I am using this code to output multiple specific values from array and sending these values to email.

If i write this:

print_r( $products[1]['Notes'], true )

Then it is displaying 1 value off course I have put "[1]" to just targeting 1 row.

and If i write this:

print_r( $products, true )

Then it is outputting all the values and all the rows.

Is there anyway i can output just multiple values of "Notes"?

3 Answers 3

1

If your php is 5.5 and more - use array_column function:

print_r(array_column($products, 'Notes'), true);

Otherwise, you need to select required columns with a foreach and print'em:

$columns = [];
foreach ($products as $prod) {
    $columns[] = $prod['Notes'];
}
print_r($columns, true);
Sign up to request clarification or add additional context in comments.

2 Comments

it is displaying the values but with this: "Array ( [0] => product 1 [1] => product 2 )". Is there any way i can just display "product 1, product 2"?
I achieved that through implode
1

There's an array_column() function that could help you here.

<?php
$data = [
    ['Notes' => 'test1'],
    ['Notes' => 'test2'],
    ['Notes' => 'test3'],
    ['Notes' => 'test4'],
    ['Notes' => 'test5'],
    ];

$notes = array_column($data, 'Notes');
print_r($notes);

Output:

Array
(
    [0] => test1
    [1] => test2
    [2] => test3
    [3] => test4
    [4] => test5
)

https://3v4l.org/Mp24S

Comments

1
$notes = array();
$i = 0;
while($i<count($products)){
  $notes[] = $products[$i]['Notes'];
  $i++;
}
print_r($notes);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.