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
)
)
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();
How can I use PHP, to select all membership plans where 'type_id' = x and level_id = x or term_id = x? For example, I want to return all Single (type_id = 1) and Basic (level_id = 1) membership plans. I'm trying to avoid additional SQL queries, as the data is already stored in an array, it's just not easy for me to lookup the data I want.
I'm also trying to avoid a ton of foreach loops. For example, I recently tried the following which sort of works... but what if I have more types than I do levels. There has to be a better way, right?
<?php
$available_types = array();
$available_levels = array();
foreach ($membership_plans as $plan) {
$available_levels[$plan->level_id] = $plan->level_name;
$available_types[$plan->type_id] = $plan->type_name;
}
$available_types = array_unique($available_types);// Unique Types
$available_levels = array_unique($available_levels);// Unique Levels
foreach ($available_levels as $level_id => $level_name) {// foreach level
foreach ($available_types as $type_id => $type_name) {// list each type with level
echo $level_name.' - '.$type_name;
// find plans tht match type and level
foreach ($plans as $plan) {
// More code goes here, but it didn't work out
}
}
}
?>
I have a membership plan array that looks like the following:
Array
(
[0] => stdClass Object
(
[id] => 1
[type_id] => 1
[type_name] => Single
[level_id] => 1
[level_name] => Basic
[term_id] => 1
[term_name] => MTM
[name] => SB_1
[description] =>
[rate] => 58
[hidden] => 1
[sort] => 1000
[active] => 1
)
[1] => stdClass Object
(
[id] => 2
[type_id]`enter code here` => 1
[type_name] => Single
[level_id] => 1
[level_name] => Basic
[term_id] => 2
[term_name] => 12 Months
[name] => SB_12
[description] =>
[rate] => 56
[hidden] => 0
[sort] => 1110
[active] => 1
)
[2] => stdClass Object
(
[id] => 14
[type_id] => 1
[type_name] => Single
[level_id] => 3
[level_name] => Premier
[term_id] => 2
[term_name] => 12 Months
[name] => SP_12
[description] =>
[rate] => 76
[hidden] => 0
[sort] => 1310
[active] => 1
)
[3] => stdClass Object
(
[id] => 16
[type_id] => 1
[type_name] => Single
[level_id] => 3
[level_name] => Premier
[term_id] => 4
[term_name] => 28 Months
[name] => SP_28
[description] =>
[rate] => 65.43
[hidden] => 1
[sort] => 1330
[active] => 1
)
[4] => stdClass Object
(
[id] => 19
[type_id] => 2
[type_name] => Dual
[level_id] => 1
[level_name] => Basic
[term_id] => 1
[term_name] => MTM
[name] => DB_1
[description] =>
[rate] => 81
[hidden] => 1
[sort] => 2100
[active] => 1
)
)
You probably want to use array_filter
array_filter(
$arr,
function($v) {
return (
$v->type_id == 'x'
&& ($v->level_id == 'x' || $v->term_id == 'x')
)
}
)
if the x needs to be variable you have the use function($v) use ($var) {
And maybe its better to check if the type_id exists.
My PHP array ($array) is as follows:
Array
(
[channels] => Array
(
[0] => Array
(
[position] => 5
[id] => 11
[name] => AFK
)
[1] => Array
(
[position] => 1
[id] => 22
[name] => ARK
)
[2] => Array
(
[position] => 2
[id] => 33
[name] => ESO
)
[3] => Array
(
[position] => 4
[id] => 44
[name] => semi-afk
)
[4] => Array
(
[position] => 0
[id] => 55
[name] => SPACE
)
[5] => Array
(
[position] => 3
[id] => 66
[name] => Tanks & Ships
)
)
[instant_invite] =>
[id] => 123
[members] => Array
(
[0] => Array
(
[username] => Chartographer
[status] => online
[nick] => Chaz Rambone
[avatar_url] => https://cdn.discordapp.com/embed/avatars/0.png
[avatar] =>
[discriminator] => 3270
[id] => 124
)
[1] => Array
(
[username] => Chukers
[status] => online
[mute] =>
[suppress] =>
[deaf] =>
[channel_id] => 789
[game] => Array
(
[name] => The Elder Scrolls Online
)
[avatar_url] => https://cdn.discordapp.com/embed/avatars/1.png
[avatar] =>
[self_deaf] =>
[discriminator] => 9851
[self_mute] =>
[id] => 456
)
)
[name] => TEST
)
I want to sort by "position" starting at 0 and ASC.
I tried some of the examples found here but not getting it yet. Usort and a few others are not working for me. Sure it's syntax but not sure
if ($array->channels) {
usort($array->channels, function($a, $b) {
return $a->position > $b->position ? 1 : -1;
});
As was mentioned in the comments, you're trying to use object notation, but you're referencing an array. The documentation has very helpful examples.
usort($array['channels'], function($a, $b) {
return $a['position'] > $b['position'] ? 1 : -1;
});
See this fiddle example.
I have this object:
stdClass Object
(
[daily_inventoryID] => 1
[inventory_timestamp] => 2012-06-08 14:35:42
[inventory_date] => 2012-06-08
[inventory] =>
Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)
[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
...........
Let's say, we name the objects as $inv, i would like to display the value of $inv->inventory which is an array containing objects.
How would I do that using foreach or for loop?
foreach ($inv->inventory as $inventory) {
print_r($inventory);
}
In a template for a content type I am loading a node from a node reference.
It loads and if I do a print_r I get this:
stdClass Object (
[vid] => 40
[uid] => 14
[title] => Cover
[log] =>
[status] => 1
[comment] => 0
[promote] => 1
[sticky] => 0
[nid] => 40
[type] => portfolio_image_main
[language] => en
[created] => 1309382711
[changed] => 1309382711
[tnid] => 0
[translate] => 0
[revision_timestamp] => 1309382711
[revision_uid] => 14
[field_portolio_image] => Array (
[en] => Array (
[0] => Array (
[fid] => 5626
[alt] =>
[title] =>
[uid] => 14
[filename] => Cover.jpg
[uri] => public://Cover.jpg
[filemime] => image/jpeg
[filesize] => 147898
[status] => 1
[timestamp] => 1309382711
)
)
)
[name] => jojo
[picture] => 0
[data] => a:1:{s:7:"contact";i:1;}
)
and Im trying to access the single variable here:
$newImagePath1 = $newImage1->field_portfolio_image['en '][0]['filename'];
but so far nothing. Any thoughts?
please try to use below code
$keys = array_keys($arr[field_portolio_image][en]);
$arr[field_portolio_image][en][$keys][filename];
There is a helper function to access field items for the user's correct language (otherwise, you'd have to hardcode the ['en'] part).
field_get_items()
So your code would end up being something like this:
$field_instances = field_get_info('node', $newImage1, 'field_portfolio_image');
// $field_instances should now be an array.
foreach ($field_instances as $field_instance) {
print $field_instance['filepath'];
}