Handeling with nested multiple array in PHP (foreach) - php

I am trying to handle a situation with a nested multiple array that is received in PHP by $_POST from Javascript-Jquery (as Object not as Json)
. The nested Object looks like this:
{
"Videotheck":{
{
"Category":"Comedy",
"Title_Liste":[
{
"Title":"Millers",
"Year":"2014"
},
{
"Title":"Yogi",
"Year":"2012"
}
]
},
{
"Category":"Action",
"Title_Liste":[
{
"Title":"Rodulf",
"Year":"2014"
},
{
"Title":"Matrix",
"Year":"2000"
}
]
}
}
}
And now the information in this Object need to be splited. For example the title list of each category should be stored in a var
$comedy_title_liste = [];
$action_title_liste = [];
I tryed this:
if($_POST){
$arr1 = $_POST['Videotheck'];
foreach($arr1 as $vtk){
foreach($vtk as $data => $v){
foreach($v as $key => $value){
foreach($value as $k => $info){
echo $k.' '. $info;
}
}
}
}
}
Like this I can get only all title list from all categories, but is necessary to get for each category the list of titles separeted. I don't know really how to handle the situation.
Well this is what I have. I guest that there is something not correct.

Not 100% exact but you can give a try :
$result = array();
$parent = $_POST['Videotheck'];
foreach($parent as $key=> $child) {
$result[$child['Category']."_title_liste"] = array();
foreach($child['Title_Liste'] as $cKey => $val) {
$result[$child['Category']."_title_liste"][] = $val['Title'];
}
}

Related

Invalid argument supplied for foreach() error for array of json objects

I am decoding the array of json objects into html lists . i have tried with some demo it worked but when i deal with this array of json this gives error as : Invalid argument supplied for foreach(). what is i am missing ?
<?php
$json_string = '{"error":false,"data":[{"jb_product_category_id":"1","jb_product_category_name":"Mother","jb_product_category_prefix":"jbpm","jb_product_category_delete_status":"0","jb_product_category_created_on":"1501500876531","jb_product_category_updated_on":"1501500876531","subCategory1":[{"jb_product_subcategory1_1_id":"1","jb_product_subcategory1_2_category_id":"1","jb_product_subcategory1_3_name":"Cloths","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1501563015164","jb_product_subcategory1_6_updated_on":"1501563015164","subCategory2":[{"jb_product_subcategory2_1_id":"1","jb_product_subcategory2_2_category_id":"1","jb_product_subcategory2_3_subcategory1_id":"1","jb_product_subcategory2_4_name":"Pregnancy wear","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1501574226464","jb_product_subcategory2_7_updated_on":"1501574226464"}]}]},{"jb_product_category_id":"2","jb_product_category_name":"Child Wear","jb_product_category_prefix":"jbpc","jb_product_category_delete_status":"0","jb_product_category_created_on":"1502429483534","jb_product_category_updated_on":"1502429483534","subCategory1":[{"jb_product_subcategory1_1_id":"2","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Girls","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1502429606169","jb_product_subcategory1_6_updated_on":"1502429606169","subCategory2":[{"jb_product_subcategory2_1_id":"2","jb_product_subcategory2_2_category_id":"2","jb_product_subcategory2_3_subcategory1_id":"2","jb_product_subcategory2_4_name":"Western","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1502429794573","jb_product_subcategory2_7_updated_on":"1502429794573"}]},{"jb_product_subcategory1_1_id":"3","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Boys","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1505105190176","jb_product_subcategory1_6_updated_on":"1505105190176","subCategory2":[]}]}]}';
$array = json_decode($json_string, true);
function build_list($array) {
$list = '<ul>';
foreach($array as $key => $value) {
foreach($value as $key => $index) {
if(is_array($index)) {
$list .= build_list($index);
} else {
$list .= "<li>$index</li>";
}
}
}
$list .= '</ul>';
return $list;
}
echo build_list($array);
?>
Simply use only one foreach, the nested one seems useless :
foreach($array as $key => $index) {
if(is_array($index)) {
/* ... */
Just add a condition to check if $value is a valid array or not. This way it will not process $value if it's not an array and warnings will go away.
if (!is_array($value)) {
continue;
}
Use this condition inside foreach before the looping $array.
foreach($array as $key => $value) {
if (!is_array($value)) {
continue;
}
foreach($value as $k => $index) {
if(is_array($index)) {
$list .= build_list($index);
} else {
$list .= "<li>$index</li>";
}
}
}
Ideone link : Code

How to access associate array of array in php?

I'm from Perl but I'm beginner in PHP. I'm having following array
$rating_data = Array ("51" => Array (5,3,4,2));
I'm trying to access the each data using loop so I tried following
foreach ($keys as $rating_data)
{
foreach ($index as $rating_data[$keys])
{
echo "$index";
}
}
But the above one is not working. I have also tried the below one also,
$all_keys = array_keys($rating_data);
foreach ($keys as $all_keys)
{
foreach ($values as $all_keys)
{
echo "$values";
}
}
But I didn't get the output. It works if I hard code the keys like below:
$rating_data["51"][0];
How to fix this issue.?
You can get the key position and iter value ( sencod level ).
foreach($rating_data as $key => $values)
{
echo $key; // Output "51"
foreach($values as $value)
{
echo $value; // Output: iter1: "5", iter2: "3", iter3: "4", iter1: "2",
}
}
It should be as simple as below:
foreach($rating_data as $key => $values) {
echo $key;
foreach($values as $value) {
echo $value;
}
}
You seem to have your parameters the wrong way around in your foreach statements. I would advise a quick read through the foreach docs.

Accessing Certain JSON Elements

How would I go about accessing these elements from a Shopify JSON in PHP
JSON:
{
"orders":[
{ "note_attributes":[
{
"name":"field1",
"value":"xxxxxxxxxxxx"
},
{
"name":"field2",
"value":"xxxxxx"
},
{
"name":"field3",
"value":"xxxxxx"
}
],
This is just snippet of the object. How would I access each value and assign it to the name? Eg this $req['account_id'] would equal the value for that name tag. This is how am trying to do it but it's not working:
foreach ($order['note_attributes'] as $attributes) {
$req = array();
$req[$attributes->name] = $attributes[$attributes->value];
}
I would then like to echo $req['account_id'] and that would = xxxxxxxxx#gmail.com but it's not working any suggestions or fixes?
When iterating over Objects properties, one approach is to use (as you have) foreach
// accessing property
foreach ($objects as $object) {
echo $object->property;
}
// accessing key-value pair
foreach ($object as $key => $value) {
echo "$key => $value\n";
}
An example:
$attributes = array();
.
.
foreach($note_attributes as $note_attribute) {
$key = $note_attribute['name'];
$value = $note_attribute['value'];
$attributes[$key] = $value;
}
The structure should be in key-value pairs. ie.
attributes [
"account1234" => "abc#xxx.com",
"account_password" => "asdasdasd"
]
Hope this helps.
Update: as Roopendra mentioned, json_decode() with a TRUE flag with return an associative array, else stdClass.

Searching for duplicates in multidimensional array PHP

I have a multidimensional PHP array looking like this in JSON:
array = [{
"id":"1",
"key":"Ferrari",
"type":"car"},
{
"id":"1",
"key":"Red",
"type":"color"},
{
"id":"73",
"key":"Yellow",
"type":"color"
}]
Because it's actually a search result, I would like to, dynamically combine the results where the id's are the same. In other words the new array should look like:
array = [{
"id":"1",
"key":"Red Ferrari",
"type":"keyword"},
{
"id":"73",
"key":"Yellow",
"type":"color"
}]
I have been looking at a lot of PHP functions, but they have a limited functionality, in multidimensional arrays. Any suggestions are appreciated :)
Many Regards
Andreas
There's no base functions to do as specific thing as you mention. So you can make something like this:
print_r(my_array_merge($data));
function my_array_merge($data)
{
// sort all records by id to merge them later
$data_by_id = array();
foreach ($data as $item)
{
if (!isset($data_by_id[ $item['id'] ])) {
$data_by_id[ $item['id'] ] = array();
}
$data_by_id[ $item['id'] ][] = $item;
}
// merge records with same id
$return = array();
foreach ($data_by_id as $id => $items)
{
foreach ($items as $item)
{
if (!isset($return[ $id ])) {
$return[ $id ] = $item;
} else {
$return[ $id ]['key'] .= ' ' . $item['key'];
$return[ $id ]['type'] = 'keyword'; // I don't really get what you mean by 'keyword'
}
}
}
// reset keys before return
return array_values($return);
}

PHP Merge Similar Objects In A Multidimensional Array

I have a multidimensional array in PHP, something that looks like:
array(array(Category => Video,
Value => 10.99),
array(Category => Video,
Value => 12.99),
array(Category => Music,
Value => 9.99)
)
and what I would like to do is combine similar categories and output everything into a table, so the output would end up being:
<tr><td>Video</td><td>23.98</td></tr>
<tr><td>Music</td><td>9.99</td></tr>
Any suggestions on how to do this?
EDIT:
I can have these in two different arrays if that would be easier.
A simple loop will do:
$array = [your array];
$result = array();
foreach ($array as $a) {
if (!isset($result[$a['Category']])) {
$result[$a['Category']] = $a['Value'];
} else {
$result[$a['Category']] += $a['Value'];
}
}
foreach ($result as $k => $v) {
echo '<tr><td>' . htmlspecialchars($k) . '</td><td>' . $v . '</td></tr>';
}
$result = array();
foreach ($array as $value) {
if (isset($result[$value['Category']])) {
$result[$value['Category']] += $value['Value'];
} else {
$result[$value['Category']] = $value['Value'];
}
}
foreach ($result as $category => $value) {
print "<tr><td>$category</td><td>$value</td></tr>";
}

Categories