I have a script that is using the google charts API and the gChart wrapper.
I have an array that when dumped looks like this:
$values = implode(',', array_values($backup));
var_dump($values);
string(12) "8526,567,833"
I want to use the array like this:
$piChart = new gPieChart();
$piChart->addDataSet(array($values));
I would have thought this would have looked like this:
$piChart->addDataSet(array(8526,567,833));
Howerver when I run the code it creates a chart with only the first value.
Now when I hardcode the values in instead I get each value in the chart.
Does anyone know why it's acting this way?
Jonesy
I think
$piChart->addDataSet(array_values($backup));
// or just: $piChart->addDataSet($backup); depends on $backup
should do it.
$values only contains a string. So if you do array($values), you create an array with one element:
$values = "8526,567,833";
print_r(array($values));
gives
Array
(
[0] => 8526,567,833
)
array(8526,567,833) would be the same as array_values($backup) or maybe even just $backup, that depends on the $backup array.
Looks like you want to use $backup instead of $values as $values is the imploded string... and since 8526,567,833 isn't a valid number, it parses 8526 and leaves the rest alone.
Related
I have the following string output if I run print_r($val):
{"next_offset":-1,"records":[{"id":"e3266222-5389-11ed-ab30-0210c01ad3d2","name":"That is a nice name"}]}
Now, I need the value of attribute "id". Sounds simple but I'm not able getting there.
Does something like this work?
I'm assuming $val is a json string.
<?php
$val = "{\"next_offset\":-1,\"records\":[{\"id\":\"e3266222-5389-11ed-ab30-0210c01ad3d2\",\"name\":\"That is a nice name\"}]}";
$val = json_decode($val);
print_r($val->records[0]->id);
?>
In PHP i have array variable from another function like this
$v->params:
(
[{"username":"myusername","email":"myemail#gmail_com","phone":"0123456789","password":"abc123","fullname":"myfullname","register_ip":"127_0_0_1","country":"Qu\u1ed1c_Gia","birthday":"N\u0103m_sinh","gender":"male","bank_code":"Ng\u00e2n_h\u00e0ng","ip":"127_0_0_1","os":"Windows_10","device":"Computer","browser":"Mozilla_Firefox_77_0"}] =>
)
Now i want to access to it item, how can i code to access item value like this:
$password = $v->params->password; //myemail#gmail_com
I new with PHP thank you all
The data seems the wrong way round as it's the key of the array rather than a value.
So using array_keys()[0] to get the first key and then json_decode this...
$data = json_decode(array_keys($v->params)[0]);
you can then use the $data object to get at the values...
echo $data->username;
I have a script that loops through and retrieves some specified values and adds them to a php array. I then have it return the value to this script:
//Returns the php array to loop through
$test_list= $db->DatabaseRequest($testing);
//Loops through the $test_list array and retrieves a row for each value
foreach ($test_list as $id => $test) {
$getList = $db->getTest($test['id']);
$id_export[] = $getList ;
}
print(json_encode($id_export));
This returns a JSON value of:
[[{"id":1,"amount":2,"type":"0"}], [{"id":2,"amount":25,"type":"0"}]]
This is causing problems when I try to parse the data onto my android App. The result needs to be something like this:
[{"id":1,"amount":2,"type":"0"}, {"id":2,"amount":25,"type":"0"}]
I realize that the loop is adding the array into another array. My question is how can I loop through a php array and put or keep all of those values into an array and output them in the JSON format above?
of course I think $getList contains an array you database's columns,
use
$id_export[] = $getList[0]
Maybe can do some checks to verify if your $getList array is effectively 1 size
$db->getTest() seems to be returning an array of a single object, maybe more, which you are then adding to a new array. Try one of the following:
If there will only ever be one row, just get the 0 index (the simplest):
$id_export[] = $db->getTest($test['id'])[0];
Or get the current array item:
$getList = $db->getTest($test['id']);
$id_export[] = current($getList); //optionally reset()
If there may be more than one row, merge them (probably a better and safer idea regardless):
$getList = $db->getTest($test['id']);
$id_export = array_merge((array)$id_export, $getList);
I'm trying to build URL query from an Array that looks like that:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
I would like to get query like that:
localhost/get?serial=3804689&serial=3801239&serial=3555689
(you get the idea)
I'm trying to use http_build_query($serials, 'serial', '&'); but it adds the numeric index to the prefix 'serial'.
Any idea how to remove that numeric index?
Well you can't really have the same GET parameter in the string, After all if you try to access that variable server side, what would you use?
$_GET['serial'] - But which serial would it get?
If you really want to get a list of serials, Simply turn the array into a string, save it as an array and there you go. for example :
$serials = "string of serials, delimited by &";
Then you can use the http build query.
Maybe use a foreach:
$get = "localhost/get?serial=" . $serials[0];
unset( $serials[0] );
foreach( $serials AS serial ){
$get .= "&serial=$serial;
}
Just as an FYI, PHP doesn't handle multiple GET variables with the same name natively. You will have to implement something fairly custom. If you are wanting to create a query string with multiple serial numbers, use a delimiter like _ or -.
Ex: soemthing.com/serials.php?serials=09830-20990-91234-12342
To do something like this from an array would be simple
$get_uri = "?serial=" . implode("-", $serials);
You would be able to get the array back from a the string using an explode to
$serials = explode("-", $_GET['serials']);
Yes its quite possible to have such format, you have to build it query string by indices. Like this:
No need to build the query string by hand, use http_build_query() in this case:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$temp = $serials;
unset($serials);
$serials['serial'] = $temp;
$query_string = http_build_query($serials);
echo urldecode($query_string);
// serial[0]=3804689&serial[1]=3801239&serial[2]=3555689&serial[3]=3804687&serial[4]=1404689&serial[5]=6804689&serial[6]=8844689&serial[7]=4104689&serial[8]=2704689&serial[9]=4604689
And then finally, if you need to process it somewhere, just access it thru $_GET['serial'];
$serials = $_GET['serial']; // this will now hold an array of serials
You can also try this
$get = "localhost/get?serial=";
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$list = implode("&serial=",$serials);
echo $get.$list;
Having the same GET parameter will not work this way. You will need to use an array in the get parameter:
localhost/get?serial[]=3804689&serial[]=3801239&serial[]=3555689
IF you just print $_GET through this URL, you will receive
Array ( [serial] => Array ( [0] => 380468 [1] => 3801239 [2]=> 3555689) )
Then you can do this to build your query string
$query_str = 'serial[]='.implode('&serials[]=',$serials);
You can try this version.
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$get = implode('&serials[]=',$serials);
echo 'test';
var_dump($_GET['serials']);
Result
I want to do something like bellow using php:
$turl=array(trim($params->get('c2')));
$tname=array(trim($params->get('cn2')));
and want to display each $turl with each $tname.
I tried like this:
$result=array_combine($turl,$tname);
print_r($result);
but given result as:
Array ( [http://184.107.144.218:8282/,http://184.107.144.218:8082/] => ABC Radio,AHH Radio )
But I want like this:
Array ( [http://184.107.144.218:8282/=> ABC Radio,
http://184.107.144.218:8082/=> AHH Radio )
Thanks in advance
maybe you want to use a code similar to this:
$turl = explode(',',trim($params->get('c2')));
$tname = explode(',',trim($params->get('cn2')));
$result=array_combine($turl,$tname);
print_r($result);
The error seems to be into the $turl and $tname arrays creation as array combine should work as intended
Obviously I would add several checks, as, for example, Have the two arrays the same size?
Addendum
In your comment you give me a specimen of what $params->get returns if called with parameters 'c2' and 'cn2'.
$params->get('c2')="hoicoimasti.com,google.com"
$params->get('cn2')="hoicoi,google".
The example code I gave you do what required, or at leas what I was thinking you are trying to obtain. A different code with a different result is:
$turl = explode(',',trim($params->get('c2')));
$tname = explode(',',trim($params->get('cn2')));
$result=array_map(function($x,$y){return $x.','.$y;},$turl,$tname);
print_r($result);
or, if you want to obtains a single string:
$result=join(',',array_map(function($x,$y){return $x.'=>'.$y;},$turl,$tname));
print_r($result);
You can obtain any desired result modifying one of the previous example.
To encase the results in an option you need a slight variation of the array_map user function:
$result=join("\n",array_map(
function($x,$y){
return "<option>$x=>$y</option>";
},
$turl,
$tname
));
print_r($result);
PHP < 5.3 version
Put this function declaration somewhere in the global scope
function formatOption($x,$y){
return "<option>$x=>$y</option>";
};
and then your code will become:
$result=join("\n",array_map( 'formatOption', $turl, $tname));
print_r($result);
If possible I won't clutter the global namespace with function like formatOption,
but when anonymous function are not available
Reference:
array_map
join