Question: I am being passed inconsistent data. The problem is the individual rows of `$data_array` are not consistently in the same sequence but each has a reliable "text:" preceding the value. 

Each row contains about 120 elements of data. I only need 24 of those elements.

It's also possible one of the elements I need could be missing, such as "cost".

(I'm using php version 5.4)  

-- Task: 

Using $order\_array, create a new $data\_array\_new by reordering the data in each "row" into the same sequence as $order\_array.

If an elements is missing from a row insert "NA".

Once the elements are in the correct sequence the "text" is no longer required.

```
$order_array = array("price", "cost", "vol", "eps")
$data_array = Array (
    $one = Array ("cost":43.40, "vol":44000,   "eps":1.27,   "price":65.00),
    $two = Array ("eps":5.14,   "price":33.14, "vol":657000),
    $thr = Array ("vol":650000, "cost":66.67,  "eps":1.33,   "price":44.31),
);

```

The resulting ouput should appear with the data in this order: ("price", "cost", "vol", "eps")

```
$data_array_new = Array (
    $one = Array (65.00,43.40,44000,1.27),
    $two = Array (33.14,"NA",657000,5.14),
    $thr = Array (44.31,66.67,650000,1.33),
);

```

Answer: 
> 
> Is this possible?
> 
> 
> 

It definetely is. You could just check which `Activity` is hosting your `Fragment` instance: 

```
private void button1OnClick(){
 /* could also use instanceof, BUT: if you have something like ActivityC extends ActivityA 
         then instanceof would evaluate to true for both */
   if(getActivity().getClass().equals(ActivityA.class)) {
     // do stuff
   } else if(getActivity().getClass().equals(ActivityB.class)) {
     // do another stuff
   }
}

```

> 
> Is this the right way?
> 
> 
> 

(*attention opinionated answer*)
It depends. If you have a complex and unique layout/functionality, I'd use different `Fragments`. If you have a simple layout with some buttons that just need to act differently in different `Activities` it is a good idea to reuse an existing `Fragment` class.
Your activities have different logic, you can define the button logic in each of them and share the views in this way. You can use a fragment to accomplish this however you can be more direct by sharing a partial layout.

Create a partial layout called three\_buttons.xml

three\_buttons.xml

```
<LinearLayout>
<BUtton android:text="button 1"/>
<BUtton android:text="button 2"/>
<BUtton android:text="button 3"/>
</LinearLayout>

```

activity\_a.xml 

```
<LinearLayout>
<TextView android:text="I am A"/>
<include
            android:id="@+id/three_buttons"
            layout="@layout/three_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
</LinearLayout>

```

activity\_b.xml 

```
<LinearLayout>
<TextView android:text="I am B"/>
<include
            android:id="@+id/three_buttons"
            layout="@layout/three_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
</LinearLayout>

```