Convert the collection object to string or array in php - php

I want to get the count of each variable in an array.
Am getting the object like [1,1,2,2,3,4,5,4,6]
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
This function is the solution. But I can't make the collection object to string or array.

I noticed this is tagged with laravel, in that case you could do collect($vals)->toArray(). If I remember correctly.
edit: you are looking for the php function implode.
http://php.net/manual/en/function.implode.php
string implode ( string $glue , array $pieces )

Related

How to store the string values into an array in php?

I have one parameter every time it comes from the request object in a standard order,I want to store that string into an array by splitting \for that i am using explode function it's throwing an error,please help me to explode with backslash
$obj = "App\Http\Data\User";
$array = explode("\",$obj);
You can use this:
$obj = "App\Http\Data\User";
$array = explode("\\",$obj);
print_r($array);
example here

Trim string with array using PHP

How can i trim with array of string in php. If I have an dynamic array as follows :
$arr = array(' ','<?php','?>',"'",'"');
so how can i use this array in trim() to remove those string, I tried very hard in the code below :
$text = trim(trim(trim(trim(trim($text),'<?php'),'?>'),'"'),"'");
but i can not use this because array is dynamic, it may have more than 1000 values.
It takes a lot of time to turn into a loop even after trying it.So I can do anything as follows
$text = trim($text, array(' ','<?php','?>',"'",'"') );
It's possible to apply trim() function to an array. The question seems to be unclear but you can use array_map(). Unclear because there are other enough possible solutions to replace substrings. To just apply trim() function to an array, use the following code.
$array = array(); //Your array
$trimmed_array = array_map('trim', $array); //Your trimmed array is here
If you also want to fulfill the argument requirement in trim() you can apply a custom anonymous function to array_map() like this:
$array = array(); //Your array
$trimmed_array = array_map(function($item){
return trim($item, 'characters to be stripped');
}, $array); //Your trimmed array is here

array_map does not give an empty array

I have the below line which takes a string, explodes it to an array by ',' and then trims any whitespace for each array item.
$essentialArray = array_map('trim', explode(',', $essential_skills));
However when $essential_skills string = "" $essentialArray will equal Array ( [0] => )
But I need it to equal Array() so that I can call empty() on on it.
That is normal and logical behavior of the function.
Look at it this way: what does the explode() call return? An array with a single element which is the empty string. The array_map() call does not change that at all.
So you might ask: why does explode('') result in an array with a single element which is the empty string? Well, why not? It takes the empty string and splits it at all comma characters in there, so exactly no times. That means the empty string stays unaltered. But it does not magically vanish!
explode return Array ( [0] => ), so you need to check your string before array_map
here is a one solution
$exploderesult = $essential_skills ? explode(',', $essential_skills) : array();
$essentialArray = array_map('trim', $exploderesult );
The easiest solution here will be just filter result array, like this:
$essentialArray = array_filter(array_map('trim', explode(',', $essential_skills)));
but proper way is:
if ($essential_skills === '') {
$essentialArray = [];
}
else {
$essentialArray = array_map('trim', explode(',', $essential_skills));
}

How can I add a variable to an array?

How can I add a variable to an array? Let say I have variable named $new_values:
$new_values=",543,432,888"
And now I would like to add $new_values to function. I tried in that way:
phpfunction1(array(114,763 .$new_values. ), $test);
but I got an error Parse error: syntax error, unexpected T_VARIABLE, expecting ')'
How my code should look if I would like to have array(114,763,543,432,888)?
$new_values=",543,432,888";
should be converted to an array:
$new_values= explode(',', "543,432,888");
and merged to existing values with:
array_merge(array(114,763), $new_values);
Whole code should looks like:
$new_values = explode(',', "543,432,888");
$values = array(114,763);
$values = array_merge($values, $new_values);
phpfunction1($values, $test);
If you pass to explode a string that is starting with , you will get first empty element, so avoid it.
if you have an array already i.e.
$values = array(543,432,888);
You can add to them by : $values[]=114; $values[]=763;
Apologies if I missed the point there...
In your example, $new_values is a string, but, since it's comma delimited, you can create an array directly from it. Use $new_array = explode(',', $new_values); to create an array from the string.
You need to convert the string into an array using the explode function and then use the array_merge function to merge the two arrays into one:
$new_values=",543,432,888";
$currentArray=array(114,763);
$newArray=array_merge($currentArray,explode(',',$new_values));
functionX($newArray...)
But watch out for the empty array element because of the first comma.
For that use "trim($new_values, ',')" - see answer from rajesh.
you can do like this.
$old_values = array(122,555);
$new_values=",543,432,888";
$values = explode(',', trim($new_values, ','));
$result = array_merge($old_values, $values);
print_r($result);
try array merge
looks like this
phpfunction1(array_merge(array(114,763) ,$new_values), $test);
and yes your first array is not an array
change it to this
$new_values=Array(543,432,888);

declare or convert a string to array format PHP

How to convert a string format into an array format?
I have a string, $string = 'abcde'
I want to convert it to a 1 element array
$string[0] = 'abcde'
Is there a built in function for this task? Or the shortest way is to
$string = 'abcde';
$array[0] = $string;
$string = $array;
TIA
All kinds of ways in php..
$array = array('abcde');
$array[] = 'abcde';
Etc... not too sure what you're going for.
Edit: Oh, I think you might want to convert the first variable? Like this?
//Define the string
$myString = 'abcde';
//Convert the same variable to an array
$myString = array($myString);
Edit 2: Ahh, your comment above I think clears it up a little. You're getting back either an array or a string and don't know which. If you do what I just said above, you might get an array inside an array and you don't want that. So cast instead:
$someReturnValue = "a string";
$someReturnValue = (array)$someReturnValue;
print_r($someReturnValue);
//returns
Array
(
[0] => a string
)
$someReturnValue = array("a string inside an array");
$someReturnValue = (array)$someReturnValue;
print_r($someReturnValue);
//returns
Array
(
[0] => a string inside an array
)
Lets not forget the standard way of declaring a string as an array in PHP, before adding elements..
$string = array();

Categories