Trying to set array variable from part of another array variable - php

I'm trying to set a part of array variable into a new array variable like below:
$this->data['uploadFront'] = array();
$this->data['uploadFront'] = $this->data['Card']['uploadFront'];
But I'm getting undefined index error.
The $this->data['Card'] array is like below:
Array
(
[Card] => Array
(
[name] =>
[company_name] =>
[firstname] =>
[lastname] =>
[position] =>
[location] =>
[phone] =>
[website] =>
[mobile] =>
[comp_name] => 0
[uploadFront] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[uploadBack] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
)
What could be wrong that needs to be fixed in this process?

$this->data['uploadFront'][] = $this->data['Card']['uploadFront'];
If you want to append to array, you have to use [] to show that you are appending, not assinging.

Sorry, I didn't read your post right the first time.
As Catalin told you, if your array is what you tell us, ie $this->data['Card'] = ARRAY, you're creating an unnecessary extra level: two times 'Card', so ['Card']['Card']. I propose you define $this->data as what you told is your array, and then ask to copy ['uploadFront'] to the first level of your $this->data array. In what you described you're trying to access an unexisting part of the array.
In code (probably you'll need to declare the $this->data array somewhere else in your classes, but for the sake of explaining I write it down like this):
$this->data = array
(
'Card' => array
(
'name' => '',
'company_name' => '',
...
'uploadFront' => array
(
'name' =>'',
'type' => '',
'tmp_name' => '',
'error' => '4',
'size' => '0'
)
'uploadBack' => array
(
...
)
)
$this->data['uploadFront'] = $this->data['Card']['uploadFront'];

Related

Merging PHP array adding key

Ok, this question was probably asked many times here. Tried searching for a way that works but I dont find the correct term to search it.
I have following 2 array
Array 1
Array
(
[1] => Array
(
[data] => DFF022
)
[2] => Array
(
[data] => DFF026
)
)
Array 2
Array
(
[0] => Array
(
[number] => INC0000002
[ia] =>
[description] => Printer not working
[state] => Monitoring - Waiting for Client
[updated] => 12/30/2020 19.09.01
[opened] => 12/24/2020 20.35.36
)
[1] => Array
(
[number] => INC0000003
[ia] =>
[description] => Monitor broke down
[state] => Pending - Awaiting Change Approval/Implementation
[updated] => 12/29/2020 23.57.06
[opened] => 12/29/2020 08.21.38
)
)
Now the number of item inside the array is always will be same. If array 1 got 10 items, then array 2 will have 10 items as well.
I'm looking a way to merge the array to something like this
Array
(
[0] => Array
(
[number] => INC1879727
[ia] =>
[description] => Unable to replay CME NS message
[state] => Monitoring - Waiting for Client
[updated] => 12/30/2020 19.09.01
[opened] => 12/24/2020 20.35.36
[data] => DFF022
)
[1] => Array
(
[number] => INC1884171
[ia] =>
[description] => mw_uat - UAT00MSV_LNP6_01_pga_aggregate_limit
[state] => Pending - Awaiting Change Approval/Implementation
[updated] => 12/29/2020 23.57.06
[opened] => 12/29/2020 08.21.38
[data] => DFF026
)
)
Any idea on how to accomplish this?
Using array merge or combine just combines the array and I have double the items I wanted.
You can run a foreach() to inject the data. To directly modify array elements of $arr2 within the loop we precede $value with &, so the value is assigned by reference:
<?php
$arr1 = [
['data' => 'DFF022',],
['data' => 'DFF026',],
];
$arr2 = [
[
'number' => 'INC0000002',
'ia' => '',
'description' => 'Printer not working',
'state' => 'Monitoring - Waiting for Client',
'updated' => '12/30/2020 19.09.01',
'opened' => '12/24/2020 20.35.36',
],
[
'number' => 'INC0000003',
'ia' => '',
'description' => 'Monitor broke down',
'state' => 'Pending - Awaiting Change Approval/Implementation',
'updated' => '12/29/2020 23.57.06',
'opened' => '12/29/2020 08.21.38',
],
];
foreach($arr2 as $key => &$value) {
$value['data'] = $arr1[$key]['data'];
}
Output (print_r($arr2)):
Array
(
[0] => Array
(
[number] => INC0000002
[ia] =>
[description] => Printer not working
[state] => Monitoring - Waiting for Client
[updated] => 12/30/2020 19.09.01
[opened] => 12/24/2020 20.35.36
[data] => DFF022
)
[1] => Array
(
[number] => INC0000003
[ia] =>
[description] => Monitor broke down
[state] => Pending - Awaiting Change Approval/Implementation
[updated] => 12/29/2020 23.57.06
[opened] => 12/29/2020 08.21.38
[data] => DFF026
)
)
working demo

PHP Nested Array loop nested results

I am experimenting with my first API and getting stuck with the results. I am getting an Array Back:
Array
(
[GetOrderListResult] => Array
(
[Status] => Success
[MessageCode] => 0
[ResultData] => Array
(
[OrderResponseItem] => Array
(
[0] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T11:41:45.953
[TotalOrderAmount] => 12.7800
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138711
[ClientOrderIdentifier] => 138711
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
[1] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T12:59:01.78
[TotalOrderAmount] => 6.3900
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138750
[ClientOrderIdentifier] => 138750
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
)
)
)
)
Now the onyl way I know how to reference a fied such as the order id in the array is:
echo "Order ID: ".$result['GetOrderListResult']['ResultData']['OrderResponseItem']['0']['OrderID'];
But I want to be able to loop through the array of orders and execute code for each item, could somewbody please point me in the right direction for:
a) is there a better way to refernce these fields?
b) how do I loop through the OrderResponseItem part of the array?
The only loop I could think of was for the whole array not nested items in the array.
Sorry if I'm missing something simple....
Thanks and if you need the data in any other format please let me know.
Since you already know the keys you could just use a foreach to access them an point to that key then loop. Something like this:
foreach($result['GetOrderListResult']['ResultData']['OrderResponseItem'] as $value) {
$order_id = $value['OrderID'];
// your other processes
}

How should I merge the associative array with $_FILES array in a way I wanted?

I've an associative array called $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[pt_doc_title] => Array
(
[1] => Test Document
[2] => New Joining
[3] => Hallo Jolly
)
[pt_doc_id] => Array
(
[0] => 6
[1] => 7
)
[submit] => Update
)
In order to keep all the package type documents data together I've manipulated the above array as follows:
foreach ($data['pt_doc_title'] as $key => $title) {
$id = isset($data['pt_doc_id'][$key-1]) ? $data['pt_doc_id'][$key-1] : null;
$data['pt_documents_data'][] = array(
'pt_doc_title' => $title,
'pt_doc_id' => $id
);
}
unset($data['pt_doc_title'], $data['pt_doc_id']);
After manipulation I'm getting following array $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
)
)
)
My issue is I'm haivng another array called $_FILES as follows and I want to merge one of it's key(name) into above array in a same manner.
Array
(
[document_file_name_1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_3] => Array
(
[name] => FAQ.doc
[type] => application/msword
[tmp_name] => /tmp/phpFiBYKB
[error] => 0
[size] => 35840
)
)
That is if there exists a value under [name] then the final array should be as follows. As there is a value present only in last array element of array $_FILES
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
[pt_doc_file_iname] =>
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
[pt_doc_file_iname] =>
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
[pt_doc_file_iname] => FAQ.doc
)
)
)
Can anyone please help me in creation of such final array?
Simplest way would be to walk the $_FILES array and extract the id from the last segment of the field name... then use that -1 as the basis for adding the file to your result array.
Something like this should get you there (untested):
foreach($_FILES as $k => $file){
$index_to_update = trim(substr($k, strrpos($k, "_")+1))-1;
$res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = isset($file["name"])?$file["name"]:"";
}
That is assuming $res is the array that is the parent of the pt_document_data element.

Fetching mysql records from CakePHP with specific index

I am using the following cakephp query to retrieve data from mysql:
$tops = $this->PageBanner->find('all', array(
'conditions' => array(
'PageBanner.status' => 1
),
'fields' => array(
'PageBanner.page_url',
'PageBanner.image',
'PageBanner.logo',
'PageBanner.logo_text',
'PageBanner.content'
)
));
This query returns me the following results:
[0] => Array
(
[PageBanner] => Array
(
[page_url] => index
[image] => home_banner.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
)
[1] => Array
(
[PageBanner] => Array
(
[page_url] => write_review
[image] => kids2.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
)
But I want the data to be returned in the following format:
[index] => Array
(
[page_url] => index
[image] => home_banner.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
[write_review] => Array
(
[page_url] => write_review
[image] => kids2.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
I need page_url field content in place of Array index (e.i. 0, 1). Is that possible to get data in this format or I need to manually configure the arrays?
$result = Set::combine($tops, '{n}.PageBanner.page_url', '{n}.PageBanner');
pr($result);

complex multidimentional associative array process with foreach

I've had to ask this one again, sorry, but I'm having a problem trying to process this array. I have tried several different ways but none where right, here's the array:
Array (
[search] => Array (
[response] => Array (
[errors] =>
[number_of_hotels] => 1 of 1
)
[lr_rates] => Array (
[hotel] => Array (
[hotel_ref] => 3116
[hotel_currency] => [U] => USD
[hotel_rooms] => Array (
[room] => Array (
[ref] => 6382
[type] => 1
[type_description] => Standard
[sleeps] => 8
[rooms_available] =>
[adults] => 8
[children] =>
[breakfast] => false
[dinner] => false
[description] =>
[alternate_description] =>
[rack_rate] => 82.01
[date] => 19/08/201220/08/201221/08/2012
[numeric_hotelcurrencyprice] => FullFullFull
[formatted_date] => 19 August 201220 August 201221 August 2012
[price] => FullFullFull
[hotelcurrencyprice] => FullFullFull
[numeric_price] => FullFullFull
[requested_currency] => GBPGBPGBP
[numeric_hotelcurrencyprice] => FullFullFull
[available_online] => false
[minimum_nights] => 1
[bed_type] =>
[cancellation_policy] =>
[cancellation_days] =>
[cancellation_hours] =>
[room_terms] =>
)
[room] => Array (
[ref] => 6382
[type] => 1
[type_description] => Standard
[sleeps] => 8
[rooms_available] =>
[adults] => 8
[children] =>
[breakfast] => false
[dinner] => false
[description] =>
[alternate_description] =>
[rack_rate] => 82.01
[date] => 19/08/201220/08/201221/08/2012
[numeric_hotelcurrencyprice] => FullFullFull
[formatted_date] => 19 August 201220 August 201221 August 2012
[price] => FullFullFull
[hotelcurrencyprice] => FullFullFull
[numeric_price] => FullFullFull
[requested_currency] => GBPGBPGBP
[numeric_hotelcurrencyprice] => FullFullFull
[available_online] => false
[minimum_nights] => 1
[bed_type] =>
[cancellation_policy] =>
[cancellation_days] =>
[cancellation_hours] =>
[room_terms] =>
)
)
[cancellation_type] => First Night Stay Chargeable
[cancellation_policy] => 2 Days Prior to Arrival
[CityTax] => Array (
[TypeName] =>
[Value] =>
[OptedIn] =>
[IsCityTaxArea] =>
)
)
)
)
)
Ok, I need to traverse the array and create a loop, so for every instance of ROOM it will repeat the process. Then i need to extract the data from room array and use it to populate rows in MySQL for each instance of room.
This is the code I have so far which prints the names and values in the room array. However, it only gets one of the room arrays. What can I do to read all of the rooms? I am also thinking this is too many for-each but don't seem to be able to traverse down ['']['']['']...
or by just using the associative name.
foreach($arr['search'] as $lr_rates) {
foreach($lr_rates['hotel'] as $hotel) {
foreach($hotel['room'] as $field => $value){
print $field;print $value;
}
}
}
It might also be worth mentioning the values in these arrays are always fluctuating.
I think you can really simplify this quote a bit. If you know that this will always be the structure then you can jump right down into the hotels and then into the rooms.
foreach($arr['search']['lr_rates']['hotel'] as $hotel) {
// You can access all of the keys in the hotel array here
foreach($hotel['hotel_rooms'] as $room) {
// Do stuff with the room array
}
}
I would recommend either building your insert script on the fly and calling the database just once for the write, or if you are updating then using a transaction. As the number of rooms gets larger you will slow your script down with a bunch of writes to disk.
the formatting of your data's output is very bad and unreadable. I cannot really identify what you are trying to do.
possibility: the inner array [hotel_rooms] => Array () uses the key room multiple times. as array keys are unique, you overwrite the data at the index room. this is why you only get one room.
possibility: there are rooms inside a room -> use a recursive function to iterate over all rooms like this:
function handleRoom(array $room) {
// do something with $room
if (array_key_exists('room', $room)) {
handleRoom($room['room']);
}
}
$room = array(
'some' => 'room',
'data' => 'and another',
'room' => array(
'is' => 'inside',
'of the' => 'main room',
),
);
handleRoom($room);

Categories