Link to home
Start Free TrialLog in
Avatar of SashaIsaac
SashaIsaac

asked on

Replace array value with url

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

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SashaIsaac
SashaIsaac

ASKER

Thanks for the info - makes sense and really helped!
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) )
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...
I will try that one as well! Thanks for the help everyone. Arrays are definitely an area I am weak in,
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.
>>I don't think I've ever thought about doing that for a simple multi-dimensional array like his
:)
@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