在两个普通数组中转换关联数组值

时间:2016-05-26 07:51:47

标签: php arrays

我有这样的数组

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Access Control
            [editable] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] =>  CCTV
            [editable] => 0
        )

    [2] => Array
        (
            [id] => 3
            [name] => Fire Alarm
            [editable] => 0
        )

    [3] => Array
        (
            [id] => 4
            [name] => Intrusion Alarm System
            [editable] => 0
        )

    [4] => Array
        (
            [id] => 5
            [name] => Home Automation
            [editable] => 0
        )

    [5] => Array
        (
            [id] => 6
            [name] => Security Equipments
            [editable] => 0
        )

    [6] => Array
        (
            [id] => 7
            [name] => Audio Video
            [editable] => 0
        )

)

从此我想创建两个新数组 一个用于商店id,另一个用于商店name。 我尝试了array_values方法,但它将返回相同的数组

2 个答案:

答案 0 :(得分:3)

简单地tf_vectorizer = CountVectorizer(max_df=0.95, min_df=2, tokenizer=tokenize_and_stem, stop_words='english') %time tf = tf_vectorizer.fit_transform(descriptions) print(tf.shape) svd = TruncatedSVD(100) normalizer = Normalizer(copy=False) lsa = make_pipeline(svd, normalizer) tfidf_desc = lsa.fit_transform(tfidf_matrix_desc) explained_variance = svd.explained_variance_ratio_.sum() print("Explained variance of the SVD step: {}%".format(int(explained_variance * 100))) similarity_dist = cosine_similarity(tfidf_desc) pd.DataFrame(similarity_dist,index=descriptions.index, columns=descriptions.index).head(10) print(tfidf_matrix_desc.min(),tfidf_matrix_desc.max()) #0.0 0.736443429828 print(tfidf_desc.min(),tfidf_desc.max()) #-0.518015429416 0.988306783341 print(similarity_dist.max(),similarity_dist.min()) #1.0 -0.272010919022 可以做你想做的事。让您的数组名称为array_column

$arr

答案 1 :(得分:1)

函数array_column不适用于PHP< 5.5。如果您的PHP版本是> = 5.3,则可以使用此方法:

$ids = array_map(function($element) {
  return $element['id'];
}, $arr);

$names = array_map(function($element) {
  return $element['name'];
}, $arr);