I'm having issues building a proper URL parameter sent to a service. I need to send dynamic query results in one parameter. Here's what I have reached so far:
<a href="https://serviceurl/?language=EN&variable1=<?php
$mobiles = array();
while($row_query = mysql_fetch_assoc($query))
{
$data = $row_query['rowwithvalues'];
$myArray = implode(',', $data);
foreach($myArray as $my_Array){
echo $my_Array;
}
}
?>&variable2=Hello">Test</a>
The above returns a blank value for variable1 in the url.
Thanks in advance!
When you calling $myArray = implode(',', $data); it makes $myArray string from implode doc:
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
So you cannot loop on it - I guess you wanted to use explode. Try this:
<a href="https://serviceurl/?language=EN&variable1=
<?php
while($row_query = mysql_fetch_assoc($query)) {
$data = $row_query['rowwithvalues'];
$myArray = explode(',', $data);
foreach($myArray as $my_Array){
echo $my_Array;
}
}
?>&variable2=Hello">Test</a>
Related
I am trying to read a string into an array in PHP, but it doesn't work for me.
The string I would like to read:
$output = {"message":"Approved","responseCode":"0","responseCodeDesc":"Transaction Successful"}
The code I am using:
$arr = explode(',', $output);
foreach($arr as $v) {
$valarr = explode(':', $v);
preg_match_all('/"(.*?)"/', $valarr[0], $matches);
$narr[$matches[1][0]][$matches[1][1]] = $valarr[1];
}
Specifically, I would like to access the value for 'message' (i.e., 'Approved').
I tried this, but it still fails:
echo 'MESSAGE ' . $arr['message'];
Here is working code,
$arr = '{"message":"Approved","responseCode":"0","responseCodeDesc":"Transaction Successful"}';
$arr = json_decode($arr, true);
echo $arr['message'];
print_r($arr);
Here is working link
Thats not string, its json..
$array = json_decode($output,true);
I'm trying to write a function that do the following :
Let's say i have an array :
$data = array(
array('10','15','20','25'),
array('Blue','Red','Green'),
array('XL','XS')
)
and my result array should be like :
$result = array(
array('10','15','20','25'),
array('Blue','Red','Green','Blue','Red','Green','Blue','Red','Green','Blue','Red','Green')
array('XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS')
)
Im stuck with this because i want a function that is able to do this no matter how much array there is in the first array $data
I have only been able to write this, which is what give the $result array :
foreach($data[2] as $value2){
$result[2][] = $value2;
foreach($data[1] as $value1){
$result[1][] = $value1;
foreach($data[0] as $value0){
$result[0][] = $value0;
}
}
}
After a few research, it seems that a recursive function is the way to go in order to dynamically generate those foreach but i can't get it to work.
Thanks for your help.
This is dynamic:
$result[] = array_shift($data);
foreach($data as $value) {
$result[] = call_user_func_array('array_merge',
array_fill(0, count($result[0]), $value));
}
Get and remove the first element from original
Loop remaining elements and fill result with values X number of values in first element
Since the elements were arrays merge them all into result
If modifying the original is unwanted, then use this method:
$result[] = reset($data);
while($value = next($data)) {
$result[] = call_user_func_array('array_merge',
array_fill(0, count($result[0]), $value));
}
just use array_fill and array_merge functions
$result = array(
$data[0],
array_merge(...array_fill(0,count($data[0]), $data[1])),
array_merge(...array_fill(0,count($data[0])*count($data[0]), $data[2]))
);
print_r($result);
demo on eval.in
So I have a string with comma separated values:
$accounts = "1,2,3,4,5,6";
And I want to reverse that order. So I wrote this:
$accountsrev = implode(',',rsort(explode(',',$accounts)));
Basically I convert to an array, reverse the array, and implode it back into a string. What's wrong with that?
I get a load of errors like this:
Strict Standards: Only variables should be passed by reference in /home/username/public_html/file.php on line 121
Warning: implode(): Invalid arguments passed in /home/username/public_html/file.php on line 121
Edit:
Now I wonder if the way I build the $accounts variable is wrong. I pull 7 rows from the database and then build the $accounts variable in a while loop. The id is an integer in the database:
$accounts = '';
$i = 1;
while ($data = $getdata->fetch(PDO::FETCH_ASSOC)) {
if ($i < 7) {
$accounts .= $data['id'].',';
} else {
$accounts .= $data['id'];
}
$i++;
}
Does the way I make the $accounts variable not produce a string?
This is just something that tells you you're doing something completely wrong:
$array = [1,2,3,4];
rsort($array);
//$array is sorted.
However:
rsort(array_filter($array));
//Array filter returned a copy of the original array so $array is neither sorted nor filtered.
You need to do:
$accounts = '';
while ($data = $getdata->fetch(PDO::FETCH_ASSOC)) {
$accounts .= $data['id'].',';
}
$accountsrev = explode(',',rtrim($accounts,","));
rsort($accountsrev);
$accountsrev = implode(',',$accountsrev);//accountsrev is sorted here
<?php
$accounts = '';
$i = 0;
while ($data = $getdata->fetch(PDO::FETCH_ASSOC)) {
$i++;
if($i == 1){
$accounts = $data['id'];
} else {
$accounts .= $data['id'].',';
}
}
$accountsrev = explode(',',$accounts); // explode it as make array
rsort($accountsrev); // then use rsort which sort array reverse
$accountsrev = implode(',',$accountsrev); // again implode it
echo $accountsrev;
?>
then output will be
6,5,4,3,2,1
or you can use array_reverse () function instead rsort
echo $accountsrev = implode(',',array_reverse(explode(',',$accounts)));
then output will be
6,5,4,3,2,1
Quick and easy:
$accounts = '1,2,3,4,5,6';
echo $accountsrev = implode(',',array_reverse(explode(',',$accounts)));
Array : [{"ID":1},{"ID":2}]
$id=1;
I want to check if $id exists in the array.
Thank you!
You may try Laravel's Collection::contains method, for example:
$collection = collect(json_decode($jsonString, true));
if ($collection->contains(1) {
// Exists...
}
Also, you may use key/value pair like this:
if ($collection->contains('ID', 1) {
//...
}
Also, if you want to get that item from the collection then you may try where like this:
$id = $collection->where('ID', 1)->first(); // ['ID' => 1]
You have a json formatted array and you need to decode it using json_decode first. After that loop the array to check for the id that you want.
So the code should look like this:
$json = '[{"ID":1},{"ID":2}]';
$id = 1;
$data = json_decode($json, true);
foreach($data as $item){
if($item['id'] == $id) {
echo 'it exists';
}
}
Iterate the array using for loop and use the value as a param to json_decode.
$id = 1;
$arr = array('{"ID":1}', '{"ID":2}');
foreach($arr as $val) {
if (in_array($id, json_decode($val, TRUE))) {
echo "id present";
}
}
Try this, if value is exist it will give key of array
$jsondata = '[{"ID":1},{"ID":2}]';
$array = json_decode($jsondata,true);
$key = array_search(1, array_column($array, 'ID'));
Just check if the string is in the json array, with little computation.
I think it's the more efficient way. Check the result here.
<?php
$id = 1;
$array = ['{"ID":1}', '{"ID":2}'];
echo in_array(json_encode(["ID" => $id]), $array) ? 'Yes' : 'No';
I cannot solve this seeming simple problem. I have the following simple code and all I want is to echo the result of $ATL5_Alert_query and separated by a comma (,):
$ATL5_Alert_query = mysql_query("SELECT `Mobile` FROM `dbo_tech_name` WHERE `AlertLevel`= 1");
$dataset = array();
while ($data = mysql_fetch_array($ATL5_Alert_query))
{
$dataset[] = $data;
}
echo implode (",", $dataset);
However, I'm getting "Notice: Array to string conversion "...
In your code $data is array as well, so $dataset becomes an array of arrays, which you cannot concatenate. You should get the searched value by this:
while ($data = mysql_fetch_array($ATL5_Alert_query))
{
$dataset[] = $data['Mobile'];
}
or:
while ($data = mysql_fetch_array($ATL5_Alert_query))
{
$dataset[] = $data[0];
}
or:
while ($data = mysql_fetch_assoc($ATL5_Alert_query))
{
$dataset[] = $data['Mobile'];
}
If you however cannot change this, and already have your $dataset array, you can implode it like that:
echo implode(',', array_map(function($a){
return $a['Mobile'];
},$dataset));