Php 对于每个循环

Php 对于每个循环,php,facebook,foreach,Php,Facebook,Foreach,我试图为我的论文制作一个web应用程序。 实际上,它几乎完成了,但我仍然需要找到一种方法将用户喜欢的内容存储在数据库中。我收集用户信息,尝试看看他或她喜欢的电影和保存在不同SNS中的用户数据元素之间是否存在关系。对于单个信息,它已经按应有的方式保存了内容。问题是,facebook使用数组和对象,我无法找到一种方法将所有喜欢的内容保存在一个字符串中(然后保存到数据库中) 正如你所看到的,对于语言、爱好、喜爱的运动员,我现在用一种丑陋的方式对其进行编码,以读取个人资料并存储它。问题是有些人有超过10

我试图为我的论文制作一个web应用程序。 实际上,它几乎完成了,但我仍然需要找到一种方法将用户喜欢的内容存储在数据库中。我收集用户信息,尝试看看他或她喜欢的电影和保存在不同SNS中的用户数据元素之间是否存在关系。对于单个信息,它已经按应有的方式保存了内容。问题是,facebook使用数组和对象,我无法找到一种方法将所有喜欢的内容保存在一个字符串中(然后保存到数据库中)

正如你所看到的,对于语言、爱好、喜爱的运动员,我现在用一种丑陋的方式对其进行编码,以读取个人资料并存储它。问题是有些人有超过100个喜欢的人,用这种方式编码当然不方便。当我使用
对于每个it always bug…

如果您只想为单个表连接数组,请使用PHPs内爆函数

与此处提到的数组映射相结合

或者,按照评论中提到的方法对数据库进行正常化

    'favorite_athletes'     => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['favorite_athletes']));
    'likes'               => implode(",", array_map(function($x){ return $x['name']}, $fbUserProfile['likes']['data']));

如果您只想连接数组,可以使用PHPs
内爆
函数。您不应该在单个数据库字段中将所有具有名称的like保存为长字符串。与其规范化数据库,不如为相似的信息使用一个不同的表,并使用一个单独的表链接这两个信息。这同样适用于所有其他连接的值。它总是错误的。这到底是什么意思
    'favorite_athletes'     => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['favorite_athletes']));
    'likes'               => implode(",", array_map(function($x){ return $x['name']}, $fbUserProfile['likes']['data']));
// Insert or update user data to the database
$fbUserData = array(
    'oauth_provider'        => 'facebook',

    //basic profile
    'oauth_uid'             => $fbUserProfile['id'],
    'first_name'            => $fbUserProfile['first_name'],
    'last_name'             => $fbUserProfile['last_name'],
    'picture'               => $fbUserProfile['picture']['url'],
    'email'                 => $fbUserProfile['email'],
    'link'                  => $fbUserProfile['link'],
    'age_range'             => $fbUserProfile['age_range']['min'],
    'currency'              => $fbUserProfile['currency']['user_currency'],
    'devices'               => $fbUserProfile['devices'][0]['hardware'],
    'gender'                => $fbUserProfile['gender'],
    'install_type'          => $fbUserProfile['install_type'],
    'installed'             => $fbUserProfile['installed'],
    'is_shared_login'       => $fbUserProfile['is_shared_login'],
    'is_verified'           => $fbUserProfile['is_verified'],
    'locale'                => $fbUserProfile['locale'],
    'name_format'           => $fbUserProfile['name_format'],
    'payment_pricepoints'   => $fbUserProfile['payment_pricepoints'], //credits?
    'security_settings'     => $fbUserProfile['security_settings']['secure_browsing']['enabled'],
    'test_group'            => $fbUserProfile['test_group'],
    'timezone'              => $fbUserProfile['timezone'],
    'updated_time'          => $fbUserProfile['updated_time'],
    'verified'              => $fbUserProfile['verified'],
    'video_upload_limits'   => $fbUserProfile['video_upload_limits']['size'],
    'viewer_can_send_gift'  => $fbUserProfile['viewer_can_send_gift'],

    //extended permissions
    'relationship_status'   => $fbUserProfile['relationship_status'],
    'hometown'              => $fbUserProfile['hometown']['name'],
    'languages'             => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['languages'])),//$fbUserProfile['languages'][0]['name'].",".$fbUserProfile['languages'][1]['name'].",".$fbUserProfile['languages'][2]['name'].",".$fbUserProfile['languages'][3]['name'].",".$fbUserProfile['languages'][4]['name'],
    'likes'               => implode(",", array_map(function($x){ return $x['name'];}, $fbUserProfile['likes']['data'])),
    'favorite_athletes'     => implode(",", array_map(function($x) { return $x['name'];},$fbUserProfile['favorite_athletes']))