3

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!

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice thanks a ton, just curious though lets say you wanted to get the keys AND the values.. like "name" "Red Shirt", "name" "Black Shirt", "name" "Blue Shirt", of all arrays how can that be done?
@LucasSantos how can an array have same keys?? the word key means its unique... it can't be duplicate.... if same named key is there in array than it will override the last value...
1

Simple,Use like this

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

Comments

1

Try this

foreach($products as $key => $val) {
    // For name
    echo $val['name'];

    // For specific product price
    if($key == 103) {
        echo $val['price'];
    }
}

Let me know, if there is any issue.

Comments

1

Try the following code (This will cover all scenarios):

function getMyArray($key, $arr = array()){
    $result = array();
    foreach($arr as $arrVal){
        $result[] = $arrVal[$key];
    }
    return implode(", ", $result);
}

echo getMyArray('name', $products);  // Red Shirt, Black Shirt, Blue Shirt
echo getMyArray('img', $products);   //path1, path2, path3
echo getMyArray('price', $products); // 18, 20, 20

If you want some particular value:

echo $products[103]['price']; // 20

1 Comment

Nice way of using a function to implode the results as an array
0

Try this one

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

Comments

0
$productNames = array();
foreach($products as $key=>$product) {
    $productNames[] = $product['name'];
}

//all names with comma

echo implode(',',$productNames);
echo "\n";

//only price of $products[103]

echo $products[103]['price'];

Result:

Red Shirt, Black Shirt, Blue Shirt
20

Comments

0

you will get exact the same out put as you wished by this code

foreach($products as $key=>$result)
{

    echo $result['name'].', ';
}

Out put :

Red Shirt, Black Shirt, Blue Shirt

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.