Change array key value with foreach loop - php

I have an array like
"id": "948",
"app_id": "172",
"image": "75733F51448032347-img.png",
"type": "phone",
"created_at": "2015-11-21 02:12:27",
"updated_at": "2015-11-21 02:12:27"
},
{
"id": "950",
"app_id": "172",
"image": "5813pKb1448032353-img.png",
"type": "phone",
"created_at": "2015-11-21 02:12:33",
"updated_at": "2015-11-21 02:12:33"
},
{
"id": "951",
"app_id": "172",
"image": "6864qUU1448032355-img.png",
"type": "phone",
"created_at": "2015-11-21 02:12:35",
"updated_at": "2015-11-21 02:12:35"
}
I want to change all item image value. I am using this code but not work correctly
foreach($icon as $key => $value)
{
$icon[2] = asset('uploads/apk-screen/'.$value);
unset($icon[2]);
}
Please help me to find out what’s wrong I am doing.

Finally its work
foreach($icon as $key => $value)
{
$existngValue=$icon[$key]['image'] = $value['image'];
$newVal=asset('uploads/apk-screen/'.$existngValue);
$icon[$key]['image'] = $newVal;
}

Related

Rejecting certain index parameters in an array

Given an array, in which each index there are certain key value pairs, how do I select certain key value pairs and reject the others.
$channels = Channel::get()->toArray();
This will yield the following Array:
"channels": [
{
"id": 1,
"name": "Info Release",
"slug": "info-release",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-02 01:23:50",
"updated_at": "2018-12-05 07:54:41"
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-05 05:34:50",
"updated_at": "2018-12-05 07:54:32"
},
{
"id": 12,
"name": "haha",
"slug": "haha",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-29 23:27:16",
"updated_at": "2018-12-29 23:27:16"
}
],
What is the best way to turn that array into this:
"channels": [
{
"id": 1,
"name": "Information Release",
"slug": "information-release",
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
},
{
"id": 12,
"name": "haha",
"slug": "haha",
}
],
So that if I write
$channels[0]->id
it will spit out 1
Can You try this
$viewFileds = ['id', 'name', 'slug'];
$channels = Channel::get()
->map(function ($each) use ($viewFileds) {
return Arr::only($each, $viewFileds);
});
It will return the new Collection by Only the field enabled in the $viewFileds
Hope it helps

Array in other formate

I am working on restfull api's for mobile app development. I am returning an php array in json which is like,
"data": {
"Waxing": [
{
"id": "119",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "15",
"date": "09-06-2018",
"time": "08.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "D",
"deleted_at": null,
"created_at": "2018-06-09 05:22:47",
"updated_at": "2018-06-09 14:22:04",
"setdate": "09 Jun",
"name": "Waxing"
}
],
"Massage": [
{
"id": "145",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "4",
"date": "14-06-2018",
"time": "08.00 AM,09.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "O",
"deleted_at": null,
"created_at": "2018-06-13 14:24:15",
"updated_at": "2018-06-13 14:24:15",
"setdate": "14 Jun",
"name": "Massage"
}
]
},
and creating this output with these line of code:
foreach ($getproviderdatetime as $getproviderdatetimes){
$categorycheck = Category::where('id', '=', $getproviderdatetimes->category_id)->first();
if(count($categorycheck)>0){
$alldata[$categorycheck->name][]=$getproviderdatetimes;
}
}
but now I need data in this format:
"data": {
[
"categoryname": "Waxing",
"services": [
{
"id": "119",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "15",
"date": "09-06-2018",
"time": "08.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "D",
"deleted_at": null,
"created_at": "2018-06-09 05:22:47",
"updated_at": "2018-06-09 14:22:04",
"setdate": "09 Jun",
"name": "Waxing"
}
]
],
[
"categoryname": "massage",
"services": [
{
"id": "145",
"provider_id": "54",
"provider_service_id": "0",
"category_id": "4",
"date": "14-06-2018",
"time": "08.00 AM,09.00 AM,10.00 AM,11.00 AM,12.00 PM,01.00 PM,02.00 PM,03.00 PM,04.00 PM,05.00 PM,06.00 PM,07.00 PM",
"repeat": "O",
"deleted_at": null,
"created_at": "2018-06-13 14:24:15",
"updated_at": "2018-06-13 14:24:15",
"setdate": "14 Jun",
"name": "Massage"
}
]
]
},
I have array of services and getting category name from database. I am using Laravel framework and returning data in json format. How can I achive this. Need help.
Here's the solution:
foreach ($getproviderdatetime as $getproviderdatetimes){
$categorycheck = Category::where('id', '=', $getproviderdatetimes->category_id)->first();
if(count($categorycheck)>0){
// append new item to all data:
$alldata[] = [
"categoryname" => $categorycheck->name,
"services" => [$getproviderdatetimes],
];
}
}
$testing = []
foreach ($alldata as $key=>$all){
array_push($testing,["categoryname"=>$key,"services"=>$all]);
}
print_r($testing);
Loop again ur $alldata..Bellow is ur solution
https://3v4l.org/9iT9l

PHP - Most efficient way to filter two arrays

I have a question on what is the most performant way to filter two arrays of objects. I have two arrays of products from different systems and i want to work out which products have been removed from one array and then return the products that have been removed.
See the current function i have below which i know is super slow.
public function checkRemove($externalProducts, $localProducts){
//Push all the SKU codes from feed to an array();
$arr = [];
foreach ($externalProducts->products as $product) {
if($product->StockNumber != null){
array_push($arr, $product->StockNumber);
}
}
//Loop through the local products
$productsRemove = [];
foreach ($localProducts->products as $key => $localProduct) {
if(in_array($localProduct->sku, $arr)){
}else{
array_push($productsRemove, $localProduct);
}
}
return $productsRemove;
}
$externalProducts = {
"Filter": {
"Title": "All Products"
},
"Products": [{
"Type": "Jacket",
"Price": 75,
"ExpiryDate": "2018-06-30",
"StockNumber": "180220/003",
"Created": "2018-02-20 12:24:06",
"Modified": "2018-05-30 02:00:23"
},
{
"Type": "Jeans",
"Price": 150,
"ExpiryDate": "2018-06-30",
"StockNumber": "180221/004",
"Created": "2017-08-10 15:11:44",
"Modified": "2018-05-30 02:00:22"
},
{
"Type": "Jacket",
"Price": 240,
"ExpiryDate": "2018-06-30",
"StockNumber": "150804/012",
"Created": "2015-08-04 17:03:42",
"Modified": "2018-05-30 02:00:22"
}
]
}
$internalProducts = "localProducts": [{
"title": "Fur Coat",
"id": 16526,
"created_at": "2018-05-17T10:15:45Z",
"updated_at": "2018-05-17T10:15:45Z",
"sku": "180514/001",
"price": "75.00",
"regular_price": "75.00",
"categories": [
"Jackets",
],
},
{
"title": "Ripped Jeans",
"id": 16527,
"created_at": "2018-05-17T10:15:45Z",
"updated_at": "2018-05-17T10:15:45Z",
"sku": "180221/004",
"price": "150.00",
"regular_price": "150.00",
"categories": [
"Jeans",
],
},
{
"title": "Leather Jacket",
"id": 16528,
"created_at": "2018-05-17T10:15:45Z",
"updated_at": "2018-05-17T10:15:45Z",
"sku": "150804/012",
"price": "240.00",
"regular_price": "240.00",
"categories": [
"Jackets",
],
}
]
Take a look at array_filter
You can provide a callback function which will be run for each element in the array. If the callback function returns true, the current value from the array is returned in the result array.
You still have to iterate over one array at least. It is $localProducts. So, for $localProducts there're no improvements. But you can improve $externalProducts - add a special method (if you can) that will return StockNumbers only. More effective will be if StockNumbers will be of structure as:
[
'stocknumber1' => true,
'stocknumber2' => true,
'stocknumber3' => true,
'stocknumber4' => true,
'stocknumber5' => true,
]
This will improve your search, as checking isset($StockNumbers['stocknumber4']) is faster than in_array or array_search.
If you can't change structure of $externalProducts->products, than build array of stock numbers in a loop:
public function checkRemove($externalProducts, $localProducts){
//Push all the SKU codes from feed to an array();
$arr = [];
foreach ($externalProducts->products as $product) {
if ($product->StockNumber != null){
// Again I add sku as key, not as value
$arr[$product->StockNumber] = true;
}
}
//Loop through the local products
$productsRemove = [];
foreach ($localProducts->products as $localProduct) {
// check with `isset` is faster
if (isset($arr[$localProduct->sku])) {
array_push($productsRemove, $localProduct);
}
}
return $productsRemove;
}

How to access child array key value pair in Laravel controller?

My response code :
Shpping : [
{
"id": "1",
"name": "Dress",
"deleted_at": null,
"created_at": null,
"updated_at": null,
"minicomp": [
{
"id": "1",
"cname": "basic",
"base_id": 44
},
{
"id": "2",
"cname": "Shirt",
"base_id": 177444
},
{
"id": "3",
"cname": "Pants",
"base_id": 444
}
]
}
];
I want to access base_id of of every object in child array 'minicomp' whose parent is 'Shipping'. How can I access it?
First convert your json into array using json_decode().Like this..
$json =<your json>;
$array = json_decode($json,true);
Then
echo $array['Shpping '][0]]['minicomp'][0]['id'];//outputs 1
Example:
<?php
$json = '[{
"id": "1",
"name": "Dress",
"deleted_at": null,
"created_at": null,
"updated_at": null,
"minicomp": [{
"id": "1",
"cname": "basic",
"base_id": 44
}, {
"id": "2",
"cname": "Shirt",
"base_id": 177444
}, {
"id": "3",
"cname": "Pants",
"base_id": 444
}]
}]';
$array = json_decode($json,true);
//print_r($array);
$minicomp = $array[0]['minicomp'];
echo $minicomp[0]['id'];
echo $minicomp[1]['id'];
?>
UPDATE
For getting all ids.Without defining indexes.Use foreach loop:
foreach($minicomp as $key=>$value){
echo $minicomp[$key]['id']."<br/>";
}

Error: Indirect modification of overloaded property

I have an array that has values that look like:
{
"1": {
"id": "1",
"user_id": "9",
"post_id": "61",
"parent_id": "0",
"message": "Level 0.",
"created_at": "2015-06-27 12:46:18",
"updated_at": "-0001-11-30 00:00:00",
"children": []
},
"2": {
"id": "2",
"user_id": "9",
"post_id": "61",
"parent_id": "1",
"message": "Level 1.",
"created_at": "2015-06-27 12:46:22",
"updated_at": "-0001-11-30 00:00:00",
"children": []
}
}
I then have a loop that looks like:
foreach ($tree as $key => &$value)
{
if ($value->parent_id != 0)
{
$tree[$value->parent_id]->children[] = &$value;
}
}
This, however, returns the following error:
Indirect modification of overloaded property Comment::$children has no effect
Why? And how can I fix this?

Categories