Now my array is Storing values like
Array([0] => "Aaa", "bbb", "ccc")
How can I make this Array as below using PHP
Array[0] = "Aaa", Array[1] = "bbb"
How to make like this. I tried with explode its not taking values correctly If anyone knows solution. Please help me to get out of this. Thanks in advance.
$oldarray[0]='"abc","def","hij"';
$oldarray[1]='"abc","def","hij"';
$newarray=array();
foreach ($oldarray as $value) {
$newarray[] = str_replace('"','',explode(',',$value));
//print_r($value);die();
}
print_r($newarray);
Use explode
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter comma.
$array1 = explode(',',$array[0]);
Use str_replace to remove double quotes
str_replace('"', '', $array[0]);
$array1 = explode(',',str_replace('"', '',$array[0]));
Check array1[0], array1[1] and array1[2] to find your values
Use explode to split the value in multiple values based on the coma, use str_replace to remove the quotes :
you do something like this
$newarray = explode(',',str_replace('"', "",$oldarray[]));
or:
$newarray = explode('","',trim($oldarray[],'"'));
docs
Related
I've got an array;
Array
(
[0] => Test1
[1] => Test2
[2] => Test3
)
Now I've used the Implode Function from which i got the comma separated String:
Test1, Test2, Test3
now I'd like to put a quotes ("") before and after every word e.g.
"Test1", "Test2", "Test3"
How could I change it to work how I want.
Try this simple one-liner:
$quotedStrings = '"' . implode('","', $myArray) . '"';
The "glue" parameter may be a complex string, though you only have to put the " at the beginning and end.
While the answer from Axel is totally fine for the given szenario,
using array_map along with implode will also work.
And this has the advantage, that modifications to each element are not limited to the start/end of the element. For instance, you can turn each entry to lower-case as well, or perform other, more complex operations, before applying implode.
$quotedString = implode(",", array_map("myCallback", $myArray));
function myCallback($entry){
//here you can to whatever you like to EACH element.
return '"'.$entry.'"';
}
Consider this an option
You can use make a new array by using foreach loop and then use implode. Use the code below
<?php
$array = array("Test1","Test2","Test3");
$quotes = array();
foreach($array as $p){
$quotes[] = '"'.$p.'"';
}
echo implode(",",$quotes); // Outputs "Test1","Test2","Test3"
?>
Another way you can do it by enclosing the implode in "" quotes and then implode comma , in quotes.
<?php
$array = array("Test1","Test2","Test3");
echo '"'.implode('","',$array).'"'; // Outputs "Test1","Test2","Test3"
?>
Hope this helps you
I have one string variable as
$p_list="1,2,3,4";
i want to convert it to an array such as
$a[0]='1';
$a[1]='2';
$a[2]='3';
$a[3]='4';
how to do this in php?
Use explode.
$p_list = "1,2,3,4";
$array = explode(',', $p_list);
See Codepad.
Try explode $a=explode(",","1,2,3,4");
Try this:
In PHP, explode function will convert string to array, If string has certain pattern.
<?php
$p_list = "1,2,3,4";
$noArr = explode(",", $p_list);
var_dump($noArr);
?>
You will get array with values stores in it.
Thanks
array_intersect takes two arrays and looks for matching === values and returns the result. However the values in the array have to match character for character. Is there a function or a method for comparing two arrays and looking for values that contain similar strings instead of equal similar strings. Something like stripos but with array_intersect.
$array1 = array("howdyhorse", "monkeyjoe", "bill", "donkeymonkey", "carrothorse")
$array2 = array("bill", "horse", "monkeybunk", "apple", "panda")
function($array1, $array2);
Returns an array = array("bill", "horse", "monkeyjoe")
The order is of no particular concern.
Is running all the values of each array through something like
foreach( $array as $slice )
$slice = trim( preg_replace( $pattern, $replacement ) ) ;
to make everything lowercase and remove spaces and special chars and then doing an array_intersect an option?
You could use array_uintersect and similar_text. similar_text is O(N**3), so you need to write your own function if your compare similar logic is simpler.
Array {
[0] => http://abc.com/video/ghgh23;
[1] => http://smtech.com/file/mwerq2;
}
I want to replace the content between /sometext/ from the above array. Like I want to replace video, file with abc.
You don't need to loop over every element of the array, str_replace can take an array to replace with:
$myArray = str_replace(array('/video/', '/file/'), '/abc/', $myArray);
However, based on your question, you might want to replace the first path segment, and not a specific index. So to do that:
$myArray = preg_replace('((?<!/)/([^/]+)/)', '/abc/', $myArray);
That will replace the first path element of every URL in $myArray with /abc/...
One way is to use str_replace()
You can check it out here: http://php.net/str_replace
Either str_replace as other comments suggested or using a regular expression especially if you might have a longer url with more segments like http://example.com/xxx/somestuff/morestuff
In that case str_replace will not be enough you will need preg_replace
This is another option. Supply a array, foreach will pick it up and then the first parameter of str_replace can be a array if needed. Hope you find this helpful.
<?php
$array = array('http://abc.com/video/ghgh23','http://smtech.com/file/mwerq2');
$newarray = array();
foreach($array as $url) {
$newarray[] = str_replace(array('video','file'),'abc',$url);
}
print_r($newarray);
?>
//every element in $myArray
for($i=0; $i < count($myArray); $i++){
$myArray[$i] = str_replace('/video/','/abc/',$myArray[$i]);
}
$array = array('http://abc.com/video/ghgh23', 'http://smtech.com/file/mwerq2');
foreach ($array as &$string)
{
$string = str_replace('video', 'abc', $string);
$string = str_replace('file', 'abc', $string);
}
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);