<?php
$data=array(array("name"=>"Ambiorix","Lastname"=>"Rodriguez","age"=>21));
$info=json_encode($data);
print_r("{info : ".$info."}");
?>
lunes, 6 de enero de 2020
MYSQL to json Object array
<?php
$link = mysqli_connect("localhost", "root", "", "asteriskcdrdb");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$cars=array();
$query = " select * from cdr limit 5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "$row[uniqueid], $row[src]<br>";
$row[uniqueid]=array("src"=>$row[src],"dst"=>$row[dst]);
$cars[]=$row[uniqueid];
}
/* free result set */
mysqli_free_result($result);
}
$info=json_encode($cars);
print_r($info);
//else {
// printf("Error: %s\n", mysqli_error($link));
//}
$link = mysqli_connect("localhost", "root", "", "asteriskcdrdb");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$cars=array();
$query = " select * from cdr limit 5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "$row[uniqueid], $row[src]<br>";
$row[uniqueid]=array("src"=>$row[src],"dst"=>$row[dst]);
$cars[]=$row[uniqueid];
}
/* free result set */
mysqli_free_result($result);
}
$info=json_encode($cars);
print_r($info);
//else {
// printf("Error: %s\n", mysqli_error($link));
//}
/* close connection */
mysqli_close($link);
?>
[{"src":"+16145993800","dst":"s"},{"src":"+16145993800","dst":"600"},{"src":"+16145993800","dst":"600"},{"src":"100","dst":"500"},{"src":"+16145993800","dst":"600"}]
domingo, 5 de enero de 2020
creating json array object
<?php
$cars=array(array("name"=>"Ambiorix","lastname"=>"Rodriguez","age"=>38));
$info=json_encode($cars);
print_r($info);
?>
[{"name":"Ambiorix","lastname":"Rodriguez","age":38}]
$cars=array(array("name"=>"Ambiorix","lastname"=>"Rodriguez","age"=>38));
$info=json_encode($cars);
print_r($info);
?>
[{"name":"Ambiorix","lastname":"Rodriguez","age":38}]
viernes, 3 de enero de 2020
allow numbers
106
You can use preg_replace in this case;
$res = preg_replace("/[^0-9]/", "", "Every 6 Months" );
$res return 6 in this case.
If want also to include decimal separator or thousand separator check this example:
$res = preg_replace("/[^0-9.]/", "", "$ 123.099");
$res returns "123.099" in this case
Include period as decimal separator or thousand separator: "/[^0-9.]/"
Include coma as decimal separator or thousand separator: "/[^0-9,]/"
Include period and coma as decimal separator and thousand separator: "/[^0-9,.]/"
domingo, 10 de noviembre de 2019
object to array
217
The easiest way is to JSON-encode your object and then decode it back to an array:
$array = json_decode(json_encode($object), True);
Or if you prefer, you can traverse the object manually, too:
foreach ($object as $value)
$array[] = $value->post_id
$info=json_decode($reply);
$array = json_decode(json_encode($info), True);
//print_r($array);
foreach($array['result'] as $key=>$value) {
echo " $key : $value<br>";
}
Curl request using get and JSON and a Custom header
curl -H "Content-Type: application/json" -H "apikey:3ac" -G -d "telephone=100&call_id=555" http://api.dev.com
we use the H parameter multiples time for more than 1 header
we use the H parameter multiples time for more than 1 header
martes, 5 de noviembre de 2019
mult function to clean string
<?php
$agent_audio="Local/101@from-queue/n";
echo $d= str_replace('/','',stristr(stristr($agent_audio, '@', true),'/'));
echo "<br>"
?>
will print 101
$agent_audio="Local/101@from-queue/n";
echo $d= str_replace('/','',stristr(stristr($agent_audio, '@', true),'/'));
echo "<br>"
?>
will print 101
Suscribirse a:
Entradas (Atom)