Solved

How to check if value is in an array

Posted on 2016-08-14
4
55 Views
Last Modified: 2016-08-21
Hi,
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?
thanks!
0
Comment
Question by:tjyoung
4 Comments
 
LVL 82

Expert Comment

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

Open in new window

2
 
LVL 4

Expert Comment

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

Accepted Solution

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

Open in new window

PHP
<pre>
<?php
if (!file_exists('t1483.xml')) {
  echo "File does not exist!";
  die();
}

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

// Dump XML so we can see what is there
print_r($xml);

// 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
print_r($vehicle);

// From http://php.net/manual/en/simplexmlelement.attributes.php#97266
function xml_attribute($object, $attribute)
{
  // Check if attribute exists and return it
  if(isset($object[$attribute]))
    return (string) $object[$attribute];
}
?>
</pre>

Open in new window

Working sample here
0
 
LVL 108

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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now