Iterating POST data with PHP gives me stupid results - php

So I have this multi-dimensional $_POST array in PHP:
Array
(
[addonGroupName] => Testname
[addons] => Array
(
[1534766970prkszomwvb] => Array
(
['title'] => Addon Title
['products'] => Array
(
[0] => Array
(
['code'] => 1
['title'] => test product
['price'] =>
['active'] => on
)
)
)
)
)
Strangely I cant seem to access the value parts of the array, see what I mean:
if(count($_POST['addons']) != 0)
{
foreach($_POST['addons'] as $key => $addon)
{
echo $key;
//prints 1534766970prkszomwvb
print_r($addon);
// prints
Array
(
['title'] => beta
['products'] => Array
(
[0] => Array
(
['code'] =>
['title'] =>
['deliveryPrice'] =>
['pickupPrice'] =>
['active'] => on
)
)
)
echo $addon['title'];
// prints ""
}
}
Now I'm not new to programming but this has me pulling my hair out for a solid 2 hours now.
Is there something that I am missing or is there a limitation to the depth and accessibility of multi-dimensional arrays?

If this is the array:
Array
(
[addonGroupName] => Testname
[addons] => Array
(
[1534766970prkszomwvb] => Array
(
['title'] => Addon Title
['products'] => Array
(
[0] => Array
(
['code'] => 1
['title'] => test product
['price'] =>
['active'] => on
)
)
)
)
)
then the code you used:
foreach($_POST['addons'] as $key => $addon)
{
print_r($addon);
Should actually output:
[1534766970prkszomwvb] => Array
(
['title'] => Addon Title
['products'] => Array
(
[0] => Array
(
['code'] => 1
['title'] => test product
['price'] =>
['active'] => on
)
)
)
Which means there is another array in between: 1534766970prkszomwvb
foreach($_POST['addons'] as $key => $addon){
foreach($addon as $sub){
print_r($sub); // should give you the output in question.
echo $sub["title"]; // Addon Title
}
}
If you want to output the products array you can use array_column to get the products array and loop that (if there is more than one item).
You can use array_column to get the "products" array.
$products = array_column($_POST, "products");
foreach($products as $prod){
echo $prod["title"]; //test product
echo $prod["code"]; //1
}

Related

Find Key By Value In Array Inside Array Using Php

Hey Dear I am Making Referal System With 11 Level Of Referal So Ho Can I Show All Referals And There Level I Have Tried
Array
(
[L1] => Array
(
[0] => TL422632
[1] => TL626461
)
[L2] => Array
(
[0] => TL4321
[1] => TL191123
)
[L3] => Array
(
[0] => TL555938
)
[L4] => Array
(
[0] => TL197752
)
[L5] => Array
(
[0] => TL835309
)
[L6] => Array
(
[0] => TL495903
)
[L7] => Array
(
[0] => TL207447
)
[L8] => Array
(
[0] => TL427427
)
[L9] => Array
(
[0] => TL288884
)
[L10] => Array
(
[0] => TL251399
)
[L11] => Array
(
[0] => TL284394
)
)
But I Don't Know Ho To Get L Number By Value For Example I Have TL284394 And I Want To Know Level So It Will Show L11 How Can I Do It
you can use two foreach loop inside each other and array_search() like this:
$a = array("L1" => [ 0 => "TL422632" ],"L2"=> [ 0 => "TL422635" ]);
$x = "TL422635";
foreach($a as $key=>$value){
if($x === $value){
return $key;
}else{
foreach($value as $k=>$v){
if($x === $v){
return array_search([$k=>$v],$a);
}
}
}
}

Bulid for loop based on given array

I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā€¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}

How to parse array elements?

I tried to parse a string array using the code below but the required data never printed! could any one tell me how to fix it ?Thanks
$data Array structure :
Array
(
[js] => Array
(
[total_items] => 20
[max_page_items] => 2
[selected_item] => 0
[cur_page] => 0
[data] => Array
(
[0] => Array
(
[tmp] => 1
[name] => mango
[abc] => abcd4 http://mysite/items/1234
[number] => 1123
[itemCategory_title] => fruits
[logo] => 2123.png
[itemCategory_id] => 90
)
[1] => Array
(
[tmp] => 0
[name] => cherry
[abc] => abcd4 http://mysite/items/1235
[number] => 1124
[itemCategory_title] => fruits
[logo] => 2124.png
[itemCategory_id] =>
)
)
)
[text] => no error
)
php code:
<?
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2);
foreach( $data as $item ) {
echo $item['tmp'];
echo $item['name'];
echo $item['abc'];
echo $item['number'];
echo $item['itemCategory_title'];
echo $item['log'];
echo $item['itemCategory_id'];
}
?>
It should be:
foreach ($data['js']['data'] AS $item)
because the array is nested several levels down in $data.
Note that you need to call json_decode($code2, true) to get an associative array like that. By default, it returns an object, not an array, so you would do:
foreach ($data->js->data as $item) {
echo $item->tmp;
echo $item->name;
...
}

How to get all meta value of a post type

I made a code to get all meta data of a particular post type cef_donor
$arg = array(
'post_type' => 'cef_donor',
);
$posts_array = get_posts($arg);
$metaData = array();
foreach($posts_array as $key => $value){
$metaData[] = get_post_meta($value->ID,'', true);
}
When I print $metaData I get array like this
Array
(
[0] => Array
(
[meta_key_one] => Array (
[0] => value one
)
[meta_key_two] => Array
(
[0] => value two
)
[meta_key_three] => Array
(
[0] => value three
)
)
[1] => Array
(
[meta_key_one] => Array (
[0] => another value one
)
[meta_key_two] => Array
(
[0] => another value two
)
[meta_key_three] => Array
(
[0] => another value three
)
)
)
But I just need an array like this
Array
(
[0] => Array
(
[meta_key_one] => value one
[meta_key_two] => value two
[meta_key_three] => value three
)
[1] => Array
(
[meta_key_one] => another value one
[meta_key_two] => another value two
[meta_key_three] => another value three
)
)
Is there any other easy way to get all meta_key and its meta_value of a post type.
Please help
You can just create a new one. Loop the old value to the new one. Consider this example:
$metaData = array( array( 'meta_key_one' => array('value_one'), 'meta_key_two' => array('value_two'), 'meta_key_three' => array('value_three'), ), array( 'meta_key_one' => array('another_value_one'), 'meta_key_two' => array('another_value_two'), 'meta_key_three' => array('another_value_three'), ),);
$new_values = array();
foreach($metaData as $key => $value) {
foreach($value as $index => $element) {
$new_values[$key][$index] = $element[0];
}
}
print_r($new_values);
Sample Output:
Array
(
[0] => Array
(
[meta_key_one] => value_one
[meta_key_two] => value_two
[meta_key_three] => value_three
)
[1] => Array
(
[meta_key_one] => another_value_one
[meta_key_two] => another_value_two
[meta_key_three] => another_value_three
)
)
Sample Fiddle

Get a part from the array

The following code is converting XML to array. I want to get a sting by the value of [name]. So for example: $..[offerid].. should give 8b5f7fd3806a42ccb0ade9f8309c5587
Who can help me?? :)
$xmldata = file_get_contents($XMLURL);
$arr= xml2ary($xmldata);
foreach($arr['m4n']['_c']['data']['_c']['record'] as $result){
echo "<pre>"; print_r($result);
}
The echo result is:
Array
(
[recordHash] => Array
(
[_v] => -652572603
)
[column] => Array
(
[0] => Array
(
[_a] => Array
(
[name] => url
)
[_v] => http://ad.dd.com/ppc/?20910868C187884459&zpar6=DF_1&ULP=[[http%3A%2F%2F]]
)
[1] => Array
(
[_a] => Array
(
[name] => title
)
[_v] => This is the title
)
[2] => Array
(
[_a] => Array
(
[name] => description
)
[_v] => Aanbod
)
[3] => Array
(
[_a] => Array
(
[name] => offerid
)
[_v] => 8b5f7fd3806a42ccb0ade9f8309c5587
)
)
)
Try looping through all of the items using a php foreach loop.
$name_to_find = 'offerid';
$value = FALSE;
foreach ($result['column'] as $item) {
if ($item['_a']['name'] == $name_to_find) {
$value = $item['_v'];
break;
}
}
// Now $value has the value of the entry with the name $name_to_find
If you end up finding an entry with the given $name_to_find, then $value will not be FALSE (granted that, you don't have any FALSE values :D).

Categories