如何在php中将数据从一个数组存储到另一个数组

时间:2015-06-05 11:45:26

标签: php arrays associative-array

我需要什么

  • 我需要获取一些键值并存储在另一个数组中。

api数据

  Array
            (
            [0] => Array
            (
            [id] => 180462
            [des] => India International Bearing Expo New Delhi is a 3 day event being held from 5th June to the 7th June 2015 at the Delhi Haat in New Delhi, India. This
            [membership] => 0
            [name] => India International Bearing Expo New Delhi
            [abbr_name] => India International Bearing Expo
            [paid] => 0
            [event_wrapper] => 
            [event_samll_wrapper] => http://im.gifbt.com/industry/14-350x210.jpg
            [event_url] => india-international-bearing-expo-newdelhi
            [website] => http://www.bearingexpo.com/
            [eventType] => 1
            [venue_name] => Delhi Haat
            [startDate] => 2015-06-05
            [endDate] => 2015-06-07
            [city] => New Delhi
            [country] => India
            [country_url] => india
            [country_shortname] => India
            [industry_id] => 14
            [industry_name] => Industrial Products
            [industry_url] => industrial-products
            [event_status] => 
            [total_visitors] => 19
            [total_exhibitor] => 0
            [total_speakers] => 0
            )

            [1] => Array
            (
            [id] => 185988
            [des] => The International Conference On Advances in Electrical, Power Control, Electronics and Communication Engineering, organized by the Krishi Sanskriti wi
            [membership] => 
            [name] => International Conference On Advances in Electrical, Power Control, Electronics and Communication Engineering
            [abbr_name] => AEPCECE
            [paid] => 
            [event_wrapper] => 
            [event_samll_wrapper] => http://im.gifbt.com/industry/55-350x210.jpg
            [event_url] => aepcece
            [website] => http://krishisanskriti.org/aepcece.html
            [eventType] => 2
            [venue_name] => Jawaharlal Nehru University
            [startDate] => 2015-06-06
            [endDate] => 2015-06-07
            [city] => New Delhi
            [country] => India
            [country_url] => india
            [country_shortname] => India
            [industry_id] => 55
            [industry_name] => Business Services
            [industry_url] => business-consultancy
            [event_status] => 
            [total_visitors] => 11
            [total_exhibitor] => 0
            [total_speakers] => 0
            )

            [2] => Array
            (
            [id] => 193245
            [des] => The International Conference on Advances in Healthcare Management Services, organized by the Indian Institute of Management will take place from 6th J
            [membership] => 
            [name] => International Conference on Advances in Healthcare Management Services
            [abbr_name] => International Conference on Advances in Healthcare
            [paid] => 
            [event_wrapper] => 
            [event_samll_wrapper] => http://im.gifbt.com/industry/55-350x210.jpg
            [event_url] => international-conference-on-advances-in-healthcare
            [website] => http://www.iimahd.ernet.in/cmhsconf2015/
            [eventType] => 2
            [venue_name] => Indian Institute of Management
            [startDate] => 2015-06-06
            [endDate] => 2015-06-07
            [city] => Ahmedabad
            [country] => India
            [country_url] => india
            [country_shortname] => India
            [industry_id] => 55
            [industry_name] => Business Services
            [industry_url] => business-consultancy
            [event_status] => 
            [total_visitors] => 7
            [total_exhibitor] => 0
            [total_speakers] => 0
            )

            )

php code

          $noty = array();
          $notification=json_decode($notifications,true);
            //print_r($notifications);
             foreach($notification as $key =>$value)
             {

                $id['id']=$value['id'];
                $name['name']=$value['name'];
                 $abbr_name['abbr_name']=$value['abbr_name'];
                 $startDate['startDate']=$value['startDate'];
                 $endDate['endDate']=$value['endDate'];
                  $city['city']=$value['city'];
                   $country['country']=$value['country'];
                    $data[$key]=array_push($noty,$id,$name,$abbr_name, $startDate,$endDate,$city,$country);

             }

       $n=json_encode($noty,true);
  • 我需要将一些数据分组。

输出应该是:

                    Array
            (
            [0] => Array
            (
            [id] => 180462

            [venue_name] => Delhi Haat
            [startDate] => 2015-06-05
            [endDate] => 2015-06-07
            [city] => New Delhi
            [country] => India
            [country_url] => india
            [country_shortname] => India


            )

            [1] => Array
            (
            [id] => 185988

            [membership] => 
            [name] => International Conference On Advances in Electrical, Power Control, Electronics and Communication Engineering
            [abbr_name] => AEPCECE
            [paid] => 
            [event_wrapper] => 
            [event_samll_wrapper] => http://im.gifbt.com/industry/55-350x210.jpg
            [event_url] => aepcece

            [venue_name] => Jawaharlal Nehru University
            [startDate] => 2015-06-06
            [endDate] => 2015-06-07
            [city] => New Delhi
            [country] => India

            )

            [2] => Array
            (
            [id] => 193245

            [name] => International Conference on Advances in Healthcare Management Services
            [abbr_name] => International Conference on Advances in Healthcare

            [event_samll_wrapper] => http://im.gifbt.com/industry/55-350x210.jpg
            [event_url] => international-conference-on-advances-in-healthcare
            [website] => http://www.iimahd.ernet.in/cmhsconf2015/
            [eventType] => 2
            [venue_name] => Indian Institute of Management
            [startDate] => 2015-06-06
            [endDate] => 2015-06-07
            [city] => Ahmedabad
            [country] => India

            )

            )

问题是它的o / p数组

 Array
(
[0] => Array
    (
        [id] => 180462
    )

[1] => Array
    (
        [name] => India International Bearing Expo New Delhi
    )

[2] => Array
    (
        [abbr_name] => India International Bearing Expo
    )

[3] => Array
    (
        [startDate] => 2015-06-05
    )

[4] => Array
    (
        [endDate] => 2015-06-07
    )

[5] => Array
    (
        [city] => New Delhi
    )
  • 请说明我做错了。
  • 我需要一些键和值并存储在另一个数组中。

1 个答案:

答案 0 :(得分:1)

您的错误是您以错误的方式使用array_push:您给array_push的每个参数都会向该数组添加另一行。 你想要做的是当时只添加一行,包含所需的数据。

你应该这样做:

<?php

$noty = array();
$notification=json_decode($notifications,true);
//print_r($notifications);
foreach($notification as $key =>$value)
{
    array_push($noty, array(
        'id' => $value['id'],
        'name' => $value['name'],
        'abbr_name' => $value['abbr_name'],
        'startDate' => $value['startDate'],
        'endDate' => $value['endDate'],
        'city' => $value['city'],
        'id' => $value['id'],
        'country' => $value['country'],
        ));
}
$n=json_encode($noty,true);