How to parse array elements? - php

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

Related

Get the values of an associaive array [PHP]

How can i get the values of the array bellow?
Array
(
[list_items] => Array
(
[0] => Array
(
[productName] => BUGGY INFANTIL PRO 125CC
[productId] => 1
[productColor] => Azul
[productQuantity] => 2
[productIMG] => http://localhost/kasi_src/img/vehicules/buggygasolina/BUGGY-INFANTIL-125CC-AZUL.png
[productURL] => http://localhost/kasi_src/125CCInfantil.html
[productPrice] => 1000
)
[1] => Array
(
[productName] => PATINETE 24V
[productId] => 2
[productColor] => Azul
[productQuantity] => 2
[productIMG] => http://localhost/kasi_src/img/vehicules/patinetes/24.png
[productURL] => http://localhost/kasi_src/patinete-24v.html
[productPrice] => 240
)
)
)
I tried a simple foreach loop
foreach($items as $key => $value){
echo $value . "\n";
}
but i ended up getting this error : Notice: Array to string conversion in .....
Any ideas ?
You cannot echo a array.
foreach($array["list_items"] as $list_item){
echo $list_item["productName"];
}
Use var_export($obj,true) if you want to get a textual representation of the full content of each array associated to a key in an associative array as in the example, or simply access the keys of each subarray to retrieve individual values as productName.
<?php
$list_items = array(
0 => array("productName" => "Marshmallows"),
1 => array("productName" => "Mikado"),
);
foreach($list_items as $key=>$obj){
echo var_export($obj,true)."<br/>";
// Or access each array key, for example: echo $obj['productName'];
}
?>
Output:
array ( 'productName' => 'Marshmallows', )
array ( 'productName' => 'Mikado', )

Iterating POST data with PHP gives me stupid results

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
}

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

Intersection of two arrays using common key value for comparison

I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);

Looping through array contain 3 classes

I am having trouble getting my head around how to loop through stdClasses.
Printing the array gives me the following:
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1003
[entity_id] => 0
[redirect_url] => http://yahoo.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1002
[entity_id] => 0
[redirect_url] => http://google.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1001
[entity_id] => 0
[redirect_url] => http://bing.com
[type] => Image
)
)
[total_count] => 1
I am trying to loop though with the following and print out a value (id) but I keep getting nothing.
foreach ($piggies_array as $key => $value) {
echo $piggies_array[$key]['id'];
}
foreach ($piggies_array as $key => $value) {
if (is_array($value)){
echo $value[0]->id;
}
}
I think you need:
for ($i = 0; $i < count($piggies_array); $i++) {
echo $piggies_array[$i]['piggyback'][0]->id;
}
...assuming we can only see part of your output ;)
try replace it to echo $value->id;

Categories