Replace array value with url

SashaIsaac
SashaIsaac used Ask the Experts™
on
I am trying to assign a url to array values once they are printed to the page, but it isn't working with the code that I have. I am attaching the code snippet that I am using to assign the array value and then also the dump of the array. I am just getting "Array" as the print out.

When I do "print_r($field_academic_unit_value); " it does print out the correct value.

Thanks for any help!
$field_academic_unit_value = array();
  if (in_array('New College of Interdisciplinary Arts and Sciences',$field_academic_unit_value))
  {
   $field_academic_unit_value = "http://example.com";
  }
 
//// this is what that array looks like when I do print_r
 
[field_academic_unit] => Array
        (
            [0] => Array
                (
                    [value] => New College of Interdisciplinary Arts and Sciences
                )
 
        )

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
That's because your $field_academic_unit is a multi-dimensional array, and in_array() only searches the first level of an array.

Imagine a street lined with houses, and inside each house is a person. What you're currently trying to do is use in_array() to find the house that has a person named "Michael" by simply looking at the outside of the houses, which is impossible. If you want to find out the name of the person inside, you need to go into each house and then ask the same question, "Are you Michael?"

So what you could do is this:
foreach($field_academic_unit as $insideArray)
{
   if($insideArray["value"] == "New College of Inter....")
   {
      $field_academic_unit_value = "http://www.example.com";
   }
}

That said, there ARE tricks out there to speed up the process. Sometimes when I'm dealing with multidimensional arrays, I like to create another array called a lookup table. Basically, it might look like this:

$lookup["New College of Inter...etc..."] = 0;
$lookup["Some other College"] = 1;
...etc...

The key being your name and the value being the numeric index inside $field_academic_unit.

Hope that makes sense!

Author

Commented:
Thanks for the info - makes sense and really helped!
Expert of the Year 2008
Top Expert 2008

Commented:
change line 2:
 if (in_array('New College of Interdisciplinary Arts and Sciences',$field_academic_unit_value))

to:
if( in_array( array("value"=>"New College of Interdisciplinary Arts and Sciences") , $field_academic_unit) )
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Commented:
You know, for all the times I've ever used in_array(), I don't think I've ever thought about doing that for a simple multi-dimensional array like his. That's actually much better than my suggestion, hielo.

You learn something new everyday...

Author

Commented:
I will try that one as well! Thanks for the help everyone. Arrays are definitely an area I am weak in,

Commented:
Well, at least you picked the right language to learn arrays. PHP has fantastic array-handling  capabilities and makes it pretty easy to work with them. Once you get a handle on arrays, you'll never figure out how you lived without them.
Expert of the Year 2008
Top Expert 2008

Commented:
>>I don't think I've ever thought about doing that for a simple multi-dimensional array like his
:)
Most Valuable Expert 2011
Top Expert 2016

Commented:
@gr8gonzo: Excellent explanation.
@SashaIsaac: You got some sick code there!

Consider this:

$field_academic_unit_value = array();
  if (in_array('New College of Interdisciplinary Arts and Sciences',$field_academic_unit_value))
  {
   $field_academic_unit_value = "http://example.com";
  }

First you set that $field_academic_unit_value to be an empty array, then you set it to be a character string.  Those sort of practices are sure to lead to confusion.

Learn about var_dump() to print out data structures.  If you echo "<pre>"; before var_dump() the output is much easier to read.

If you want to describe what you're trying to accomplish in plain language, we maybe able to help you with the code.  I am just guessing, but I think you are trying to associate a list of college names with the URLs of the colleges?  Maybe this example will be helpful (untested but valid in principle)
<?php
// THE TEST DATA
$schools = array
           (
              "Arts and Sciences" => "www.art.org",
              "Business"          => "www.biz.org",
              "Education"         => "www.edu.org",
           );
           
// ITERATE OVER THE ARRAY TO SHOW THE DATA
foreach ($schools as $college => $url)
{
    echo "<br/>THE COLLEGE OF $college HAS A WEB SITE AT $url \n";
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial