How to check if value is in an array

Posted on 2016-08-14
Last Modified: 2016-08-21
I have an array I create from results of a soap call.
I'm having trouble trying to figure out how to catch it when a certain value doesn't exist: bestTrimName

I tried below but I get 'Undefined index: bestTrimName' when there isn't a value:
$vehicle = array(
'year'		=> $array['@attributes']['modelYear'],
'make'		=> $array['@attributes']['bestMakeName'],
'model'		=> $array['@attributes']['bestModelName'],
'trim'		=> ($array['@attributes']['bestTrimName'] ? $array['@attributes']['bestTrimName'] : '')

Open in new window

Not sure best way to handle this scenario?
Question by:tjyoung
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 82

Expert Comment

ID: 41755886
'trim'		=> ( array_key_exists('bestTrimName', $array['@attributes'] ) ? $array['@attributes']['bestTrimName'] : '')

Open in new window


Expert Comment

by:Elizabeth Anderson
ID: 41755965
Use in_array to check if value is in an array. Here's example below -
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
if (in_array("mac", $os)) {
    echo "Got mac";
LVL 58

Accepted Solution

Julian Hansen earned 500 total points
ID: 41755979
You can use isset
<?xml version="1.0" encoding="UTF-8" ?> 
<vehicle modelYear="1996" bestMakeName="vw" bestModelName="Polo"></vehicle>

Open in new window

if (!file_exists('t1483.xml')) {
  echo "File does not exist!";

$xml = simplexml_load_file('t1483.xml');

// Dump XML so we can see what is there

// Create our vehicle array
$vehicle = array(
  'year'    => xml_attribute($xml, 'modelYear'),
  'make'    => xml_attribute($xml, 'bestMakeName'),
  'model'   => xml_attribute($xml, 'bestModelName'),
  'trim'    => xml_attribute($xml, 'bestTrimName')

// Check the contents

// From
function xml_attribute($object, $attribute)
  // Check if attribute exists and return it
    return (string) $object[$attribute];

Open in new window

Working sample here
LVL 110

Expert Comment

by:Ray Paseur
ID: 41756198
Best way to get a tested and working example would be to use var_export($array) and post that output in the code snippet here at E-E. Then we could copy the var_export() output exactly and show you the code that gives the exact response you need.  No guessing, no accidental omissions, Easy!

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question