4

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"
)
4
  • How can I create the associative array from application and publication numbers? Commented Oct 12, 2012 at 16:24
  • Form the above array .. what is your expected output .... Commented Oct 12, 2012 at 16:25
  • @Baba Have a look at my Edit please Commented Oct 12, 2012 at 16:30
  • from what u defined, you have more than one pub_number in one app_number. do you want the value of each element to be an array of pub_numbers Commented Oct 12, 2012 at 16:31

5 Answers 5

4

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
)
Sign up to request clarification or add additional context in comments.

3 Comments

This is already I ma doing i want to change it now to associative array
@UmairIqbal it is an associative array. That's what's being shown except he's var_dumped the result instead of print_r
I edited my answer because the question wasn't clear at first, @UmairIqbal 's comment is before the edit :P
1

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)

Comments

1

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

Comments

0

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

Comments

0

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.