php reading posted arrays - php

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"];

Related

Gwt data string from inside a nested array

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]

PHP push new data into given array

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;

combine same index of array?

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

json_encode() doesn't return results?

So I have an array named $subcats like this :
Array
(
[83] => Array
(
[0] => Array
(
[id_sous_categ] => 4
[val] => filtre a essance
)
[1] => Array
(
[id_sous_categ] => 6
[val] => filtre AIR
)
)
[89] => Array
(
[0] => Array
(
[id_sous_categ] => 8
[val] => plaquette de frein
)
[1] => Array
(
[id_sous_categ] => 9
[val] => disque de frien
)
)
)
When i'm doing : $jsonSubCats = json_encode($subcats);
The result is empty.. What am I doing wrong ?
Edit : var_dump($subcats) shows :
array(7) { [83]=> array(4) { [0]=> array(2) { ["id_sous_categ"]=> string(1) "4" ["val"]=> string(16) "filtre a essance" } [1]=> array(2) { ["id_sous_categ"]=> string(1) "6" ["val"]=> string(10) "filtre AIR" } [2]=> array(2) { ["id_sous_categ"]=> string(1) "7" ["val"]=> string(14) "filtre a huile" } [3]=> array(2) { ["id_sous_categ"]=> string(2) "14" ["val"]=> string(16) "filtre a gasoile" } } [89]=> array(2) { [0]=> array(2) { ["id_sous_categ"]=> string(1) "8" ["val"]=> string(19) "plaquette de frein " } [1]=> array(2) { ["id_sous_categ"]=> string(1) "9" ["val"]=> string(15) "disque de frien" } } [84]=> array(6) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "10" ["val"]=> string(23) "huile SAE 50 5L 5000 km" } [1]=> array(2) { ["id_sous_categ"]=> string(2) "11" ["val"]=> string(23) "huile SAE 50 1L 5000 km" } [2]=> array(2) { ["id_sous_categ"]=> string(2) "12" ["val"]=> string(22) "huile 15W40 5L 7000 km" } [3]=> array(2) { ["id_sous_categ"]=> string(2) "13" ["val"]=> string(22) "huile 15w40 1L 7000 km" } [4]=> array(2) { ["id_sous_categ"]=> string(2) "15" ["val"]=> string(22) "huile 10W40 5L10000 km" } [5]=> array(2) { ["id_sous_categ"]=> string(2) "16" ["val"]=> string(22) "huile 10W40 1L10000 km" } } [91]=> array(3) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "17" ["val"]=> string(13) "joint culasse" } [1]=> array(2) { ["id_sous_categ"]=> string(2) "18" ["val"]=> string(20) "joint cache soupape " } [2]=> array(2) { ["id_sous_categ"]=> string(2) "19" ["val"]=> string(13) "joint carter " } } [86]=> array(6) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "20" ["val"]=> string(6) "avant " } [1]=> array(2) { ["id_sous_categ"]=> string(2) "21" ["val"]=> string(7) "arrier " } [2]=> array(2) { ["id_sous_categ"]=> string(2) "22" ["val"]=> string(18) "amortisseur avant " } [3]=> array(2) { ["id_sous_categ"]=> string(2) "23" ["val"]=> string(18) "amortisseur arrier" } [4]=> array(2) { ["id_sous_categ"]=> string(2) "24" ["val"]=> string(28) "biellette suspension arriere" } [5]=> array(2) { ["id_sous_categ"]=> string(2) "25" ["val"]=> string(26) "biellette suspension avant" } } [96]=> array(1) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "26" ["val"]=> string(6) "moteur" } } [80]=> array(1) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "27" ["val"]=> string(15) "but�e embrayage" } } }
Thanks in advance.
json_encode fails to encode non-utf8 strings. Check your strings encoding. If that's the case, you have to either convert strings to utf8 or use your own implementation of json_encode (which you can quickly google by phrase "php json_encode implementation").
Make sure you have json support enabled in your server.
Run a simple <?php phpinfo() ?> in any php file and search for JSON.

How to filter array to get only the object and not array in PHP

I'm not even sure what to search for for this question. What I really want is I have an array of objects like this
array(3) {
[0]=>
object(stdClass)#423 (4) {
["name"]=>
string(3) "Blah"
["full_name"]=>
string(10) "/Blah"
["id"]=>
string(32) "BlahBlah"
["parent_id"]=>
string(32) "BlahBlah"
}
[1]=>
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah2"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
[2]=>
object(stdClass)#421 (4) {
["name"]=>
string(4) "Blah3"
["full_name"]=>
string(11) "Blah3"
["id"]=>
string(32) "BlahBlah3"
["parent_id"]=>
string(32) "BlahBlahBlah3"
}
}
I want to filter to just the object that I want so what I did was
$found_label = array_filter($labels, function($obj) use($label) {
return $obj->name === $label;
});
But then the results I got is this
array(1) {
[1]=>
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah1"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
}
But what I really want is just this
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah1"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
Then I have to do this to just get the actual object
$theKey = key($found_label);
return $found_label[$theKey];
I thought they should be a better way of doing this, also I'm new to PHP.
You can't do that with array_filter, there is no way to stop it and return only the first result. If you can't use the key to extract the result you want from the array returned from array_filter, you should use a loop. Something like this:
$label = "wantedLabel";
foreach ($labels as $l) {
if( $l->name === $label ) {
print_r ($l);
break;
}
}
This:
<?php
$labels = array(
"0" => (object) array('name' => "name1", "title" => "title1"),
"1" => (object) array('name' => "name2", "title" => "title2")
);
$label = "name1";
$found_label = array_filter($labels, function($obj) use($label) {
return $obj->name === $label;
});
print_r($found_label[0]);
Produces:
stdClass Object ( [name] => name1 [title] => title1 )

Categories