如何将多维数组的键值添加到另一个数组

时间:2014-12-01 13:01:52

标签: php arrays json multidimensional-array

我的问题是我需要从数组中只获取一个字段,然后存储在另一个数组中。

代码:

$obj = json_decode($event,true);

[data] =>数组(

[0] => Array
(
    [metadata] => Array
        (
            [name] => Suchi
            [publicProfileUrl] => http://www.linkedin.com/in/cc
            [summary] => Building and scaling businesses focused on simple yet powerful consumer products has been the dream, and this has come to life both at Skype, Gumtree and now most recently at LimeRoad.com. We are the the start of the journey to build the most extensive and engaging lifestyle platform ever. Our sole objective is to make the discovery experience on LimeRoad so delightful, that we become the online destination to find gorgeous yet affordable products.Earlier, with 13m users at Gumtree, we built UK's largest horizontal classifieds business. In c. two years, we went from a No. 3 position to market leading positions in jobs and consumer-to-consumer car sales, growing visits by 35% y-o-y in the UK (marketing spend remaining unchanged). Gumtree required transformational leadership, and I spent the better part of my time there scaling team capabilities, product, sales, marketing and CS functions, whilst re-architecting the financials towards core revenue streams.Prior to this, Skype was a powerful leadership development experience - being part of the executive management team of a high growth technology company, transitioning from founder–led-startup with 3 CEO changes in 2 years, whilst also building the most dramatic upgrade of the product. What a roller coaster, but not one to trade out of!Specialties: Scaling businesses, by setting direction and managing execution around product development, marketing, sales and operations. Building and empowering high performing teams.
        )

)

[1] => Array
(
    [metadata] => Array
        (
            [name] => Suchi
            [publicProfileUrl] => http://www.linkedin.com/in/cc
            [summary] => Building and scaling businesses focused on simple yet powerful consumer products has been the dream, and this has come to life both at Skype, Gumtree and now     most recently at LimeRoad.com.  We are the the start of the journey to build the most extensive and engaging lifestyle platform ever. Our sole objective is to make the     discovery experience on LimeRoad so delightful, that we become the online destination to find gorgeous yet affordable products.Earlier, with 13m users at Gumtree, we built     UK's largest horizontal classifieds business.  In c. two years, we went from a No. 3 position to market leading positions in jobs and consumer-to-consumer car sales,     growing visits by 35% y-o-y in the UK (marketing spend remaining unchanged).  Gumtree required transformational leadership, and I spent the better part of my time there     scaling team capabilities, product, sales, marketing and CS functions, whilst re-architecting the financials towards core revenue streams.Prior to this, Skype was a     powerful leadership development experience - being part of the executive management  team of a high growth technology company, transitioning from founder–led-startup     with 3 CEO changes in 2 years, whilst also building the most dramatic upgrade of the product.   What a roller coaster, but not one to trade out of!Specialties: Scaling     businesses, by setting direction and managing execution around product development, marketing, sales and operations.  Building and empowering high performing teams.
        )

)

我尝试从数组中获取摘要并将其存储在另一个数组中:

$count=0;
$newArray = array();
foreach($obj as $row)
{ 
    $newArray[] = array('summary' => $row);
    $new=$newArray[$count]['summary'][$count]['metadata']['summary'];
    $count++;
} 

这是我最终想要达到的目标:

    data=> Array
    (
        [0] => Array
            (
                [metadata] => Array
                    (

                        [summary] => Building and scaling businesses focused on simple yet powerful consumer products has been the dream, and this has come to life both at Skype, Gumtree and now most recently at LimeRoad.com.  We are the the start of the journey to build the most extensive and engaging lifestyle platform ever. Our sole objective is to make the discovery experience on LimeRoad so delightful, that we become the online destination to find gorgeous yet affordable products.Earlier, with 13m users at Gumtree, we built UK's largest horizontal classifieds business.  In c. two years, we went from a No. 3 position to market leading positions in jobs and consumer-to-consumer car sales, growing visits by 35% y-o-y in the UK (marketing spend remaining unchanged).  Gumtree required transformational leadership, and I spent the better part of my time there scaling team capabilities, product, sales, marketing and CS functions, whilst re-architecting the financials towards core revenue streams.Prior to this, Skype was a powerful leadership development experience - being part of the executive management  team of a high growth technology company, transitioning from founder–led-startup with 3 CEO changes in 2 years, whilst also building the most dramatic upgrade of the product.   What a roller coaster, but not one to trade out of!Specialties: Scaling businesses, by setting direction and managing execution around product development, marketing, sales and operations.  Building and empowering high performing teams.
                    )

            )
        [1] => Array
            (
                [metadata] => Array
                    (

                        [summary] => Building and scaling businesses focused on simple yet powerful consumer products has been the dream, and this has come to life both at Skype, Gumtree and now most recently at LimeRoad.com.  We are the the start of the journey to build the most extensive and engaging lifestyle platform ever. Our sole objective is to make the discovery experience on LimeRoad so delightful, that we become the online destination to find gorgeous yet affordable products.Earlier, with 13m users at Gumtree, we built UK's largest horizontal classifieds business.  In c. two years, we went from a No. 3 position to market leading positions in jobs and consumer-to-consumer car sales, growing visits by 35% y-o-y in the UK (marketing spend remaining unchanged).  Gumtree required transformational leadership, and I spent the better part of my time there scaling team capabilities, product, sales, marketing and CS functions, whilst re-architecting the financials towards core revenue streams.Prior to this, Skype was a powerful leadership development experience - being part of the executive management  team of a high growth technology company, transitioning from founder–led-startup with 3 CEO changes in 2 years, whilst also building the most dramatic upgrade of the product.   What a roller coaster, but not one to trade out of!Specialties: Scaling businesses, by setting direction and managing execution around product development, marketing, sales and operations.  Building and empowering high performing teams.
                    )

            )
    )

我面临的问题是我无法从我的代码中获取数组的整个摘要我只获取了第一个数组索引。

我试图通过增加计数来获取数据,尽管它不起作用。

1 个答案:

答案 0 :(得分:2)

只需定位您想要的相应数据,然后由于它们具有相同的结构,它在另一个上将是相同的:

$new['data'] = array();
foreach($obj['data'] as $values) {
    $new['data'][]['metadata']['summary'] = $values['metadata']['summary'];
    // ^ new assignment
}

echo '<pre>';
print_r($new);

或者,如果您不想要新副本,您还可以在foreach中的每个副本上添加一个引用,并在那些您不想要的索引上创建unset()

foreach($obj['data'] as &$values) {
    unset($values['metadata']['name'], $values['metadata']['publicProfileUrl']);
}