1

I have array

array (size=2)
  0 => 
    array (size=1)
      'Product' => 
        array (size=2)
          'id' => string '109' (length=3)
          'name' => string 'product1' (length=2)
  1 => 
    array (size=1)
      'Product' => 
        array (size=2)
          'id' => string '110' (length=3)
          'name' => string 'product2' (length=2)

Is it possible to get name for id? Example, I have id 109 and want get name product1.

0

2 Answers 2

3

Try this to find the product name:

function getProductNameById($products, $productId) {
    foreach($products as $p) {
        if($p['Product']['id'] == $productId) {
            return $p['Product']['name'];
        }
    }
}

Where $products is your array with your products in it and $productId the id of the product for which you want to find its name.

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

Comments

1

You can use something like this

function getName($id, $array)
{
  foreach ($array as $product)
  {
    if ($product['Product']['id'] == $id)
      return $product['Product']['name'];
  }
}

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.