Jump to content

help getting values from array


CyberShot

Recommended Posts

I am only able to get the value from the first index and I don't know what I am doing. I have an array that returns this when I dump the varaiable

array(1) {
  [0]=>
  array(5) {
    ["title"]=>
    string(12) "Social links"
    ["facebook"]=>
    string(23) "http://www.facebook.com"
    ["twitter"]=>
    string(22) "http://www.twitter.com"
    ["google"]=>
    string(0) ""
    ["dribble"]=>
    string(0) ""
  }
}

I tried to echo the values like so.

	if( !empty($social[0]['facebook')){
      echo '
      <li><a href="' . $social[0]['facebook'] . '"><img src="' . get_template_directory_uri() . '/images/icons/icon2.gif"><img src="'. get_template_directory_uri() . '/images/icons/icon2_active.gif" class="img_act"></a></li>';
	}
	if( !empty($social[1]['twitter'])){
      echo '
      <li><a href="' . $social[1]['twitter'] . '"><img src="' . get_template_directory_uri() . '/images/icons/icon1.gif"><img src="'. get_template_directory_uri() . '/images/icons/icon1_active.gif" class="img_act"></a></li>';
	}
	if( !empty($social[2]['google'])){
      echo '
      <li><a href="' . $social[2]['google'] . '"><img src="' . get_template_directory_uri() . '/images/icons/icon3.gif"><img src="'. get_template_directory_uri() . '/images/icons/icon3_active.gif" class="img_act"></a></li>';
	}
	if( !empty($social[3]['dribble'])){
      echo '
      <li><a href="' . $social[3]['dribble'] . '"><img src="' . get_template_directory_uri() . '/images/icons/icon4.gif"><img src="'. get_template_directory_uri() . '/images/icons/icon4_active.gif" class="img_act"></a></li>';
	}

It only returns the first value in the array.

 

 

EDIT.... I just figured it out. instead, Is there a more eloquent way of doing this?

Link to comment
Share on other sites

We'd need to see your code. Given the variable you dumped everything would have been:

 

 

echo $social[0]['title'];
echo $social[0]['facebook'];
//etc
If the annoyance is that you have a useless array element, you could do this:

 

$social = $social[0];
Then you'd just reference the associative elements by key.

 

echo $social['twitter'];
With that said, foreach() loops are helpful:

 

foreach ($social[0] as $key => $value) {

    echo "$key = $value </br>";
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.