I have this piece of code:
<?php
require_once ('mercadopago.php');
$monto = $_POST['amount'];
$mp = new MP('XXXX', 'XXXXXXXXXXX');
$preference_data = array(
"items" => array(
array(
"title" => "item",
"quantity" => 1,
"currency_id" => "usd",
"unit_price" => HERE
)
)
);
$preference = $mp->create_preference ($preference_data);
?>
and I need to make the variable $monto that i define from a post send before to give its value to "Unit_price" where it says "HERE". I tried just writting $monto, but it didnt work.
there is some how i can do this? thanks u and sorry for my english. its not pretty good.
you can just use the variable, like:
//check if your POST data is not empty and assign some default value in case its empty
$monto = (!empty($_POST['amount'])) ? $_POST['amount'] : 0; //0 is default value
$preference_data = array(
"items" => array(
array(
"title" => "item",
"quantity" => 1,
"currency_id" => "usd",
"unit_price" => $monto
)
)
);
<?php
$preference_data = array(
"items" => array(
array(
"title" => "RosquinhaPvP - NickDoPlayer - VIP",
"quantity" => 1,
"currency_id" => "BRL",
"unit_price" => doubleval($monto)
)
)
);
Related
I have a problem which I cant figure out. I have an array $navItems. In there I have multiple arrays with the items.
$navItems = array(
array(
"url" => 'index.php',
"title" => 'Home',
"id" => 1
),
array(
"url" => "pages/projects.php",
"title" => "Projects",
"id" => 2
),
array(
"url" => "pages/about.php",
"title" => "About",
"id" => 3
),
array(
"url" => "pages/contact.php",
"title" => "Contact",
"id" => 4
),
array(
"url" => "pages/login.php",
"title" => "Login",
"id" => 5
),
);
// Database conectie en query
$dbh = getDbConnection();
$sql = "SELECT * FROM pages";
$pages = $dbh->query($sql);
// Navitems
$navItems = array();
foreach($pages as $singe_page){
$page_title = $singe_page['title'];
$page_url =$singe_page['url'];
$page_id = $singe_page['id'];
$pagina = array(
'title' => $page_title,
'url' => $page_url,
'id' => $page_id
);
array_push($navItems, $pagina);
}
//This is the code on my array.php page. And it loads that in on the head.php
But now I want to add all the page arrays with an foreach loop. Because I have the pages saved in my database. And now I'm trying to pull the pages out my database and put them in the array.
But I just don't know anymore...
I just want for every record in my database it to throw that in an array and that array in the $navItems array.
Nevermind. When I was typing this I changed a little bit of code. And now it works. I added the array_push($navItems, $pagina) in the foreach loop. And it works.
I have a json array and i want to filter an json object which **state **keys contains this word "ALTS".
<pre>
$arr = array(
array(
"region" => "valore1",
"price" => "valore11",
"state" => "declare par /AMG/OMS/FRA/"
),
array(
"region" => "valore2",
"price" => "valore22",
"state" => "declare par /AMG/OMS/ALTS/"
),
array(
"region" => "valore4",
"price" => "valore44",
"state" => "declare par /AMG/OMS/FRA/"
),
array(
"region" => "valore5",
"price" => "valore55",
"state" => "declare par /AMG/OMS/ALTS/"
),
array(
"region" => "valore3",
"price" => "valore33",
"state" => "declare par /AMG/OMS/ALTS/PT"
)
);
$myJsonArray = json_encode($arr);
echo $myJsonArray;
</pre>
I want to find an example of regular expression which filters this array and only returns an array containing objects with a state containing "/ ALTS".
Any ideas ?
You don't need regex for that. If you find one word you can use strpos function.
$newArr = [];
$findTerm = '/ALTS/';
foreach($arr as $key => $valArr)
{
if(strpos($valArrp['state'], $findTerm) !== false) {
$newArr[] = $valArr;
}
}
I need to remove Jackie Jackson from this array, I tried unset(I do not want to use key), array_diff, array_search. Nothing is working for me.
$employeeList= array(
array(
"ID" => "ID",
"Name" => "Name",
"Surname" => "Surname",
),
array(
"ID" => 1,
"Name" => "John",
"Surname" => "Smith",
),
array(
"ID" => 2,
"Name" => "Jackie",
"Surname" => "Jackson",
),
array(
"ID" => 3,
"Name" => "Chris",
"Surname" => "Jones",
),
array(
"ID" =>4,
"Name" => "Amanda",
"Surname" => "Cullen",
),
array(
"ID" =>5,
"Name" => "Jeremy",
"Surname" => "Goodwin",
),
);
if you want to unset you can use the id of Jackie Jackson to match his key.
$id = 2;
unset($employeeList[array_search($id,array_column($employeeList, "ID"))]);
Have fun :)
Array filter to check if the name and surname match. The string must contain name and surname separated by a space in this case so we can "explode" and pick up each individually. If name or surname do not exist in the "name_to_exclude" the function will simply return the original array.
$name_to_exclude = "Jackie Jackson";
$exclude = explode(' ', $name_to_exclude);
$employeeList = array_filter($employeeList, function($val) use ($exclude) {
if(array_key_exists(0, $exclude) && array_key_exists(1, $exclude)) {
return $val['Name'] != $exclude[0] && $val['Surname'] != $exclude[1];
}
return true;
});
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've that proceed this array in PHP
array(
"id" => 1,
"name" => "Carlos"
"other" => array("key" => "Hello")
),
array(
"id" => 3,
"name" => "Carlos"
"other" => array("key" => "Hello")
),
array(
"id" => 2,
"name" => "Carlos"
"other" => array("key" => "Hello")
)
and I need to order by "id". I've try it using usort and many multidimensional solutions but doesn't work for me.
I used that:
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
But doesn't work because my array has many dimentions.
$departamento = $this->Departamentos->get($id, [
'contain' => [
'Asignaturas.Mallas',
'Asignaturas.Secciones.Perfiles',
'Asignaturas.Secciones.Mallas.Carreras',
'Unidades'
]
]);
That is my query in Cakephp. I need to order by Secciones.id
I used Hash::sort
http://book.cakephp.org/3.0/en/core-libraries/hash.html
And works fine for me ;)