1

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'] ) );
        }
    }
}
1
  • remove this foreach ($media_item as $item) Commented Oct 20, 2013 at 16:20

1 Answer 1

2

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'] 
            ) ;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.