I've an array returned from function and it is like
this is used to print a grid , but grid comes to be blank not even a single entry
Array
(
[0] => Array
(
[0] => Array
(
[product_id] => 47
[name] => Test Product 1
[description] => tesw
[sku] => test050
[price] => 125
[quantity] => 12
[status] => 1
[created_at] => 2014-07-09 12:53:07
[updated_at] => 0000-00-00 00:00:00
)
[1] => Array
(
[product_id] => 49
[name] => Test Product 3
[description] =>
[sku] => test053
[price] => 3600
[quantity] => 56
[status] => 2
[created_at] => 2014-07-09 12:53:07
[updated_at] => 0000-00-00 00:00:00
)
[2] => Array
(
[product_id] => 50
[name] => Test Product 4
[description] => test
[sku] => test054
[price] => 5450
[quantity] => 5
[status] => 1
[created_at] => 2014-07-09 12:53:07
[updated_at] => 0000-00-00 00:00:00
)
)
)
and it stored in $n, if i do
foreach($n as $reach)
{
echo $reach['product_id']
}
it gives error .... Help ... Thanks in advance
try with this
foreach($n as $reach)
{
foreach($reach as $a)
{
echo $a['product_id']
}
}
for fetching product_id from 3d array you can use this
foreach{$n as value}
{
foreach($value as value1)
{
foreach($value1 as value2)
{
echo $a['product_id'];
}
}
}
Related
I am facing issue for creating assosications based on multiple columns. Here is my code and array :-
$getdetails = OrdersProduct::with('getattributes')->get();
And My OrdersProduct.php Model file
public function getattributes(){
return $this->hasMany('App\ProductsColor','product_id','product_id');
}
See the array below:-
Array
(
[0] => Array
(
[order_id] => 100015390
[product_id] => 1203
[product_size] => 12
[attributes] => Array
(
[0] => Array
(
[id] => 5748
[product_id] => 1203
[sku_website] => N7W84308-BLACK-10
[sku] => 8907613878595
[color] =>
[size] => 10
[price] => 2799
[stock] => 0
[ip_address] =>
[created_at] => 2018-08-07 16:15:36
[updated_at] => 2018-08-07 16:15:36
)
[1] => Array
(
[id] => 5749
[product_id] => 1203
[sku_website] => N7W84308-BLACK-12
[sku] => 8907613878601
[color] =>
[size] => 12
[price] => 2799
[stock] => 0
[ip_address] =>
[created_at] => 2018-08-07 16:15:37
[updated_at] => 2018-08-07 16:15:37
)
)
)
)
My expected output is below:-
Array
(
[0] => Array
(
[order_id] => 100015390
[product_id] => 1203
[product_size] => 12
[attributes] => Array
(
[0] => Array
(
[id] => 5749
[product_id] => 1203
[sku_website] => N7W84308-BLACK-12
[sku] => 8907613878601
[color] =>
[size] => 12
[price] => 2799
[stock] => 0
[ip_address] =>
[created_at] => 2018-08-07 16:15:37
[updated_at] => 2018-08-07 16:15:37
)
)
)
)
I want to compare with product_id and product_size in order_products table form products_color table having product_id and size. thanks
Compoships adds support for multi-columns relationships in Laravel 5's Eloquent.
It allows you to specify relationships using the following syntax:
public function b()
{
return $this->hasMany('B', ['key1', 'key2'], ['key1', 'key2']);
}
where columns have to match.
try this in your OrdersProduct model :
public function getattributes(){
return $this->hasMany('App\ProductsColor','product_id','product_id')->where('size',$this->product_size);
}
Finally done. i have add the join in getattributes. Hope it will help others in future:-
$getdetails = OrdersProduct::with(['getattributes'=>function($query){
$query->join('orders_products','orders_products.product_size','=','products_colors.size');
}])->get();
please someone will guide me to how to add the values of below conditions and then update the added value in code .
how to sum value of where code =low_order_fee and where code= goods_total then update the sum value in where code=goods_total.
Array
(
[0] => Array
(
[order_total_id] => 999
[order_id] => 194
[code] => goods_total
[title] => Goods-Total
[text] => £130.00
[value] => 130.0000
[sort_order] => 1
)
[1] => Array
(
[order_total_id] => 1000
[order_id] => 194
[code] => low_order_fee
[title] => * Carriage
[text] => £10.00
[value] => 10.0000
[sort_order] => 3
)
[2] => Array
(
[order_total_id] => 1001
[order_id] => 194
[code] => sub_total
[title] => Sub-Total
[text] => £130.00
[value] => 130
[sort_order] => 4
)
[3] => Array
(
[order_total_id] => 1002
[order_id] => 194
[code] => tax
[title] => VAT (20%)
[text] => £26.00
[value] => 26.0000
[sort_order] => 5
)
[4] => Array
(
[order_total_id] => 1003
[order_id] => 194
[code] => total
[title] => Invoice Total
[text] => £166.00
[value] => 166.0000
[sort_order] => 9
)
)
A rudimentary solution:
foreach ($array_var as $key => $item) {
if ($item['code'] == 'low_order_fee') {
$first_val = $item['value'];
}
if ($item['goods_total'] == 'low_order_fee') {
$sec_val = $item['value'];
$position = $key;
}
}
$array_var[$position]['goods_total'] = $first_val + $sec_val;
But maybe you should think about store the values in another way to make easier access to them.
I hope it helps.
I have a long array from which I wish to fetch all the values and store it in a separate variable, and store each value in database.
The array that I have is:
Array
(
[success] => 1
[categories] => Array
(
[0] => Array
(
[category_id] => 39
[name] => BAGS
[categories] => Array
(
[0] => Array
(
[category_id] => 59
[name] => Handcrafted Purses
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 45
[parent_id] => 39
[name] => Laptop Bag
[categories] =>
[status] => 1
)
)
[status] => 1
)
[1] => Array
(
[category_id] => 40
[name] => BOXERS
[categories] => Array
(
[0] => Array
(
[category_id] => 56
[parent_id] => 40
[name] => Women Boxers
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 91
[parent_id] => 0
[name] => Business Corporate
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 92
[parent_id] => 91
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 93
[parent_id] => 92
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 60
[parent_id] => 0
[name] => Business Corporates
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 90
[parent_id] => 60
[name] => Art Cushions
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 67
[parent_id] => 60
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 77
[parent_id] => 67
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 76
[parent_id] => 67
[name] => Smart Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 86
[parent_id] => 60
[name] => Fashion Jewellery
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 88
[parent_id] => 86
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 61
[parent_id] => 60
[name] => Men Footwear
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 65
[parent_id] => 61
[name] => Canvas Loafers
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 87
[parent_id] => 60
[name] => Shawls And Stoles
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 89
[parent_id] => 87
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 15
[parent_id] => 0
[name] => ETHNIC WEAR
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 28
[parent_id] => 15
[name] => Designer Lehngas
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 2
[parent_id] => 15
[name] => Suits
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
)
I am able to fetch the outer values of this array by using this code:
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
}
}
I got values as:
39
40
91
60
15
16
38
57
But I also wish to access the inner most values of the array. Can anyone tell how I can create a nested loop?
If you now how deep your array is you can just simply add other foreach-loops within your main loop.
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
if(isset($category['categories'])){
foreach($category['categories'] as $category2)
{
echo $category2['category_id'];
echo "<br>";
if (isset($category2['categories'])){
foreach($category2['categories'] as $category3)
{
echo $category3['category_id'];
echo "<br>";
...
} }
}}
}
}
Just to learn more techniques in PHP , I'm so used in loop foreach and I want to find some other ways on how to check an array object. Let's say if we have a result something like this:
Array
(
[0] =; stdClass Object
(
[gallery_id] => 38
[artist_id] => 58
[title] => Appearances
[description] => Appearances
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 03:27:23
[update_timestamp] => 2014-08-07 15:26:24
[url] => talents/58/photos/38/Appearances
[position] =>
)
[1] => stdClass Object
(
[gallery_id] => 36
[artist_id] => 58
[title] => Endorsements
[description] => Endorsements
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 03:17:28
[update_timestamp] => 2015-01-13 15:50:09
[url] => talents/58/photos/36/Endorsements
[position] => 1
)
[2] => stdClass Object
(
[gallery_id] => 34
[artist_id] => 58
[title] => Magazine Covers
[description] => Magazine Covers
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 02:54:27
[update_timestamp] => 2015-01-13 15:50:10
[url] => talents/58/photos/34/Magazine-Covers
[position] => 2
)
)
I know we can do something like this to check a value in the object:
$myFlag = 2;
foreach($objArr as $obj){
if($obj->position == $myFlag){
return true;
}
}
But is there a way to check it without using a loop? A more efficient way, coz I feel whenever I use this(most of the time) , It's not that efficient and somehow takes time to process.
How about using array_filter with closure.
$myFlag = 2;
$target = array_filter($objArr, function($elem) use($myFlag){
return $elem->position === $myFlag;
});
print_r($target);
result is ...
Array
(
[2] => stdClass Object
(
[gallery_id] => 34
[artist_id] => 58
[title] => Magazine Covers
[description] => Magazine Covers
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 02:54:27
[update_timestamp] => 2015-01-13 15:50:10
[url] => talents/58/photos/34/magazine-covers
[position] => 2
)
)
I am using PHP 5.5.2 with MySQL back-end. I have a self-referencing database table from that I am getting an array as below :
Array
(
[0] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => Aaa
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 48
[s_id] => 37
[m_id] =>
[title] => bbbbbbbbbbbbb
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 49
[s_id] => 38
[m_id] =>
[title] => cccccccccc
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 50
[s_id] => 39
[m_id] =>
[title] => ddddddddd dddd
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[2] => Array
(
[id] => 51
[s_id] => 40
[m_id] =>
[title] => eeeeeee eeeeeee.
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 54
[s_id] => 18
[m_id] =>
[title] => fffffffff ffff
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 55
[s_id] => 19
[m_id] =>
[title] => gggggg gggggg
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 56
[s_id] => 20
[m_id] =>
[title] => hhhhhhhhhh hhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[2] => Array
(
[id] => 57
[s_id] => 21
[m_id] =>
[title] => iiiiiiii iiii
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
[0] => Array
(
[id] => 58
[s_id] => 22
[m_id] =>
[title] => jjjjjjj jjj
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 59
[s_id] => 23
[m_id] =>
[title] => kkkkkkkk kkkk
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => xxxx xxx xxx
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 112
[s_id] => 37
[m_id] =>
[title] => lllllllllll llllll
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 113
[s_id] => 38
[m_id] =>
[title] => mmmmmmm mmmm
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 123
[s_id] => 39
[m_id] =>
[title] => nnnnnnnnn hhhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 456
[s_id] => 18
[m_id] =>
[title] => ppppppp pppppp
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 545
[s_id] => 19
[m_id] =>
[title] => qqqqqqqqq qqq
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 435
[s_id] => 20
[m_id] =>
[title] => ssssssssss sssssssssss
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
)
)
)
There might be N nesting levels in this array.
Now for each array element, if ['type'] is G then I want to remove keys [s_id] and [m_id] and for any other type I want to remove keys [start], [end] and [time].
Edit : I have tried using a recursive function but it is not giving the desired result.
private function getarray(array &$arr){
foreach ($arr as $item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
foreach ($item["children"] as $c)
{
$child = array();
getarray($child);
}
}
}
How can I achieve this?
Thanks.
function filterMyArray($arr){
if(isset($arr['type'] && $arr['type'] == 'G'){
unset($arr['s_id']);
unset($arr['m_id']);
}
else if(isset($arr['type']){
unset($arr['start']);
unset($arr['end']);
unset($arr['time']);
}
if(isset($arr['children'])
$arr['children'] = filterMyArray($arr['children']);
return $arr;
}
This function is not tested, I wrote it from memory.
But seriously, Googling something like that doesn't hurt. It would take you 5 minutes to get all the required knowledge to do this...
Resolved it myself :
private function getarray(array &$arr){
foreach ($arr as &$item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
if (is_array($item['children']) && sizeof($item['children']) > 0){
getarray($item['children']);
}
}
}
And this is working fine. I have posted this answer to help other people who are novice to php and trying to overcome their problem.
And this is a slap to the jealous people who did not helped me but asked "What did you tried so long?" with a superiority complex attitude. I did it myself.