I have an array with a lot of nested array data.
I need to get:
array(4) { ["id"]=> int(90)
this number, the 90.
Please see the whole array data below.
Thank you very much for help.
array(2)
{ ["_sft_product_cat"]=>array(5)
{
["name"]=> string(18)
"Produkt-Kategorien"
["singular_name"]=> string(9)
"Kategorie"
["all_items_label"]=> string(15)
"Alle Kategorien"
["type"]=> string(8)
"taxonomy"
["active_terms"]=> array(1)
{ [0]=> array(4)
{ ["id"]
=> int(90) ["name"]
=> string(7) "Hyundai" ["value"]
=> string(7) "hyundai" ["count"]
=> int(0)
}
}
}
["_sft_pa_typ"]=> array(5)
{ ["name"]
=> string(18) "Produkt Typ/Modell" ["singular_name"]
=> string(10) "Typ/Modell" ["all_items_label"]
=> string(5) "Al le " ["type"]
=> string(8) "taxonomy" ["active_terms"]
=> array(1)
{ [0]=> array(4)
{ ["id"]
=> int(9040) ["name"]
=> string(8) "Coupe GK" ["value"]
=> string(8) "coupe-gk" ["count"]=> int(0) } } } }
You're looking for:
$whatever_your_var_name_is["_sft_product_cat"]["active_terms"][0]["id"]
That is the same as referencing the levels one by one...
$sft_product_cat = $whatever_your_var_name_is['_sft_product_cat'];
$active_terms = $sft_product_cat['active_terms'];
$first_terms = $active_terms[0];
$id = $first_terms['id'];
It's notable that active_terms has a numeric indexed array (the [0] part). It's a hint that other instances of '_sft_product_cat' might have more than 1 terms and you'll need a loop to get them all.
I'm not familiar with php, but firstly I would format and indent the code to get a better picture of what you need from the nested objects:
array(2) {
["_sft_product_cat"]=> array(5)
{
["name"]=> string(18) "Produkt-Kategorien"
["singular_name"]=> string(9) "Kategorie"
["all_items_label"]=> string(15) "Alle Kategorien"
["type"]=> string(8) "taxonomy"
["active_terms"]=> array(1)
{
[0]=> array(4)
{
["id"]=> int(90)
["name"]=> string(7) "Hyundai"
["value"]=> string(7) "hyundai"
["count"]=> int(0)
}
}
}
["_sft_pa_typ"]=> array(5)
{
["name"]=> string(18) "Produkt Typ/Modell"
["singular_name"]=> string(10) "Typ/Modell"
["all_items_label"]=> string(5) "Alle "
["type"]=> string(8) "taxonomy"
["active_terms"]=> array(1)
{
[0]=> array(4)
{
["id"]=> int(9040) ["name"]=> string(8) "Coupe GK"
["value"]=> string(8) "coupe-gk" ["count"]=> int(0)
}
}
}
}
This would lead me to believe that you're after the id at:
<object>["_sft_product_cat"]["active_terms"][0]["id]
or
<object>[0][4][0][0]
Related
I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}
Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5
I may seem having difficulty in understanding on how array works. So result, I can't work on the issue.
foreach($results as $key => $value){
$product_key = array(
'key' => $key
);
array_push($results, $product_key);
}
var_dump($results); exit;
Expected Output
array(2) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
["key"]=>
int(0)
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
["key"]=>
int(1)
}
Unexpected Output
array(4) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
}
[2]=>
array(1) {
["key"]=>
int(0)
}
[3]=>
array(1) {
["key"]=>
int(1)
}
}
You push new value (which is array) to the end of existsing array, what do you expect then?
If you want to modify current interated array value use this approach:
foreach($results as $key => $value) {
// use `->` as `$value` is object
$value->key = $key;
}
var_dump($results); exit;
I have following $_POST array
array(5) {
["addcatagory"]=>
string(8) "CATEGORY"
["reg_admin_id"]=>
string(2) "25"
["subcatagory"]=>
array(2) {
[0]=>
string(9) "SUB CAT 1"
[1]=>
string(9) "sub cat 2"
}
["subCat_Detais"]=>
array(2) {
[0]=>
string(9) "AAAAAAAAA"
[1]=>
string(8) "BBBBBBBB"
}
["submit"]=>
string(15) "Submit Catagory"
}
and
array(1) {
["subCatFile1"]=>
array(5) {
["name"]=>
array(3) {
[0]=>
string(5) "2.jpg"
[1]=>
string(5) "3.jpg"
[2]=>
string(0) ""
}
["type"]=>
array(3) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
[2]=>
string(0) ""
}
["tmp_name"]=>
array(3) {
[0]=>
string(18) "/var/tmp/phpN5ENy2"
[1]=>
string(18) "/var/tmp/phpRyJdcc"
[2]=>
string(0) ""
}
["error"]=>
array(3) {
[0]=>
int(0)
[1]=>
int(0)
[2]=>
int(4)
}
["size"]=>
array(3) {
[0]=>
int(65101)
[1]=>
int(49550)
[2]=>
int(0)
}
}
}
now what i want to achieve is combine 0 index of subcatagory and subcat_details in one array and of 1 index of subcatagory and subcat_details in second array and so on...
how can i achieve this?? is it even possible??
Expectations
array( 'name' => 'SUB CAT 1',
'details' => 'AAAAAAAAA',
'image_name'=>'2.jpg'
);
array( 'name' => 'SUB CAT 2',
'details' => 'BBBBBBB',
'image_name'=>'2.jpg'
);
This can be done with a simple foreach() loop -
$newArray = [];
foreach($_POST["subcatagory"] as $key => $value) {
$newArray[] = array("name" => $_POST["subcatagory"][$key],
"details" => $_POST["subCat_Detais"][$key]);
}
As #CharlotteDunois mentioned, you could also use an for() loop, as long as you have sequential keys, with no keys missing -
$newArray = [];
for($i=0;$i<count($_POST["subcatagory"]);$i++) {
$newArray[] = array("name" => $_POST["subcatagory"][$i],
"details" => $_POST["subCat_Detais"][$i]);
}
I have the following:
[6199]=>
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
[6192]=>
array(11) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Java"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "java"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
and my expected output is:
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba".
[1]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars",
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
[2]=>
string(9) "Java"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
[3]=>
string(9) "java"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
See how it eliminates dupes and merges each array maintaining the "origin" key.
I've tried :
foreach ($resultterms as $keyname => $valuename){
foreach ($valuename as $keysub => $valuesub) {
foreach($valuesub['name'] as $keysubsub => $valuesubsub){
# code...
$prods_atts[$keysub]['name'][$keysubsub] = $valuesubsub;
$prods_atts[$keysub]['slug'][$keysubsub] = $valuesub['slug'][$keysubsub];
$prods_atts[$keysub]['id'][$keysubsub] = $valuesub['id'][$keysubsub];
}
}
}
where $resultterms is the original arrays but it's not working. I was wondering if there was a wonderful php function I could use to merge these instead of so many nested for each loops?
I believe you're just looking for array_merge_recursive.
call_user_func_array('array_merge_recursive', array_values($prod_atts));
call_user_func_array allows to transform an array into a list of arguments
array_values because in the end, you seem to want to get rid of the first layer of your array
In order to try it, could you post the var_export of your variable instead of the var_dump?
echo(var_export($prod_atts, true));
merge your array by any suggested method. After that you will get duplicated values. And you need save only the unique items
$new = array_merge_recursive($resultterms['6199'], $resultterms['6192']);
foreach($new['Origin'] as &$item) { $item = array_unique($item); }
foreach($new['Filler'] as &$item) { $item = array_unique($item); }
This is probably a simple question but i have tried for hours without getting anything out:
I'm customizing an opensource WordPress Plugin. When I post on my customized PHP file and var_dump() the $_POST variables I get the following:
array(7) {
["hellomail"] => array(3) {
["email"] => array(7) {
["subject"] => string(4) "asda"
["from_name"] => string(14) "Myplugin"
["from_email"] => string(12) "myplugin#myplugin.com"
["replyto_name"] => string(14) "Test"
["replyto_email"] => string(12) "myplugin#myplugin.com"
["params"] => array(1) {
["schedule"] => array(2) {
["day"] => string(10) "2013/03/21"
["time"] => string(8) "00:00:00"
}
}
["email_id"] => string(2) "25"
}
["campaign_list"] => array(1) {
["list_id"] => array(1) {
[0] => string(1) "4"
}
}
["campaign"] => array(1) {
["campaign_id"] => string(2) "24"
}
}
["receiver-preview"] => string(10) "myplugin#myplugin.com"
["_wpnonce"] => string(10) "999938595d"
["_wp_http_referer"] => string(66) "/wp-admin/admin.php?page=testpage&action=editDetails&id=25"
["action"] => string(8) "savelast"
["roll_redir"] => string(0) ""
["submit-send"] => string(6) "Senden"
}
What I need is the ["campaign_id"] and the ["list_id"]. I really have no idea how to get these values, is there an easy way to access them?
world of php =)
echo $_POST["hellomail"]["campaign_list"]["list_id"]; // to get array
echo $_POST["hellomail"]["campaign_list"]["list_id"][0]; // to get first
echo $_POST["hellomail"]["campaign"]["campaign_id"];
This is the way the array looks:
array(7) {
["hellomail"]=> array(3) {
["email"]=> array(7) {
["subject"]=> string(4) "asda"
["from_name"]=> string(14) "Myplugin"
["from_email"]=> string(12) "myplugin#myplugin.com"
["replyto_name"]=> string(14) "Test"
["replyto_email"]=> string(12) "myplugin#myplugin.com"
["params"]=> array(1) {
["schedule"]=> array(2) {
["day"]=> string(10) "2013/03/21"
["time"]=> string(8) "00:00:00"
}
}
["email_id"]=> string(2) "25"
}
["campaign_list"]=> array(1) {
["list_id"]=> array(1) {
[0]=> string(1) "4"
}
}
["campaign"]=> array(1) {
["campaign_id"]=> string(2) "24"
}
}
["receiver-preview"]=> string(10) "myplugin#myplugin.com"
["_wpnonce"]=> string(10) "999938595d"
["_wp_http_referer"]=> string(66) "/wp-admin/admin.php?page=testpage&action=editDetails&id=25"
["action"]=> string(8) "savelast"
["roll_redir"]=> string(0) ""
["submit-send"]=> string(6) "Senden"
}
Try the below snippet to get at the particular array elements your looking for.
echo $_POST["hellomail"]["campaign_list"]["list_id"][0]."<br/>"
.$_POST["hellomail"]["campaign"]["campaign_id"];