PHP - merge/combine array values in multidimensional array - php

How could I convert this array:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41
)
);
to this one:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41
),
1 => array(
0 => 39,
1 => 41
)
),
1 => array(
0 => array(
0 => 41,
1 => 31
),
1 => array(
0 => 41,
1 => 39
)
)
);
I tried several ways, but find no proper solution. At the moment my brain is overheated. So maybe someone has a solution for me.
Thanks #Manos.
Unfortunatly there are dynamic arrays. So these static function wont work for me.
So the array could also look like this:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41,
1 => 49,
2 => 51
),
2 => array(
0 => 73
)
);
Output should look like this:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41,
2 => 73
),
1 => array(
0 => 31,
1 => 49,
2 => 73
),
2 => array(
0 => 31,
1 => 51,
2 => 73
),
3 => array(
0 => 39,
1 => 41,
2 => 73
),
4 => array(
0 => 39,
1 => 49,
2 => 73
),
5 => array(
0 => 39,
1 => 51,
2 => 73
),
1 => array(
0 => array(
0 => 41,
1 => 31,
2 => 73
),
1 => array(
0 => 41,
1 => 39,
2 => 73
),
2 => array(
0 => 49,
1 => 31,
2 => 73
),
3 => array(
0 => 49,
1 => 39,
2 => 73
),
etc ......
)
);
Manos function Output:
array(3) {
[0]=>
array(8) {
[0]=>
array(2) {
[0]=>
int(31)
[1]=>
int(41)
}
[1]=>
array(2) {
[0]=>
int(39)
[1]=>
int(41)
}
[2]=>
array(2) {
[0]=>
int(31)
[1]=>
int(49)
}
[3]=>
array(2) {
[0]=>
int(39)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(31)
[1]=>
int(51)
}
[5]=>
array(2) {
[0]=>
int(39)
[1]=>
int(51)
}
[6]=>
array(2) {
[0]=>
int(31)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(39)
[1]=>
int(73)
}
}
[1]=>
array(9) {
[0]=>
array(2) {
[0]=>
int(41)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(49)
[1]=>
int(31)
}
[2]=>
array(2) {
[0]=>
int(51)
[1]=>
int(31)
}
[3]=>
array(2) {
[0]=>
int(41)
[1]=>
int(39)
}
[4]=>
array(2) {
[0]=>
int(49)
[1]=>
int(39)
}
[5]=>
array(2) {
[0]=>
int(51)
[1]=>
int(39)
}
[6]=>
array(2) {
[0]=>
int(41)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(49)
[1]=>
int(73)
}
[8]=>
array(2) {
[0]=>
int(51)
[1]=>
int(73)
}
}
[2]=>
array(5) {
[0]=>
array(2) {
[0]=>
int(73)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(73)
[1]=>
int(39)
}
[2]=>
array(2) {
[0]=>
int(73)
[1]=>
int(41)
}
[3]=>
array(2) {
[0]=>
int(73)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(73)
[1]=>
int(51)
}
}
}

foreach ($a as $first_group_key => $first_group) {
foreach ($a as $second_group_key => $second_group) {
if ($second_group_key == $first_group_key) {
continue;
}
$i = count($b[$first_group_key]);
foreach ($second_group as $second_value) {
foreach ($first_group as $first_key => $first_value) {
$b[$first_group_key][$i][0] = $first_value;
$b[$first_group_key][$i][1] = $second_value;
$i++;
}
}
}
}

Related

Sort Array by court and hour ascending

How can i sort this array in php so that it stays sorted by court and hour in ascending order.
The array is the following:
array(15) {
[0]=>
array(1) {
["Court 3"]=>
string(5) "19h00"
}
[1]=>
array(1) {
["Court 1"]=>
string(5) "21h00"
}
[2]=>
array(1) {
["Court 2"]=>
string(5) "21h00"
}
[3]=>
array(1) {
["Court 1"]=>
string(5) "20h00"
}
[4]=>
array(1) {
["Court 3"]=>
string(5) "18h00"
}
[5]=>
array(1) {
["Court 2"]=>
string(5) "18h00"
}
[6]=>
array(1) {
["Court 1"]=>
string(5) "16h00"
}
[7]=>
array(1) {
["Court 1"]=>
string(5) "18h00"
}
[8]=>
array(1) {
["Court 4"]=>
string(5) "18h00"
}
[9]=>
array(1) {
["Court 2"]=>
string(5) "19h00"
}
[10]=>
array(1) {
["Court 1"]=>
string(5) "19h00"
}
[11]=>
array(1) {
["Court 2"]=>
string(5) "17h00"
}
[12]=>
array(1) {
["Court 3"]=>
string(5) "17h00"
}
[13]=>
array(1) {
["Court 4"]=>
string(5) "19h00"
}
[14]=>
array(1) {
["Court 3"]=>
string(5) "20h00"
}
}
My attempt was this one:
asort($courtsHoras);
foreach($courtsHoras as $key => $value){
foreach($value as $key1 => $value1){
echo $value1;
}
}
var_export() of the array
array ( 0 => array ( 'Court 3' => '19h00', ), 1 => array ( 'Court 1' => '21h00', ), 2 => array ( 'Court 2' => '21h00', ), 3 => array ( 'Court 1' => '20h00', ), 4 => array ( 'Court 3' => '18h00', ), 5 => array ( 'Court 2' => '18h00', ), 6 => array ( 'Court 1' => '16h00', ), 7 => array ( 'Court 1' => '18h00', ), 8 => array ( 'Court 4' => '18h00', ), 9 => array ( 'Court 2' => '19h00', ), 10 => array ( 'Court 1' => '19h00', ), 11 => array ( 'Court 2' => '17h00', ), 12 => array ( 'Court 3' => '17h00', ), 13 => array ( 'Court 4' => '19h00', ), 14 => array ( 'Court 3' => '20h00', ), )
You can use strnatcmp for this purpose along with usort. We first compare the court {some_num} key and if they are not equal, we return the result accordingly.
If they are same, we return the comparison of their timestamps.
usort($data,function($a,$b){
$court_name_1 = array_keys($a)[0];
$court_name_2 = array_keys($b)[0];
$court_ordering = strnatcmp($court_name_1,$court_name_2);
if($court_ordering === 0){
return strnatcmp($a[$court_name_1],$b[$court_name_2]);
}
return $court_ordering;
});
Demo: http://sandbox.onlinephpfunctions.com/code/fe3330ceb671bac3984e062a6f784947a6f4e81e
To simplify the sorting process, you need to normalise the array keys before sorting.
For example:
$a = [
["Court 3" => "19h00"],
["Court 1" => "21h00"],
["Court 2" => "21h00"],
["Court 1" => "20h00"],
["Court 3" => "18h00"],
["Court 2" => "18h00"],
["Court 1" => "16h00"],
["Court 1" => "18h00"],
["Court 4" => "18h00"],
["Court 2" => "19h00"],
["Court 1" => "19h00"],
["Court 2" => "17h00"],
["Court 3" => "17h00"],
["Court 4" => "19h00"],
["Court 3" => "20h00"]
];
$b = [];
foreach($a as $ka => $va) {
foreach($va as $kb => $vb) {$b[$kb][] = $vb;}
}
ksort($b); // sort courts
foreach ($b as $k => $v) {sort($b[$k]);} // sort hours
var_dump($b);

Managing two different array in one loop TWIG

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 %}

woocommerce build categories tree

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.

Check if value exists in array with php

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));
}

create php array based on another array.

I want to sort all php arrays based on first main array.
This is the main array which I want to use to sort all other arrays:
Array (
[0] => 10
[1] => 14
[2] => 15
[3] => 20
[4] => 21
[5] => 24
[6] => 25
[7] => 28
[8] => 30
[9] => 45
[10] => 60
[11] => 90
[12] => 120
[13] => 150
[14] => 180
[15] => 210
[16] => 240
[17] => 270
[18] => 365
)
This are arrays which need to be sorted :
Array (
[0] => Array
(
[14] => 49.21
[20] => 71.04
[25] => 89.58
[30] => 100.00
)
[1] => Array
(
[180] => 412.00
[150] => 347.00
[120] => 285.00
[90] => 224.00
[60] => 165.00
[30] => 100.00
[14] => 47.00
)
)
I need that final result look like this:
Array (
[0] => Array
(
[10] => n/a
[14] => 49.21
[15] => n/a
[20] => 71.04
[21] => n/a
[24] => n/a
[25] => 89.58
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => n/a
[90] => n/a
[120] => n/a
[150] => n/a
[180] => n/a
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
[1] => Array
(
[10] => n/a
[14] => 71.04
[15] => n/a
[20] => n/a
[21] => n/a
[24] => n/a
[25] => n/a
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => 165.00
[90] => 224.00
[120] => 285.00
[150] => 347.00
[180] => 412.00
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
)
Thanks.
Assuming your initial array is $source, and $todo is your second array with the two subsets, then:
$keys = array_flip($todo);
$keys = array_map(function() { return 'n/a'; }, $keys); // set all values to be "n/a";
foreach($todo as $idx => $do) {
$todo[$idx] = $do + $keys; // merge without renumbering.
}
If I understand it right from your question, you want to make both arrays of the same length and set keys not in them to "n/a".
Variant with array_merge (Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
var_dump($data);
Result:
array(2) {
[0]=>
array(23) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(5) "49.21"
[20]=>
string(5) "71.04"
[21]=>
string(5) "89.58"
[22]=>
string(6) "100.00"
}
[1]=>
array(26) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(6) "412.00"
[20]=>
string(6) "347.00"
[21]=>
string(6) "285.00"
[22]=>
string(6) "224.00"
[23]=>
string(6) "165.00"
[24]=>
string(6) "100.00"
[25]=>
string(5) "47.00"
}
}
Variant with mapping function (Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
// overload $shorten array with a mapping function
$shorten = function(array $a) use ($shorten)
{
$r = array();
foreach($shorten as $i => $k)
{
$r[$k] = isset($a[$k]) ? $a[$k] : 'n/a';
}
return $r;
};
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// apply mapping function to $data
$data = array_map($shorten, $data);
var_dump($data); # result
Result:
array(2) {
[0]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "49.21"
[15]=>
string(3) "n/a"
[20]=>
string(5) "71.04"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(5) "89.58"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(3) "n/a"
[90]=>
string(3) "n/a"
[120]=>
string(3) "n/a"
[150]=>
string(3) "n/a"
[180]=>
string(3) "n/a"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
[1]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "47.00"
[15]=>
string(3) "n/a"
[20]=>
string(3) "n/a"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(3) "n/a"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(6) "165.00"
[90]=>
string(6) "224.00"
[120]=>
string(6) "285.00"
[150]=>
string(6) "347.00"
[180]=>
string(6) "412.00"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
}

Categories