Wearybands
Wearybands

Reputation: 2455

Creating Associative Array in PHP

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

Upvotes: 4

Views: 33305

Answers (5)

Hitesh Tarbundiya
Hitesh Tarbundiya

Reputation: 464

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 )

Upvotes: 0

aleation
aleation

Reputation: 4844

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
)

Upvotes: 4

Teena Thomas
Teena Thomas

Reputation: 5239

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

Upvotes: 0

hakre
hakre

Reputation: 197564

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

Upvotes: 1

Baba
Baba

Reputation: 95101

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)

Upvotes: 1

Related Questions