I'm working on a project with an API.
I gather data from a form and send this to their website.
The data is gathered through an array and then encoded to json format:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
Then the post request.
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I can also do a get request, this get request returns a multi dimensional array:
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1');
If i do a var_dump on $tet i get this:
array(3) {
["data"] => array(2) {
[0] => array(11) {
["id"] => string(39)
"person:067af3bd2045824e62ac579e634623b8" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3552" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#03e084"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:0f16f418f1845749c79bebf9e1e753e5" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}[1] => array(11) {
["id"] => string(39)
"person:067af3bd2045824ea8c16e7ea0baf9d6" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3553" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#dce1f3"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:f48fdcaaff0211e728a2e4ccf197900b" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}
}["errors"] => NULL["debug"] => NULL
}
I dont want double values. For example if 'first_name' => $_POST['billing_first_name'],(in the $pers_payload variable) exists in $tet as a value. then dont run
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
else run:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I tried this from another question i asked:
foreach (array_keys($tet['data']) as $key) {
if (array_key_exists('first_name', $tet['data'][$key])
&& (strcasecmp($tet['data'][$key]['first_name'], 'Kevin1') == 0) ) {
echo "found key 'first_name' with value '" . $tet['data'][$key]['first_name'] . "'\n";
} else {
// perform your post request
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
}
This doesn't run the post request at all even if the value of first_name doesnt exist
I found the solution.
I accomplished this with a simple if statement:
if(strtolower($_POST['billing_last_name']) == strtolower($getOrg['data']['0']['linked_persons_contacts']['0']['family_name'])) {
// do nothing
} else {
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
Related
i'm new in twig. I had read the twig documentation but can nt find solution, as it is much easier in PHP. ((Trying two array in one loop.))
: i have two arrays one is {{ dump(render_categories) }}--
array(3) {
[0] => array(3) {
[0] => array(2) {
["category_id"] => string(2)
"65" ["name"] => string(11)
"Annivarsary"
} [1] => array(2) {
["category_id"] => string(2)
"67" ["name"] => string(9)
"Christmas"
} [2] => array(2) {
["category_id"] => string(2)
"66" ["name"] => string(8)
"Birthday"
}
} [1] => array(4) {
[0] => array(2) {
["category_id"] => string(2)
"61" ["name"] => string(6)
"Mother"
} [1] => array(2) {
["category_id"] => string(2)
"62" ["name"] => string(6)
"Sister"
} [2] => array(2) {
["category_id"] => string(2)
"60" ["name"] => string(8)
"Daughter"
} [3] => array(2) {
["category_id"] => string(2)
"63" ["name"] => string(10)
"Girlfriend"
}
} [2] => array(5) {
[0] => array(2) {
["category_id"] => string(2)
"73" ["name"] => string(8)
"Earrings"
} [1] => array(2) {
["category_id"] => string(2)
"72" ["name"] => string(22)
"Necklaces&Pendants"
} [2] => array(2) {
["category_id"] => string(2)
"71" ["name"] => string(5)
"Rings"
} [3] => array(2) {
["category_id"] => string(2)
"70" ["name"] => string(9)
"Bracelets"
} [4] => array(2) {
["category_id"] => string(2)
"69" ["name"] => string(6)
"Charms"
}
}
}
Second is {{ dump(load_steps) }} : (of different length)
array(3) {
[0] => array(4) {
["id"] => string(1)
"1" ["category_id"] => string(2)
"64" ["heading"] => string(21)
"What is the occasion?" ["status"] => string(1)
"1"
} [1] => array(4) {
["id"] => string(1)
"2" ["category_id"] => string(2)
"59" ["heading"] => string(5)
"For which Person" ["status"] => string(1)
"1"
} [2] => array(4) {
["id"] => string(1)
"3" ["category_id"] => string(2)
"68" ["heading"] => string(24)
"Type text heading 878787" ["status"] => string(1)
"1"
}
}
I'm trying to get Output like:
What is the occasion? --(get from second array)--
Annivarsary, Christmas, Birthday --(get from First array)--
For which Person? --(get from second array)--
Mother, Sister, Daughter, Girlfriend --(get from First array)--
------So, on-----
Any help is appreciated, ThankYou.
Wouldn't this do the trick ?
{% for key, step in load_steps %}
{{ step['heading'] }}
{% for category in render_categories[key] %}
{{ category['name'] }}
{% endfor %}
{% endfor %}
I have this categories into my wordpress (woocommerce), and I need to build a menĂ¹ like this using data retrieved from woocommerce api.
What's an efficient way to build a menĂ¹ like this? I have tried to inspect wordpress code without figured out.
This API lets me retrieve all product categories.
https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-product-categories
this is the object obtained from request:
array(5) {
[0] => object(stdClass)#77 (10) {
["id"] => int(15)
["name"] => string(13) "Uncategorized"
["slug"] => string(13) "uncategorized"
["parent"] => int(0)
["description"] => string(0) ""
["display"] => string(7) "default"
["image"] => NULL
["menu_order"] => int(0)
["count"] => int(0)
["_links"] => object(stdClass)#89 (2) {
["self"] => array(1) {
[0] => object(stdClass)#88 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/15"
}
}
["collection"] => array(1) {
[0] => object(stdClass)#90 (1) {
["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
}
}
}
}
[1] => object(stdClass)#91 (10) {
["id"] => int(19)
["name"] => string(16) "SUB SUB SUB ZERO"
["slug"] => string(16) "sub-sub-sub-zero"
["parent"] => int(18)
["description"] => string(0) ""
["display"] => string(7) "default"
["image"] => NULL
["menu_order"] => int(0)
["count"] => int(1)
["_links"] => object(stdClass)#93 (3) {
["self"] => array(1) {
[0] => object(stdClass)#92 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/19"
}
}
["collection"] => array(1) {
[0] => object(stdClass)#94 (1) {
["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
}
}
["up"] => array(1) {
[0] => object(stdClass)#95 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/18"
}
}
}
}
[2] => object(stdClass)#96 (10) {
["id"] => int(18)
["name"] => string(12) "SUB-SUB ZERO"
["slug"] => string(12) "sub-sub-zero"
["parent"] => int(17)
["description"] => string(4) "ssss"
["display"] => string(7) "default"
["image"] => NULL
["menu_order"] => int(0)
["count"] => int(1)
["_links"] => object(stdClass)#98 (3) {
["self"] => array(1) {
[0] => object(stdClass)#97 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/18"
}
}
["collection"] => array(1) {
[0] => object(stdClass)#99 (1) {
["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
}
}
["up"] => array(1) {
[0] => object(stdClass)#100 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/17"
}
}
}
}
[3] => object(stdClass)#101 (10) {
["id"] => int(17)
["name"] => string(8) "SUB-ZERO"
["slug"] => string(8) "sub-zero"
["parent"] => int(16)
["description"] => string(3) "sub"
["display"] => string(7) "default"
["image"] => NULL
["menu_order"] => int(0)
["count"] => int(1)
["_links"] => object(stdClass)#103 (3) {
["self"] => array(1) {
[0] => object(stdClass)#102 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/17"
}
}
["collection"] => array(1) {
[0] => object(stdClass)#104 (1) {
["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
}
}
["up"] => array(1) {
[0] => object(stdClass)#105 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/16"
}
}
}
}
[4] => object(stdClass)#106 (10) {
["id"] => int(16)
["name"] => string(4) "ZERO"
["slug"] => string(8) "zeroslug"
["parent"] => int(0)
["description"] => string(11) "prova categ"
["display"] => string(7) "default"
["image"] => NULL
["menu_order"] => int(0)
["count"] => int(1)
["_links"] => object(stdClass)#108 (2) {
["self"] => array(1) {
[0] => object(stdClass)#107 (1) {
["href"] => string(68) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories/16"
}
}
["collection"] => array(1) {
[0] => object(stdClass)#109 (1) {
["href"] => string(65) "https://woocommerce.devzone.com/wp-json/wc/v3/products/categories"
}
}
}
}
}
Finally I figured out, using a recursive function:
private function _categoriaOrder($level = 0) {
echo "<ul>";
for($i = 0; $i < count($this->_mycats[$level]); $i++) {
echo "<li style='list-style-type: none;'><label style=\"margin-bottom:0;\" for=\"woo_id_categoria_{$this->_mycats[$level][$i]['id']}\"><input value=\"{$this->_mycats[$level][$i]['id']}\" id=\"woo_id_categoria_{$this->_mycats[$level][$i]['id']}\" name=\"woo_id_categoria[]\" type=\"checkbox\"> <span>" . $this->_mycats[$level][$i]['id'] . " " . $this->_mycats[$level][$i]['name'] . "</span></label>";
if(array_key_exists($this->_mycats[$level][$i]['id'], $this->_mycats)) {
$this->_categoriaOrder($this->_mycats[$level][$i]['id']);
}
echo "</li>";
}
echo "</ul>";
}
public function getCategoriaTree(){
$categorie = $this->woo->getCategoria(); //here is the object with categories from woocommerce
foreach($categorie as $k => $category){
$this->_mycats[(int)$category->parent][] = array(
'id' => $category->id,
'name' => $category->name,
);
$categories_ordered[(int)$category->parent] = $category;
}
ob_start();
$this->_categoriaOrder();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
use the function getCategoriaTree() for retrieve html.
I hope this could help someone, I spent some hours for get it.
I have two arrays $men, $sub , I want want to sort them with this condition: if($men["titre"] == $sub["menuParent"]) I put the content of the second array that contains $sub["menuParent"] in the first one.
Case:
//Array1 ($menu)
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(3) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
//Array2 ($submenu)
array(3) {
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
["submenu2"] => array(3) {
["titre"] => string(9) "SubTitle3"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "3"
}
["submenu3"] => array(3) {
["titre"] => string(9) "SubTitle1"
["menuParent"] => string(6) "Title1"
["order"] => string(1) "1"
}
}
Here for exemple in:
array1 ["menu1"]["titre"] => string(6) "Title2"
array2 ["submenu1"]["menuParent"] => string(6) "Title2"
Now while ["menu1"]["titre"] & ["submenu1"]["menuParent"] values are the same "Title2", So I push :
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
in:
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
as child like this:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
["child"] => array(3) {
[0] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
}
So I do all this for whole elements`
Here is my try, but It push all the elements in ["menu2"] instead of ["submenu1"], ["submenu2"] IN ["menu1"] and ["submenu3"] IN ["menu3"]
My Try:
/*Injection submenu to Menu*/
foreach($menu as $men) {
foreach($submenu as $sub) {
if($men["titre"] == $sub["menuParent"]){
$key = key($menu);
$menu[$key]['child'][] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
//My array_push doesn't work also
array_push($menu, array($sub["titre"], $sub["menuParent"], $sub["order"]))
}
}
}
Result:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(4) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
["child"] => array(3) {
[0] => array(3) {
[0] => string(9) "SubTitle2"
[1] => string(6) "Title2"
[2] => string(1) "2"
}
[1] => array(3) {
[0] => string(9) "SubTitle3"
[1] => string(6) "Title2"
[2] => string(1) "3"
}
[2] => array(3) {
[0] => string(9) "SubTitle1"
[1] => string(6) "Title1"
[2] => string(1) "1"
}
}
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
Where I'm at fault, If anyone can explain to me why and if I took the good way.
The problem is where your picking the key up, you can use the foreach ( as $key->$value) method to get the key of each item (foreach works on it's own copy of the array and so picking the key from the original array isn't the same as the current one). Also you could use &$men, the & allows you to alter the original array element rather than create a new entry and trying to push the new element into the array...
foreach($menu as $key => $men) {
foreach($submenu as $key1=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$menu[$key]['child'][$key1] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
gives...
Array
(
[menu1] => Array
(
[titre] => Title2
[lien] =>
[order] => 2
[child] => Array
(
[submenu1] => Array
(
[0] => SubTitle2
[1] => Title2
[2] => 2
)
[submenu2] => Array
(
[0] => SubTitle3
[1] => Title2
[2] => 3
)
)
)
[menu2] => Array
(
[titre] => Title3
[lien] => google.com
[order] => 3
)
[menu3] => Array
(
[titre] => Title1
[lien] =>
[order] => 1
[child] => Array
(
[submenu3] => Array
(
[0] => SubTitle1
[1] => Title1
[2] => 1
)
)
)
)
Update:
Code using &$men, note that in setting the data it no longer uses the key, but uses the $men variable.
foreach($menu as &$men) {
foreach($submenu as $key=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$men['child'][$key] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
unset($men); // Good practice to ensure reference is reset
How to extract this multi dimensional array from php. I am using codeigniter framework.
array(2) {
[0] => array(14)
{
["Key"] =>
string(48) "32;FaXmBQJ7/0MATwBVAFAAMAAxADIAOA==9;1261414580;"
["Type"] => string(12) "Store_Coupon"
["Code"] => string(8) "COUP0128"
["Coupon_Issuer"] => string(2) "11"
["Coupon_Reference_No"] => string(1) "1"
["Description"] => string(5) "test2"
["Price_Group"] => string(3) "ALL"
["Discount_Type"] => string(15) "Discount_Amount"
["Discount_Value"] => string(1) "0"
["Validation_Period_ID"] => string(2) "14"
["Validation_Description"] => string(15) "2016 membership"
["Starting_Date"] => string(10) "2016-01-01"
["Ending_Date"] => string(10) "2016-12-31"
["Status"] => string(8) "Disabled"
}
[1] => array(14)
{
["Key"] => string(48) "32;FaXmBQJ7/0MATwBVAFAATwBOADAAMg==9;1261413680;"
["Type"] => string(12) "Store_Coupon"
["Code"] => string(8) "COUPON02"
["Coupon_Issuer"] => string(2) "11"
["Coupon_Reference_No"] => string(1) "2"
["Description"] => string(6) "test 3"
["Price_Group"] => string(3) "ALL"
["Discount_Type"] => string(16) "Discount_Percent"
["Discount_Value"] => string(1) "0"
["Validation_Period_ID"] => string(2) "14"
["Validation_Description"] => string(15) "2016 membership"
["Starting_Date"] => string(10) "2016-01-01"
["Ending_Date"] => string(10) "2016-12-31"["Status"] => string(8) "Disabled"
}
}
I want to extract Coupon_Reference_No element. Thanks in advance
Just do a foreach and store that variable in an array. Supposing your aray is $array
$Coupon_Reference_No = array();
foreach($array as $key=>$value){
if($key == 'Coupon_Reference_No'){
$Coupon_Reference_No[] = $value;
}
}
You will get an array $Coupon_Reference_No as array("1","2")
I have a array like below (array 1) and I need to remove stdClass from it like in below array no. 2. Currently i'm doing it using a foreach loop, is there are better way to do that wthout looping?
Array no.1
array(3) {
[0] => object(stdClass)#169 (4) {
["id"] => string(2) "59"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(20) "898998989"
}
[1] => object(stdClass)#190 (4) {
["id"] => string(2) "58"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(8) "71877858"
}
[2] => object(stdClass)#193 (4) {
["id"] => string(2) "34"
["name"] => string(9) "test"
["email"] => string(22) "abc#abc.com"
["telephone"] => string(13) "3189028092139"
}
}
Array no.2
array(3) {
[0] => array(4) {
["id"] => string(2) "62"
["name"] => string(5) "test"
["email"] => string(22) "abc#abc.com"
["telephone"] => string(10) "898998989"
}
[1] => array(4) {
["id"] => string(2) "59"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(20) "71877858"
}
[2] => array(4) {
["id"] => string(2) "58"
["name"] => string(13) "test"
["email"] => string(21) "abc#abc.com"
["telephone"] => string(8) "3189028092139"
}
}
This is what I do (casting)
foreach($moderationContacts as $contact)
{
$contacts[] = (array)$contact;
}
try
$array = json_decode( json_encode($array), true);
EDIT:
I've tested this case, and it works:
$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
"a" => Array("b","c"),
"d" => $stdClass
);
$array = json_decode( json_encode($array), true);
var_dump($array);
OUTPUT
array
'a' =>
array
0 => string 'b' (length=1)
1 => string 'c' (length=1)
'd' =>
array
'test' => string 'foo' (length=3)
You can try
$array = array_map(function ($v) {
return (array) $v ; // convert to array
}, $array);
Or if this data is from json use
$array = json_decode($data,true);