Looking for a value in a multidimensional array in PHP - php

I have this kind of array:
array(1) {
[36694730] => array(2) {
["summonerId"] => int(36694730)
["pages"] => array(6) {
[0] => array(4) {
["id"] => int(35938350)
["name"] => string(10) "Fervor ADC"
["current"] => bool(true)
["masteries"] => array(10) {
[0] => array(2) { ["id"] => int(6343) ["rank"] => int(1) }
[1] => array(2) { ["id"] => int(6131) ["rank"] => int(5) }
[2] => array(2) { ["id"] => int(6122) ["rank"] => int(1) }
[3] => array(2) { ["id"] => int(6331) ["rank"] => int(5) }
[4] => array(2) { ["id"] => int(6111) ["rank"] => int(5) }
[5] => array(2) { ["id"] => int(6141) ["rank"] => int(1) }
[6] => array(2) { ["id"] => int(6312) ["rank"] => int(5) }
[7] => array(2) { ["id"] => int(6322) ["rank"] => int(1) }
[8] => array(2) { ["id"] => int(6162) ["rank"] => int(1) }
[9] => array(2) { ["id"] => int(6151) ["rank"] => int(5) }
}
}[1] => array(4) {
["id"] => int(35938366)
["name"] => string(15) "Thunderlord ADC"
["current"] => bool(false)
["masteries"] => array(10) {
[0] => array(2) { ["id"] => int(6121) ["rank"] => int(1) }
[1] => array(2) { ["id"] => int(6343) ["rank"] => int(1) }
[2] => array(2) { ["id"] => int(6131) ["rank"] => int(5) }
[3] => array(2) { ["id"] => int(6331) ["rank"] => int(5) }
[4] => array(2) { ["id"] => int(6111) ["rank"] => int(5) }
[5] => array(2) { ["id"] => int(6141) ["rank"] => int(1) }
[6] => array(2) { ["id"] => int(6312) ["rank"] => int(5) }
[7] => array(2) { ["id"] => int(6322) ["rank"] => int(1) }
[8] => array(2) { ["id"] => int(6362) ["rank"] => int(1) }
[9] => array(2) { ["id"] => int(6352) ["rank"] => int(5) }
}
}[2] => array(4) {
["id"] => int(35938367)
["name"] => string(7) "Twisted"
["current"] => bool(false)
["masteries"] => array(10) {
[0] => array(2) { ["id"] => int(6121) ["rank"] => int(1) }
[1] => array(2) { ["id"] => int(6114) ["rank"] => int(5) }
[2] => array(2) { ["id"] => int(6131) ["rank"] => int(5) }
[3] => array(2) { ["id"] => int(6343) ["rank"] => int(1) }
[4] => array(2) { ["id"] => int(6331) ["rank"] => int(5) }
[5] => array(2) { ["id"] => int(6142) ["rank"] => int(1) }
[6] => array(2) { ["id"] => int(6312) ["rank"] => int(5) }
[7] => array(2) { ["id"] => int(6322) ["rank"] => int(1) }
[8] => array(2) { ["id"] => int(6351) ["rank"] => int(5) }
[9] => array(2) { ["id"] => int(6362) ["rank"] => int(1) }
}
}[3] => array(4) {
["id"] => int(35938368)
["name"] => string(13) "CaptainJungle"
["current"] => bool(false)
["masteries"] => array(10) {
[0] => array(2) { ["id"] => int(6121) ["rank"] => int(1) }
[1] => array(2) { ["id"] => int(6343) ["rank"] => int(1) }
[2] => array(2) { ["id"] => int(6114) ["rank"] => int(5) }
[3] => array(2) { ["id"] => int(6331) ["rank"] => int(5) }
[4] => array(2) { ["id"] => int(6321) ["rank"] => int(1) }
[5] => array(2) { ["id"] => int(6141) ["rank"] => int(1) }
[6] => array(2) { ["id"] => int(6312) ["rank"] => int(5) }
[7] => array(2) { ["id"] => int(6134) ["rank"] => int(5) }
[8] => array(2) { ["id"] => int(6362) ["rank"] => int(1) }
[9] => array(2) { ["id"] => int(6352) ["rank"] => int(5) }
}
}[4] => array(4) {
["id"] => int(35938369)
["name"] => string(6) "TeeTop"
["current"] => bool(false)
["masteries"] => array(10) {
[0] => array(2) { ["id"] => int(6343) ["rank"] => int(1) }
[1] => array(2) { ["id"] => int(6131) ["rank"] => int(5) }
[2] => array(2) { ["id"] => int(6122) ["rank"] => int(1) }
[3] => array(2) { ["id"] => int(6331) ["rank"] => int(5) }
[4] => array(2) { ["id"] => int(6111) ["rank"] => int(5) }
[5] => array(2) { ["id"] => int(6312) ["rank"] => int(5) }
[6] => array(2) { ["id"] => int(6142) ["rank"] => int(1) }
[7] => array(2) { ["id"] => int(6322) ["rank"] => int(1) }
[8] => array(2) { ["id"] => int(6351) ["rank"] => int(5) }
[9] => array(2) { ["id"] => int(6362) ["rank"] => int(1) }
}
}[5] => array(4) {
["id"] => int(35938370)
["name"] => string(13) "ThreshShields"
["current"] => bool(false)
["masteries"] => array(10) {
[0] => array(2) { ["id"] => int(6223) ["rank"] => int(1) }
[1] => array(2) { ["id"] => int(6241) ["rank"] => int(1) }
[2] => array(2) { ["id"] => int(6343) ["rank"] => int(1) }
[3] => array(2) { ["id"] => int(6312) ["rank"] => int(5) }
[4] => array(2) { ["id"] => int(6322) ["rank"] => int(1) }
[5] => array(2) { ["id"] => int(6332) ["rank"] => int(5) }
[6] => array(2) { ["id"] => int(6212) ["rank"] => int(5) }
[7] => array(2) { ["id"] => int(6231) ["rank"] => int(5) }
[8] => array(2) { ["id"] => int(6363) ["rank"] => int(1) }
[9] => array(2) { ["id"] => int(6352) ["rank"] => int(5) }
}
}
}
}
}
I want to see if an item with a specific "name" key, (for example "Fervor ADC") exists in it, in order to use it for a membership verification. How can I look for it knowing that the array can have more values? The [0] to [4] here are the number of pages and it varies from 1 to 20.

//Will comes to know value exist or not in array
echo in_array_r("Fervor ADC", $array) ? 'found' : 'not found';
//function to find a value exist or not in array.
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}

Initialize a result and search term
$result = null;
$searchName = "Fervor ADC"
Loop over the values in the 'pages' key:
foreach ($your_array['36694730']['pages'] as $x) {
// if you find one that matches your search term, set your result to that one
if ($x['name'] == $searchName) {
$result = $x;
// then stop the loop; you're done.
break;
}
}
Then if you just want to verify that it exists
if ($result)
will work. And if you end up needing to do anything else with the data associated with that name, then you'll have it available in $result.

Related

Problem in printing Multi-dimentional Array

I have passed json string through ajax jquery code then i converted it into multi-dimentional associative array. now i am getting problem in printing this multi-dimentional associative array.
In php file i did this
// Retrieve the string, which was sent via the POST parameter "user"
$user = $_POST['user'];
// Decode the JSON string and convert it into a PHP associative array.
$decoded = json_decode($user,true,10);
// var_dump the array so that we can view it's structure.
var_dump($decoded);
From Ajax request
var userStr = JSON.stringify(connections);
$.ajax({
url: base_url+"ajax/add_google_user",
type: 'post',
data: {user: userStr},
success: function(response){
appendPre(response);
}
});
I am printing by the var_dump($decoded);
and i require printing with echo how i can?
array(5) {
[0] =>
array(5) {
["resourceName"] =>
string(27)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"618d80f98fe31c72"
}
}
["displayName"] =>
string(12)"Raghav verma"
["familyName"] =>
string(5)"verma"
["givenName"] =>
string(6)"Raghav"
["displayNameLastFirst"] =>
string(13)"verma, Raghav"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"618d80f98fe31c72"
}
}
["value"] =>
string(27)"raghav.verma12345#gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"618d80f98fe31c72"
}
}
["value"] =>
string(10)"9854251378"
["canonicalForm"] =>
string(13)"+919854251378"
}
}
}
[1] =>
array(5) {
["resourceName"] =>
string(27)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"69849e8b89f8c048"
}
}
["displayName"] =>
string(13)"Dinesh Chopra"
["familyName"] =>
string(6)"Chopra"
["givenName"] =>
string(6)"Dinesh"
["displayNameLastFirst"] =>
string(14)"Chopra, Dinesh"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"69849e8b89f8c048"
}
}
["value"] =>
string(28)"dinesh.chopra12345#gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"69849e8b89f8c048"
}
}
["value"] =>
string(10)"9562145678"
["canonicalForm"] =>
string(13)"+919562145678"
}
}
}
[2] =>
array(5) {
["resourceName"] =>
string(25)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(14)"43f6888c09d0a8"
}
}
["displayName"] =>
string(12)"Rahul Sharma"
["familyName"] =>
string(6)"Sharma"
["givenName"] =>
string(5)"Rahul"
["displayNameLastFirst"] =>
string(13)"Sharma, Rahul"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(14)"43f6888c09d0a8"
}
}
["value"] =>
string(28)"rahul.sharma123456#gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(14)"43f6888c09d0a8"
}
}
["value"] =>
string(11)"97854 63214"
["canonicalForm"] =>
string(13)"+919785463214"
}
}
}
[3] =>
array(5) {
["resourceName"] =>
string(27)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"1bdbb17e08cff0b6"
}
}
["displayName"] =>
string(11)"Tarun Mehta"
["familyName"] =>
string(5)"Mehta"
["givenName"] =>
string(5)"Tarun"
["displayNameLastFirst"] =>
string(12)"Mehta, Tarun"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"1bdbb17e08cff0b6"
}
}
["value"] =>
string(27)"tarun.mehta123456#gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"1bdbb17e08cff0b6"
}
}
["value"] =>
string(11)"92635 47815"
["canonicalForm"] =>
string(13)"+919263547815"
}
}
}
[4] =>
array(4) {
["resourceName"] =>
string(26)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(4) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(15)"8e3b6280a6e5da2"
}
}
["displayName"] =>
string(26)"aniltulipacademy#gmail.com"
["givenName"] =>
string(26)"aniltulipacademy#gmail.com"
["displayNameLastFirst"] =>
string(26)"aniltulipacademy#gmail.com"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(15)"8e3b6280a6e5da2"
}
}
["value"] =>
string(26)"aniltulipacademy#gmail.com"
}
}
}
}
Since you need to print a multidimensional array, you can use as many iterative statement like for you need, to dig and print all level of your array using an echo.
You can use var_export if you pass true as a second parameters you can put the content into a var.
$a = var_export([ 1 => 2 ], true);
echo $a;
If you want to use var_dumps for some obscure reasons you can use buffers to capture the output:
ob_start();
var_dump([ "a" => "1" ]);
$a = ob_get_contents();
ob_end_clean();
echo $a;

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.

Why my array does not sort correctly

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

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

How get array from multidimensional array by order

I have this kind of array:
array(2) {
[1] => array(3) {
[3] => array(3) {
["data"] => array(3) {
["id"] => string(1) "3"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[4] => array(3) {
["data"] => array(3) {
["id"] => string(1) "4"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[2] => array(3) {
["data"] => array(3) {
["id"] => string(1) "2"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
}
[2] => &array(3) {
[15] => array(3) {
["data"] => array(3) {
["id"] => string(2) "15"
["depth"] => string(1) "2"
["parent_id"] => string(1) "3"
}
}
[16] => array(3) {
["data"] => array(3) {
["id"] => string(2) "16"
["depth"] => string(1) "2"
["parent_id"] => string(1) "2"
}
}
[18] => array(3) {
["data"] => array(3) {
["id"] => string(2) "18"
["depth"] => string(1) "2"
["parent_id"] => string(1) "4"
}
}
}
}
First level means depth (1 hasn't child, 2 has parent without child, etc.). Order of array items is correct. I need obtain different array in the same order (of course in dependency of level).
array(6) {
[3] => array(3) {
["data"] => array(3) {
["id"] => string(1) "3"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[15] => array(3) {
["data"] => array(3) {
["id"] => string(1) "15"
["depth"] => string(1) "1"
["parent_id"] => 3
}
}
[4] => array(3) {
["data"] => array(3) {
["id"] => string(1) "4"
["depth"] => string(1) "1"
["parent_id"] => NULL
}
}
[18] => &array(3) {
["data"] => array(3) {
["id"] => string(2) "18"
["depth"] => string(1) "2"
["parent_id"] => string(1) "4"
}
}
[2] => array(3) {
["data"] => array(3) {
["id"] => string(2) "2"
["depth"] => string(1) "2"
["parent_id"] => NULL
}
}
[16] => array(3) {
["data"] => array(3) {
["id"] => string(2) "16"
["depth"] => string(1) "2"
["parent_id"] => string(1) "2"
}
}
}
**
First solution
**
I found solution but I do not know how make recursion from that. It works only with two levels. Third level is order badly. Variable $this->hierarchicalData contains first array.
$_sortedHierarchyData = array();
foreach ($this->hierarchicalData as $levelKey => $_level) {
foreach ($_level as $itemKey => $item) {
if (isset($this->hierarchicalData[$levelKey + 1])) {
$_sortedHierarchyData[$item['data']['id']] = $item;
$children = $this->searchChildrenFromLevelOfHieararchicalSortedData($item['data']['id'], $this->hierarchicalData[$levelKey + 1]);
$_sortedHierarchyData += $children;
}
}
}
function searchChildrenFromLevelOfHieararchicalSortedData($parent_id, $level)
{
$_children = array();
foreach ($level as $key => $item) {
if ($item['data']['parent_id'] === $parent_id) {
$_children[$key] = $item;
}
}
return $_children;
}

Categories