So I have PHP array called 'tags', which is being filled with objects that I get from the model. And I need to push one more element into this array, which should be an empty string. For now my code looks as follows:
$tags = [];
$tags = compact(WebsiteTag::all());
array_push($tags, '');
return $tags;
And as a result I get an array where the only element is this empty string. The same happens if I try to push some actual string -- just this string remains in the array. I've tried also creating an array like:
$tags = [];
array_push($tags, compact(WebsiteTag::all()), '');
return $tags;
And
array_push($tags, WebsiteTag::all(), '');
And also
$tags = array(WebsiteTag::all(), '');
But in these cases I get an array of two elements, where the first one is an array with objects and the second one is my empty string.
Is it possible to create an array of different type elements? And if it is, how should I push them correctly?
I cannot know for sure what WebsiteTag::all() returns but I think the result is an array of tags. So what I would do is one of these two options:
$tags = WebsiteTags::all();
$tags[] = '';
// Or
$tags = array_merge(WebsiteTags::all(), array(''));
If WebsiteTag::all() returns an array you could simply merge them together:
$tags = array_merge(WebsiteTag::all(), ['']);
See the documentation of array_merge() for details
Well I got it by doing as follows:
$website_tags = [];
$tags = WebsiteTag::all();
foreach ($tags as $tag) {
array_push($website_tags, $tag);
}
array_push($website_tags, '');
Related
i have added a new array to my elements like this code :
$elements= Code::get();
foreach($elements as $element){
$string = str_random(15);
$element->total = $string;
}
I would like to return elements after set orderBy() on total using this code
return response()->json(['Status'=> '1','Elements' => $elements ]);
Thank you and i appreciate you help.
Well this is not how foreach works actually. foreach creates a "mirror" of your array so doing any insertion on $element does not affect your initial array at all.
You need to insert your value directly in your array, so you can either use a counter or simpler a for loop.
I will modify your foreach to stay similar to your approach.
Judging by how you tried to insert the value in your array i assume you have a collection and not an array so if that's the case you can add this line before everything:
$elements = $elements->toArray(); //convert collection to array
$counter = 0;
foreach($elements as $element){
$string = str_random(15);
$elements[$counter]['total'] = $string;
$counter++;
}
Your $elements array now has the field total in each of the nested arrays.
Then you simple do:
array_multisort( array_column($elements, "total"), SORT_ASC, $elements);
If you want to sort in descending order simple use SORT_DESC
I have a json file which I read in. I want to first filter the json data to return the object defined by the datasetID, then get out the datasetName. I have filtered in javascript, but would prefer to stay in php but I can't figure it out, any ideas?
note: foreach is not required as only a single record is returned when filtered using the datasetID. So rather than using a foreach method how would you swelect a single record, first for instance?
$datasetID = '5fd4058e5c8d2'; // unique 13 character string
$data = json_decode(file_get_contents(path/to/file), True);
So I need to first filter for the unique object with $datasetID = '5fd4058e5c8d2';
$filtered_data =
Then I need to return the attribute datasetName from that object
$datasetName =
pointers to the best ways to do this is welcomed.
Sample json data:
[
[
{
"datasetID":"5fd4124900827",
"institutionCode":"None",
"collectionCode":"None",
"datasetName":"None"
}
],
[
{
"datasetID":"5fd4058e5c8d2",
"institutionCode":"None",
"collectionCode":"None",
"datasetName":"None",
}
]
]
I don't know how you got that JSON but it is nested deeper than needed. You can merge the top level arrays to flatten it, then index on datasetID:
$data = array_merge(...$data);
$filtered_data = array_column($data, null, 'datasetID')[$datasetID];
$datasetName = $filtered_data['datasetName'];
Shorter:
$filtered_data = array_column(array_merge(...$data), null, 'datasetID')[$datasetID];
$datasetName = $filtered_data['datasetName'];
Or to keep them all to use:
$data = array_column(array_merge(...$data), null, 'datasetID');
$datasetName = $data[$datasetID]['datasetName'];
I tried with your sample JSON.
First, json_decode will return a PHP array to use foreach on it. I wrote a simple foreach and checked your searching ID is equal to element's datasetID. If it is equal this is the datasetName you are searching for.
<?php
$json = '[[{"datasetID":"5fd4124900827","institutionCode":"None","collectionCode":"None","datasetName":"None"}],[{"datasetID":"5fd4058e5c8d2","institutionCode":"None","collectionCode":"None","datasetName":"None"}]]';
$elements = json_decode($json,TRUE);
$searchingID = "5fd4058e5c8d2";
foreach ($elements as $element) {
if(isset($element[0]['datasetID']) && $element[0]['datasetID'] == $searchingID){
$filtered_data = $element[0];
break;
}
}
echo $filtered_data['datasetName']; // or return $filtered_data['datasetName'];
?>
I am merging array values. This is my code and it works fine.
function photomag_image_size() {
$image_sizes = get_intermediate_image_sizes();
return array_merge($image_sizes, array('full',));
}
Now I want to add another element holding an empty string in the first position of the array and also keep full as the last element. I want the first dropdown option to be empty.
Try with array_unshift() php function check here
like this
$queue=array_merge($image_sizes, array(
'full',
) );
array_unshift($queue, " ");
return $queue;
You can either use array_unshift or just merge like you did with full like this:
function photomag_image_size() {
$image_sizes = get_intermediate_image_sizes();
$image_sizes = array_merge(array(""), $image_sizes);
return array_merge($image_sizes, array('full') );
}
use array_merge again
$temp = array_merge($image_sizes, array('full'));
return array_merge(array("first") , $temp);
I don't know is there such thing as dynamic array_intersect? Anyway i have 3 arrays ( later there will be much more arrays)
$kaID = array();
$tgID = array();
$ciID = array();
I want to find matching values for all arrays using array_intersect
Arrays can be created and filled with values or not.
It can be only one populated array OR there can be all three. (later on there will be much more arrays.
How to iterate and create some kind of dynamic expression and get something like this:
array_intersect ($kaID, $tgID,$ciID,.... );
You can do something like this:
$collection = [];
//Dynamic
foreach($ids as $id) {
$collection[] = $id;
}
$result = call_user_func_array('array_intersect', $collection);
I have an array called $brand_terms. I'm accessing two objects in this array. In this case 'name' and 'slug'. I'm then trying to set values of these objects in an associative array called $values. The code is below:
$brand_terms = get_terms("pa_brand");
$values = array(
foreach ($brand_terms as $brand_term){
$brand_term->name => $brand_$term->slug,
}
);
The problem I have is with the separator ,. So the comma at the end of $brand_term->name => $brand_$term->slug,. If the loop is at the last value in the array, the comma is not needed and the the code is broken. Is there a nice way to remove this comma from the last iteration of the foreach loop?
Thanks
That syntax is completely wrong. You cannot have a loop within an array declaration.
Instead create the array, then push elements into it during the loop:
$brand_terms = get_terms("pa_brand");
$values = array();
foreach ($brand_terms as $brand_term){
$values[$brand_term->name] = $brand_$term->slug;
}
Actually, the problem isn't at all with the , literal, in fact that isn't valid PHP. You can't have a foreach loop inside of an array declaration.
The best approach is to define the array and then loop through the get_terms() return value as follows:
$values = array();
foreach( get_terms('pa_brand') as $term )
{
$values[$term->name] = $term->slug;
}