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?

Edited by CyberShot
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.