I need to create a similar array to this:
$need = Array(
"smsSend" => Array(
"account" => Array(
"user" => 123,
"password" => "xxxxxx",
"profile" => 123456
)
),
"smsConfig" => Array
(
"region" => Array
(
"locale" => "es_ES",
"timezone" => "America/Bogota"
),
"send" => Array
(
"from" => "9:00:00",
"to" => "21:00:00"
),
"sms" => Array
(
"channel" => "SMS",
"from" => "LINIO",
)
),
"templateConfig" => Array
(
"template" => Array
(
"postpago" => 1111,
"prepago" => 0010,
"notificar" => 1112
)
),
"fieldsConfig" => Array
(
"fields" => Array
(
"nombre" => "firstname",
"carrier" => "nome_transportadora",
"track" => "track",
"cantidad" => "total_depois_de_impostos"
)
),
"serverConfig" => Array
(
"test" => "http://miportal",
"prod" => "",
"mode" => "test",
"adapter" => "curl",
"type" => "post",
"telephone" => "12345"
),
"fields" => Array
(
"sms" => Array
(
"address" => "mobile_phone"
),
"email" => Array
(
"address" => "email_cliente"
)
)
);
From this arrangement to receive from $_POST:
$post = Array(
"smsSend" => Array(
"account" => Array(
"user:123",
"password:xxxxxx",
"profile:123456"
)
),
"smsConfig" => Array
(
"region" => Array
(
"locale:es_ES",
"timezone:America/Bogota"
),
"send" => Array
(
"from:9:00:00",
"to:21:00:00"
),
"sms" => Array
(
"channel:SMS",
"from:LINIO",
)
),
"templateConfig" => Array
(
"template" => Array
(
"postpago:1111",
"prepago:0010",
"notificar:1112"
)
),
"fieldsConfig" => Array
(
"fields" => Array
(
"nombre:firstname",
"carrier:nome_transportadora",
"track:track",
"cantidad:total_depois_de_impostos"
)
),
"serverConfig" => Array
(
"test:http://miportal",
"prod:",
"mode:test",
"adapter:curl",
"type:post",
"telephone:12345"
),
"fields" => Array
(
"sms" => Array
(
"address:mobile_phone"
),
"email" => Array
(
"address:email_cliente"
)
)
);
The problem is the cycle to assemble items of the parent, I use tree function to create levels:
function createLevel1($array_data) {
$array_push = array();
foreach ($array_data as $key_l1 => $elem_l1) {
array_push($array_push[$key_l1], "");
createLevel2($elem_l1, $array_push, $key_l1);
// print_r($resl1);
}
return $array_push;
}
function createLevel2($elemento_array, $push_array, $parent_key) {
foreach ($elemento_array as $key_l2 => $elem_l2) {
if (is_array($elem_l2)) {
$push_array[$parent_key][$key_l2] = "";
createLevel3($elem_l2, $push_array, $parent_key, $key_l2);
} else {
$items = explode("::", $elem_l2);
$push_array[$parent_key][$items[0]] = $items[1];
}
}
return $push_array;
}
function createLevel3($elemento_array, $push_array, $parent_key, $parentl2_key) {
$push_array[$parent_key][$parentl2_key] = "";
foreach ($elemento_array as $key_l3 => $elem_l3) {
if (is_array($elem_l3)) {
createLevel3($elem_l3, $push_array, $parent12_key, $key_l3);
} else {
$items = explode("::", $elem_l3);
$push_array[$parent_key][$parentl2_key][$items[0]] = $items[1];
}
}
return $push_array;
}
$parameter = $_POST['postData'];
#$info = createLevel1($parameter);
print_r($parameter);
The value of the parameter is array $post, the problem is when I print the result $info like this, the option is blank.:
Array
(
[smsSend] =>
[smsConfig] =>
[templateConfig] =>
[fieldsConfig] =>
[serverConfig] =>
[fields] => Array
(
[sms] =>
[email] =>
)
)
$need = array();
foreach ($post as $key => $value) {
if (is_array($value)) {
$need[$key] = $value;
foreach ($value as $key2 => $value2) {
if (is_array($value2)) {
foreach ($value2 as $key3 => $value3) {
if (is_array($value3)) {
} elseif (strpos($value3, ':') !== FALSE) {
$tmp3 = explode(':', $value3);
$need[$key][$key2][$tmp3[0]] = $tmp3[1];
unset($need[$key][$key2][$key3]);
}
}
} elseif (strpos($value2, ':') !== FALSE) {
$tmp2 = explode(':', $value2);
$need[$key][$tmp2[0]] = $tmp2[1];
unset($need[$key][$key2]);
}
}
}
}
I have wrote this code. I think it is working. But issue is there. when i use explode it divide http://miportal.com also. try another method like strstr. Try it with recursive function.
Related
I'm trying to extract data from a multidimensional array and then putting into one of my own so that I can load it into my database.
Source:
array( "ListOrdersResult" =>
array ( "Orders" =>
array( "Order" =>
array( [0] => {
"Title" => $productTitle,
"customer_name" => $customerName,
"customer_id" => $customerId,
"random_info" => $randomInfo
},
[1] => {
"Title" => $productTitle,
"customer_name" => $customerName,
"customer_id" => $customerId,
"random_info" => $randomInfo
}
)
)
)
To do this, I'm cycling through it like this - I have no issues with extracting data.
My code:
$count = count($listOrderArray['ListOrdersResult']['Orders']['Order']);
//Cycle through each Order to extract the data I want
for($i = 0; $count > $i; $i++) {
$baseArray = $listOrderArray['ListOrdersResult']['Orders']['Order'][$i];
foreach($baseArray as $key => $value) {
if($key == "Title" || $key == "customer_id") {
//ADD TO multidimensional array
}
}
}
How I'm trying to structure it.
array( [0] => {
array(
"Title" => $title,
"customer_id" => $customer_id
},
[1] => {
"Title" => $nextTitle,
"customer_id" => $next_customer_id
}
);
The ultimate goal is to make it easier to load the information into the database by gathering the data by record and then loading it to the database rather than loading by creating an new record and then coming back and modifying that record. To me that seems like it would take more resources and has a higher chance of inconsistent data, but I'm new so I could be wrong.
Any help would be greatly appreciated.
You only have to unset keys you don't want:
$result = array_map(function ($i) {
unset($i['customer_name'], $i['random_info']);
return $i;
}, $listOrderArray['ListOrdersResult']['Orders']['Order']);
More about array_map
Or you also can select the keys you want:
$result = array_map(function ($i) {
return ['Title' => $i['Title'], 'customer_id' => $i['customer_id']];
}, $listOrderArray['ListOrdersResult']['Orders']['Order']);
About your code and question:
$count = count($listOrderArray['ListOrdersResult']['Orders']['Order']);
//Cycle through each Order to extract the data I want
for($i = 0; $count > $i; $i++) {
There's no reason to use a count and a for loop, use foreach.
array( [0] => {
array(
"Title" => $title,
"customer_id" => $customer_id
},
[1] => {
"Title" => $nextTitle,
"customer_id" => $next_customer_id
}
);
doesn't make sense, what are these curly brackets? You should write it like this if you want to be understood:
array(
[0] => array(
"Title" => "fakeTitle0",
"customer_id" => "fakeCustomerId0"
),
[1] => array(
"Title" => "fakeTitle1",
"customer_id" => "fakeCustomerId1"
)
);
You have this initial variable.
$listOrderArray = array(
"ListOrdersResult" => array(
"Orders" => array(
"Order" => array(
0 => array(
"Title" => "productTitle",
"customer_name" => "customerName",
"customer_id" => "customerId",
"random_info" => "randomInfo",
),
1 => array(
"Title" => "productTitle",
"customer_name" => "customerName",
"customer_id" => "customerId",
"random_info" => "randomInfo",
),
)
)
)
);
The only thing you should do is to remove the inner array from the three outer arrays.
Here is the solution:
$orders = $listOrderArray['ListOrdersResult']['Orders']['Order'];
I have a multidimensional array of undefined depth.
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo",
"something" => 42,
"something2" => [1,2,3]
)
)
);
I need to parse through it, find all the values that are plane text and save them in another array keeping the pathway. So I expect the final array be like this:
$array = array(
"foo" => "bar",
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
At the moment I'm trying to use recurrent function
$this->printAll($array);
public function printAll($a)
{
if (!is_array($a)) {
echo $a, ' <br>'; // here we can check if it is string and add to the final array
return;
}
foreach($a as $i=>$v) {
$this->printAll($v);
echo $i;
}
}
Could someone help me to figure out how to keep indexes through iterations and put it in the final array.
<?php
function printAll($array, &$save)
{
foreach ($array as $key => $values)
{
if ( ! is_numeric($values))
{
if (is_array($values))
{
printAll($values, $save[$key]);
}
else
{
$save[$key] = $values;
}
}
}
if ( ! empty($save)) {
$save = array_filter($save);
}
}
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo",
"something" => 42,
"something2" => [1,2,3]
)
)
);
$save = array();
printAll($array, $save);
print_r($save);
Outputs: PHP
Array (
[foo] => bar
[multi] => Array (
[dimensional] => Array (
[array] => foo
)))
Array
(
[0] => Array
(
[source_name] => src_email_24
[suggested_by] => Sameer
[email_id] => sameer.m#blueoceanmi.com
[medium_name] => Email
)
[1] => Array
(
[source_name] => src_email_24
[suggested_by] => Sameer
[email_id] => sameer.m#blueoceanmi.com
[medium_name] => Display
)
)
And if the source_name and medium_name are same then i want the below output
how can i merge the multi dimensional aaray into one array.Any help would be of great help.
Array
(
[0] => Array
(
[source_name] => src_email_24
[suggested_by] => Sameer
[email_id] => sameer.m#blueoceanmi.com
[medium_name] => Email,Display
)
)
hope source_name and email address instead source_name and medium_name; also you should have only one record for a specific email on new data set
<?php
$originalDataSet = [
[
'source_name' => 'src_email_24',
'suggested_by' => 'Sameer',
'email_id' => 'sameer.m#blueoceanmi.com',
'medium_name' => 'Email'
],
[
'source_name' => 'src_email_24',
'suggested_by' => 'Sameer',
'email_id' => 'sameer.m#blueoceanmi.com',
'medium_name' => 'Display'
]
];
$processedDataSet = getProcessedDataFromOriginalDataSet($originalDataSet);
function getProcessedDataFromOriginalDataSet($originalDataSet = []) {
$processedDataSet = [];
foreach ($originalDataSet as $data) {
if (isset($processedDataSet[$data['email_id']])) {
if ($processedDataSet[$data['email_id']]['source_name'] == $data['source_name'] /* && other conditions*/) {
//change only relevant values
}
} else {
$processedDataSet[$data['email_id']] = $data;
}
}
return $processedDataSet;
}
Otherwise something like this
function getProcessedDataFromOriginal($originalDataSet = []) {
$processedDataSet = [];
foreach ($originalDataSet as $originalData) {
$isAdded = 0;
foreach ($processedDataSet as $processedData) {
if ($processedData['source_name'] == $originalData['source_name'] /* && other conditions*/) {
// make changes on processed data set
$isAdded = 1;
break;
}
}
if (!$isAdded) {
$processedData[] = $originalData;
}
}
return $processedDataSet;
}
How can i merge array with the same value?
I have three array which are $participants, $conferance_participants and $contacts
i want to compare values with this three array and merge
for example :
if $participants['calleridnum'] == $conferance_participants['uid'] == $contacts['name']
i want the output to be like this :
Array
(
[0] => Array
(
[calleridnum] => 1
[test] => yay
[uid] => 1
[channel] => deze
[name] => 1
[limit] => 1
)
)
this is my code so far:
<?php
$participants = [
[ 'calleridnum' => 1,
'test' => 'yay'
]
];
$conferance_participants = [
[ 'uid' => 1,
'test' => 'yay2',
'channel' => 'deze'
]
];
$contacts = [
[ 'name' => 1,
'test' => 'yay2',
'limit' => 1
]
];
foreach ($participants as $participant=>$p) {
foreach ($conferance_participants as $conferance_participant=>$c) {
foreach ($contacts as $contact=>$cs) {
if (($p['calleridnum'] == $c['uid']) && ($c['uid'] == $cs['name'])) {
foreach ( $c as $key=>$val ) {
if (!isset($p[$key])) {
$participants[$participant][$key] = $val;
}
}
}
}
}
}
print_r( $participants );
?>
Try to call array_merge() , but you still have to consider the different values with the same key (eg. the values of key 'test' )
if (($p['calleridnum'] == $c['uid']) && ($p['uid'] == $c['name'])) {
$participants[$participant] = array_merge(
$participants[$participant],
$conferance_participants[$conferance_participant],
$contacts[$contact]
);
}
I am having problem with session array variable
$_SESSION ['roomsInfo_' . $intHId] = $strRTypeArr;
It automatically destroy when land on the other page, by using the <form action=”booking_level.php”>
foreach ( $arrRoomInfo as $arrRoomDetails ) {
// occup details
$AdultNum = ( int ) $arrRoomDetails->AdultNum;
$ChildNum = ( int ) $arrRoomDetails->ChildNum;
// child ages details
$strChAgs = '';
if (property_exists ( $arrRoomDetails, 'ChildAges' )) {
$childAgs = $arrRoomDetails->ChildAges->ChildAge;
$arrChAge = array ();
foreach ( $childAgs as $chAgs ) {
$chAgs = $chAgs->attributes ();
$arrChAge [] = $chAgs ['age'];
}
$strChAgs = implode ( ":", $arrChAge );
}
// set array for all the above details
$strRTypeArr [$rtId] [$i] = array (
'myRoomSq' => $i,
'roomSeq' => $arrRmSeq,
'adults' => $AdultNum,
'child' => $ChildNum,
'childAges' => $strChAgs,
'maxGuests' => $maxGuests,
'maxChild' => $maxChild,
'name' => $rmName,
'HotelRoomTypeId' => $rtId,
'roomId' => $roomId,
'isPublish' => $isPublish,
'Occupancy' => array (
"attributes" => array (
'avrNgtlyRtComm' => $avrNgtlyRtComm,
'avrNightPrice' => $avrNtPr,
'bedding' => $bedding
),
"boardBase" => array (
'bbId' => $arrBrdBsId,
'bbName' => $arrBrdBsName,
'bbPrice' => $arrBrdBsPrice,
'bbPublishPrice' => $arrBrdBspBPrce,
'strBBaseExists' => $strBBaseExists
),
'PriceBreakdown' => $arrDay,
"dblBrDownPrTourico" => $dblBrDownPrTourico,
"dblTtlBrDownPr" => $dblTtlBrDownPr,
'Supplements' => $arrSupp
),
'Discount' => $arrDst,
'cancelPolicy' => $strCnlionHtml,
'arrCanPolicy' => $arrCanPolicy,
'arrChcrCanPolicy' => $arrChcrCanPolicy,
'chcr_cancelPolicy' => $chcrCancellation,
'curncy' => $strCurn
);
$i ++;
}
}
}
}
$_SESSION ['roomsInfo_' . $intHId] = $strRTypeArr;
How can I store in Session Array Variable in a MySQL database ?
Just make sure that you assign the array the way you assign a normal value and then store the array in your database.
$_SESSION['dArray'] = $strRTypeArr;
//for example:
$strRTypeArr[0][0] = array (
'myRoomSq' => 2.5,
'roomSeq' => "test this",
'adults' => true,
'child' => 5,
'childAges' => "test"
);
$strRTypeArr[0][1] = array (
'myRoomSq' => 3.5,
'roomSeq' => "this is another test",
'adults' => false,
'child' => 6,
'childAges' => "test"
);
$_SESSION['dArray'] = $strRTypeArr;
echo "<pre>";
print_r($_SESSION['dArray']);
echo "</pre>";
//Store this $_SESSION['dArray'] in your database
You can also use json_encode($_SESSION['dArray']); to store your array in the database.