Creating Associative Array in PHP

北城以北 提交于 2019-12-05 01:19:50

问题


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"
)

回答1:


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
)



回答2:


You can try

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

$final = array();
array_map(function ($var) use(&$final) {$final[reset($var)] = end($var);}, $shop);
var_dump($final);

Output

array
  'appn1' => string 'pub3' (length=4)
  'appn2' => string 'pub1' (length=4)
  'appn3' => string 'pub2' (length=4)



回答3:


You can easily convert your array into a new format by using the first element as key (see reset) and the last element (see end) as value:

foreach($shop as $fl) {
    $v[reset($fl)] = end($fl);
}

Result is in $v then.

If you want to transform the array you need to delete each element as well:

foreach($shop as $v => $fl) {
    $shop[reset($fl)] = end($fl);
    unset($shop[$v]);
}

Result is in $shop then. Unset takes care of removing from the array.

Output in both cases is:

array(3) {
  'appn1' =>
  string(4) "pub3"
  'appn2' =>
  string(4) "pub1"
  'appn3' =>
  string(4) "pub2"
}



回答4:


try this:

 foreach($shop as $k => $v) {            
     $new_arr[$v[0]] = end($v);      
 }

It should give you this result,

$new_arr = array(
      "appn1" => "pub3",
      "appn2" => "pub1",
      "appn3" => "pub2"-
);



回答5:


You can also create like this,

$arrField = [];
$arrField['id'] = '0001';
$arrField["first_name"] ='Demo Test';
print_r($arrField);

print_r($arrField) display output like this.

Array ( [id] => 0001 [first_name] => Demo Test )


来源:https://stackoverflow.com/questions/12863259/creating-associative-array-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!