Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've written the follow code to try and insert data into a table on the database. However it's just inserting letters. I'm not sure what I'm doing wrong.

$media_items = array( 
array (
   "media_name" => "Facebook",
   "link_url" => "http://www.facebook.com/insightdezign",
   "icon" => "facebook.png",
   "size" => "48",
   "order" => "0"
),
array (
   "media_name" => "Twitter",
   "link_url" => "http://www.twitter.com/insightdezign",
   "icon" => "twitter.png",
   "size" => "48",
   "order" => "1"
)
);

foreach ($media_items as $media_item) {
    if (is_array($media_item)){
        foreach ($media_item as $item) {
            $rows_affected = $wpdb->insert( $ffui_items, array( 'media_name' => $item['media_name'], 'link_url' => $item['link_url'], 'icon' => $item['icon'], 'size' => $item['size'], 'order' => $item['order'] ) );
        }
    }
}
share|improve this question
 
remove this foreach ($media_item as $item) –  u_mulder Oct 20 '13 at 16:20
add comment

1 Answer

up vote 2 down vote accepted

Inside your nested foreach loop, you will be looping over strings, not arrays. As a result of type juggling, the indexes will be evaluated to 0. Since PHP also accepts $foo['bar'] syntax on strings, it will just return the first letter.

You can simply remove the nested foreach loop and do it as follows:

foreach ($media_items as $media_item) 
{
    if (is_array($media_item))
    {
        $rows_affected = $wpdb->insert( $ffui_items, 
            array( 
            'media_name' => $media_item['media_name'], 
            'link_url' => $media_item['link_url'], 
            'icon' => $media_item['icon'], 
            'size' => $media_item['size'], 
            'order' => $media_item['order'] 
            ) ;
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.