在创建PHP关联数组 [英] Creating Associative Array in PHP

查看:168
本文介绍了在创建PHP关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组。

  $ =铺阵列(
              阵列(appn1,PUB1,PUB2,pub3),
              阵列(appn2,PUB1),
              阵列(appn3,PUB1,PUB2)
            );

每个数组中的第一项为申请号并在每个数组中其余的都是在出版号。我得到的第一个项目(申请号),每个阵列(最新出版号)的最后一个项目像这样

  $指数=计数(array_keys($店));
    为($ I = 0; $ I< $指数; $ I ++){        $ appln_nr = $店[$ i] [0];
        回声$ appln_nr;        $ publn_nr_index =计数(array_keys($店[$ i])) - 1;
        $ publn_nr = $店[$ i] [$ publn_nr_index];
        回声$ publn_nr;
   }

现在,我为每个阵列内申请号和出版号。

我要创建从申请号和出版物号码关联数组。

其中关键的应该是申请号和它的价值是出版号。

感谢

修改

我从$店铺阵列得到什么

 阵列
 (
  [0] =>排列
    (
        [0] => appn1
        [1] => PUB1
        [2] => PUB2
        [3] => pub3
    )  [1] =>排列
    (
        [0] => appn2
        [1] => PUB1
    )  [2] =>排列
    (
        [0] => appn3
        [1] => PUB1
        [2] => PUB2
    )

这就是我需要在我的关联数组

 阵列(
    appn1=> pub3
    appn2=> PUB1
    appn3=> PUB2


解决方案

我终于明白你想要什么,你的编辑XD后:

  $ =铺阵列(
    阵列(appn1,PUB1,PUB2,pub3),
    阵列(appn2,PUB1),
    阵列(appn3,PUB1,PUB2)
);
$ shopNew =阵列();的foreach($店作为$值){
    $ shopNew [$值[0] =结束($值);
}//现在如果你愿意,你可以替换$店和取消$ shopNew
$店铺= $ shopNew;
未设置($ shopNew);的print_r($店);

输出是这样的:

 阵列(
  [appn1] => pub3
  [appn2] => PUB1
  [appn3] => PUB2

I have a multidimensional array.

$shop = array( 
              array("appn1", "pub1" ,"pub2" , "pub3"),
              array("appn2", "pub1"),
              array("appn3", "pub1" ,"pub2")
            ); 

The first item in each array is application number and the rest in each array are the publication numbers. I get the first item(application number) and the last item of each array(latest publication number) like this

 $index = count(array_keys($shop));
    for($i=0;$i<$index;$i++){

        $appln_nr = $shop[$i][0];
        echo $appln_nr;

        $publn_nr_index = count(array_keys($shop[$i]))-1;
        $publn_nr = $shop[$i][$publn_nr_index];
        echo $publn_nr;
   }

Now I have application number and publication number for each inner array.

I want to create an associative array from the application numbers and publication numbers.

where the key should be the application number and its value is the publication number.

Thanks

EDIT

What I am getting from $shop array

 Array
 (
  [0] => Array
    (
        [0] => appn1
        [1] => pub1
        [2] => pub2
        [3] => pub3
    )

  [1] => Array
    (
        [0] => appn2
        [1] => pub1
    )

  [2] => Array
    (
        [0] => appn3
        [1] => pub1
        [2] => pub2
    )
)

And this is what I need in my associative array

Array(
    "appn1" => "pub3"
    "appn2" => "pub1"
    "appn3" => "pub2"
)

解决方案

Finally i understood what you wanted, after your edit XD:

$shop = array(
    array("appn1", "pub1" ,"pub2" , "pub3"),
    array("appn2", "pub1"),
    array("appn3", "pub1" ,"pub2")
);
$shopNew = array();

foreach($shop as $value){
    $shopNew[$value[0]] = end($value);
}

// now if you want you can replace $shop and unset $shopNew
$shop = $shopNew;
unset($shopNew);    

print_r($shop); 

the output is this:

Array (
  [appn1] => pub3
  [appn2] => pub1
  [appn3] => pub2
)

这篇关于在创建PHP关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆