How can I output specific data from an associative array in PHP with foreach

Lucas Santos

My associative array:

$products = array();
$products[101] = array(
    "name" => "Red Shirt",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18
);
$products[102] = array(
    "name" => "Black Shirt",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20
);
$products[103] = array(
    "name" => "Blue Shirt",
    "img" => "img/shirts/shirt-103.jpg",    
    "price" => 20
);

So lets say I wanted to output the name of ALL products array like so:

Red Shirt, Black Shirt, Blue Shirt

how can I achieve that with a foreach loop? I have tried to output only a specific key from all arrays at once but I cannot seem to do it without outputting all the keys.

Also lets say I wanted to just output the "price" of a certain array like $products[103] how can I achieve that?

Thank you!

Nishant Solanki

you can use below code, with using foreach

foreach($products as $pro)
{
    echo $pro['name'];
}

above code will print only name key of the product array

to get price of $product['103'] you can use like below code

foreach ($products as $key => $value)
{
    if ($key == '103')
    {
        echo $pro['price'];
    }
}

EDIT : to get array of names

$names = array();
foreach ($products as $pro)
{
    $names[] = $pro['name'];
}
print_r($names);

it will return

Array ( [0] => Red Shirt [1] => Black Shirt [2] => Blue Shirt )

let me know if this helped you

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I output specific data from an associative array in PHP with foreach

From Dev

How can I create an associative array from a foreach loop?

From Dev

How to create php associative array from messy data

From Dev

How can I get the prev/next key from an associative array?

From Dev

How can I paste data from array to specific range (row)

From Dev

How to update values in associative array using php foreach loop when key includes a specific string?

From Dev

PHP Associative array from encoded data

From Dev

MySQL Returns PHP Associative Array - but I Can't Access Array Data

From Dev

Inserting data into an associative array in foreach

From Dev

Inserting data into an associative array in foreach

From Dev

How can I extract data in an ordered way from a php array?

From Dev

How can I print an associative array as a matrix

From Dev

How access the data from an associative array Codeigniter

From Dev

how to loop through this php associative array using foreach loop

From Dev

How is this associative array foreach working?

From Dev

How to get all data from specific keys in PHP array, if array can have any size and nesting?

From Dev

Why I can not unset array in associative array php

From Dev

how can i get array in proper format from this foreach and nested if condition in php?

From Dev

How can I build an associative array recursively using keys from another array?

From Dev

How can I omit a specific comma from my output?

From Dev

Can I use an array in PHP as associative and also numbed ?

From Dev

PHP Why can't I access this associative array value?

From Dev

how can i extract specific data from facebook graph api array?

From Dev

How can I achieve a flattened array of promises from a forEach loop?

From Dev

how can I extract specific data from different arrays using php?

From Dev

Change $key of associative array in a foreach loop in php

From Dev

PHP Foreach Associative Array Loop for Sendy API

From Dev

associative array iterating with foreach loop, php

From Dev

Create dynamic associative array in php in foreach loop

Related Related

  1. 1

    How can I output specific data from an associative array in PHP with foreach

  2. 2

    How can I create an associative array from a foreach loop?

  3. 3

    How to create php associative array from messy data

  4. 4

    How can I get the prev/next key from an associative array?

  5. 5

    How can I paste data from array to specific range (row)

  6. 6

    How to update values in associative array using php foreach loop when key includes a specific string?

  7. 7

    PHP Associative array from encoded data

  8. 8

    MySQL Returns PHP Associative Array - but I Can't Access Array Data

  9. 9

    Inserting data into an associative array in foreach

  10. 10

    Inserting data into an associative array in foreach

  11. 11

    How can I extract data in an ordered way from a php array?

  12. 12

    How can I print an associative array as a matrix

  13. 13

    How access the data from an associative array Codeigniter

  14. 14

    how to loop through this php associative array using foreach loop

  15. 15

    How is this associative array foreach working?

  16. 16

    How to get all data from specific keys in PHP array, if array can have any size and nesting?

  17. 17

    Why I can not unset array in associative array php

  18. 18

    how can i get array in proper format from this foreach and nested if condition in php?

  19. 19

    How can I build an associative array recursively using keys from another array?

  20. 20

    How can I omit a specific comma from my output?

  21. 21

    Can I use an array in PHP as associative and also numbed ?

  22. 22

    PHP Why can't I access this associative array value?

  23. 23

    how can i extract specific data from facebook graph api array?

  24. 24

    How can I achieve a flattened array of promises from a forEach loop?

  25. 25

    how can I extract specific data from different arrays using php?

  26. 26

    Change $key of associative array in a foreach loop in php

  27. 27

    PHP Foreach Associative Array Loop for Sendy API

  28. 28

    associative array iterating with foreach loop, php

  29. 29

    Create dynamic associative array in php in foreach loop

HotTag

Archive