I'm using API Random.ORG with vendor engine LoLFactor\RandomORG\
My Code:
include '../vendor/autoload.php';
use LoLFactor\RandomORG\RandomORGClient;
$client = new RandomORGClient('5035-35353-53535-53535-3535');
$data = $client->generateSignedIntegers(1, 1, 60);
$i = 0;
$arr = array();
foreach($data as $randomInt) {
array_push($arr, ($randomInt));
}
echo '<br>';
var_dump(json_encode($arr));
And i get array
"["2.0",{"random":{"method":"generateSignedIntegers","hashedApiKey":"FZSXJR+2P+umSj1UIOvBEc3RpYuwh8ZQfqppk8O5froIsgTlcPWRmoYkKzLB07cCs4TXgMcZS+MduDyF0RK9+Q==","n":1,"min":1,"max":60,"replacement":true,"base":10,"data":[48],"completionTime":"2015-10-03 11:52:32Z","serialNumber":276},"signature":"B8FsTieibcDqHCa3uK2zTGC6ZCwx42ZyzIhymvg2RnPFYidcNwQ086ljhL8w+Ul6ui+y2OPlSymB4Zse+R6pmDlRNzgFvbhe+TqCyq8I41UU6lQyGIQq\/EGuyt+7qKAaEYOj1v98xsxQ1r7Ttxb1bJnWi7bNuVe5S0YiVo\/7JrP25RoLiso0K4P62mnySfFtCwR5j7MXM2OErQbxI6vVuvBh3bLS6jvuL7FWURs5RJZr0Ii2QZnK994PRtGz1kLjooVOII2t7cNejzq66Wc6ayLoasWgWaEXaKsQ9hTOeBCECWdA1woDy68QtEuZMr6cknhOdjmpNqiB+hYnkWitxecLVPTHW2UGG19OqwBfPQ0EHV+vnEyxoGlihf+xhjt+Pp56leKN69XKI0IxmgJAYcr99AJs0c4WUdl93Ft+mexCvOrf+7AVmMpqa6LCCJBaY2RRAL+mwSZO\/tVRaN3iiZRfAXXRqc\/nTKgHA3a+LnqOraxM\/wQidzzxyBsmSKbQIIDR0XFZbbGLlBStRFUeYRUonKLUlt5O9dn9Dm7OWiBH+Sg3gw7mBNNg9elyfMlgndSHHIk5xi1bSlNexZ3GHEljpB2VDsLI3WC1pOygdwobgZyIK4hrISPqhfolAWfI8B\/LRoaeZ5osNiGV79KaESy3VpDk4epLL\/7T0DaRErY=","bitsUsed":6,"bitsLeft":248458,"requestsLeft":742,"advisoryDelay":0},"G5nucL0Wdy9xtbejw"]"
Its bad browser console:
But i need array this type:
"{"random":{"method":"generateSignedIntegers","hashedApiKey":"FZSXJR+2P+umSj1UIOvBEc3RpYuwh8ZQfqppk8O5froIsgTlcPWRmoYkKzLB07cCs4TXgMcZS+MduDyF0RK9+Q==","n":1,"min":1,"max":60,"replacement":true,"base":10,"data":[48],"completionTime":"2015-10-03 11:52:32Z","serialNumber":276},"signature":"B8FsTieibcDqHCa3uK2zTGC6ZCwx42ZyzIhymvg2RnPFYidcNwQ086ljhL8w+Ul6ui+y2OPlSymB4Zse+R6pmDlRNzgFvbhe+TqCyq8I41UU6lQyGIQq\/EGuyt+7qKAaEYOj1v98xsxQ1r7Ttxb1bJnWi7bNuVe5S0YiVo\/7JrP25RoLiso0K4P62mnySfFtCwR5j7MXM2OErQbxI6vVuvBh3bLS6jvuL7FWURs5RJZr0Ii2QZnK994PRtGz1kLjooVOII2t7cNejzq66Wc6ayLoasWgWaEXaKsQ9hTOeBCECWdA1woDy68QtEuZMr6cknhOdjmpNqiB+hYnkWitxecLVPTHW2UGG19OqwBfPQ0EHV+vnEyxoGlihf+xhjt+Pp56leKN69XKI0IxmgJAYcr99AJs0c4WUdl93Ft+mexCvOrf+7AVmMpqa6LCCJBaY2RRAL+mwSZO\/tVRaN3iiZRfAXXRqc\/nTKgHA3a+LnqOraxM\/wQidzzxyBsmSKbQIIDR0XFZbbGLlBStRFUeYRUonKLUlt5O9dn9Dm7OWiBH+Sg3gw7mBNNg9elyfMlgndSHHIk5xi1bSlNexZ3GHEljpB2VDsLI3WC1pOygdwobgZyIK4hrISPqhfolAWfI8B\/LRoaeZ5osNiGV79KaESy3VpDk4epLL\/7T0DaRErY=","bitsUsed":6,"bitsLeft":248458,"requestsLeft":742,"advisoryDelay":0}"
Its good browser console:
without code ["2.0", and code ,"G5nucL0Wdy9xtbejw"] at end.
How i can delete first and last key-values. ?
I try array_merge , array_split but do not understand. How to create array that i need..
Sounds like instead of var_dump(json_encode($arr)); you just want var_dump(json_encode($arr[1])); ... as in, add the [1] to get the middle element of the array.
Alternatively, $arr = $arr[1];
But, array_slice, array_pop, array_shift won't really help you. They'll shorten the array to a single element, but you don't want the outer array at all, so just extract the middle element that you do want.
Related
when in use json_decod with option "JSON_FORCE_OBJECT" its return out put index started with 0 and its true but i need to start the out put index with 1 so how i can fix my problem?
json_encode($request->get('poll_items'), JSON_FORCE_OBJECT)
The output result is and current BUT:
"{"0":"option1","1":"option2","2":"option3"}"
I need to return like this:
"{"1":"option1","2":"option2","3":"option3"}"
Thank you.
An easy solution would be to use array_unshift() and unset():
$array = $request->get('poll_items');
// Add an element to the beginning
array_shift($array, '');
// Unset the first element
unset($array[0]);
Now you're left with an associative array that starts with 1.
Here's a demo
My first question would be why do you need this to be 1 indexed instead of 0?
If this data is consumed outside of your control then you could map the data across to another array and encode that instead. For example:
$newArray = array();
foreach ($request->get('poll_items') as $index => $value) {
$newArray[++$index] = $value
}
$output = json_encode($newArray, JSON_FORCE_OBJECT);
NOTE: ++$index instead of $index++ as the latter will only alter the value after the line has computed.
I need to remove a element from an associative array with an string structure.
Example:
$array = array(
"one"=>array("Hello", "world"),
"two"=>"Hi"
)
I want to create a function that removes the elements like this:
function removeElement($p) {
// With the information provided in $p something like this should happen
// unset($array["one"]["hello"])
}
removeElement("one.hello");
Your base array is associative, the inner array (key one) is not, its a indexed array, which you can not access via ["hello"] but rather [0].
You can remove the hello value by using the unset function, but the indexes will stay as they are:
$array = ['Hello', 'World']; // array(0: Hello, 1: World)
unset($array[0]); // Array is now array(1: World)
If you wish to keep unset and keep the array indexes in order, you can fetch the values using the array_values function after unset:
unset($array[0]);
$array = array_values($array); // array(0: World)
Or you could use array_splice.
When it comes to using a string as key for multidimensional array with a dot-separator I'd recommend taking a look at laravels Arr::forget method which does pretty much exactly what you are asking about.
This would be a static solution to your question, in any case, you need to use explode.
function removeElement($p, $array) {
$_p = explode('.', $p);
return unset($array[$_p[0]][$_p[1]]);
}
But bear in mind, this doesn't work if you have more in $p (like: foo.bar.quux)
how to slice an array to pass it to a function. I cannot use unset because I need that array further. I know I can copy whole array to variable, however it's quite big and don't thing it's efficient. My code:
$list = array(0=>2123, 2=>1231, 7=>123123,...);
unset($list[0]); //I can't do this because I still need whole $list
$seats = $allocatingClass->allocateSeats($seatsNumber, $list); //here I need to slice $key from $list and pass $list without reiterating
If you need to keep index 0, store it, rather than storing the entire array elsewhere:
$tmp = $list[0];
Then splice the array:
$list = array_splice($list,1,count($list));
Then pass it to your function:
$seats = $allocatingClass->allocateSeats($seatsNumber, $list);
Then, when you need it, put back the value to the array:
$list[] = $tmp;
Edit: if you actually need to put it exactly at index 0, then you may want to unset the index 0 of the array instead of splicing it. If you can, however, push it at the end of the array just follow what is written above.
To clearify, if you need to LATER push back the element to index 0, do
unset($list[0]);
instead of the splice...
And to put back the element, do:
$list[0] = $tmp;
However you do it, a copy will be made when passing the array (unless you pass it by reference).
either use splice. or create a copy and shift one value.
after sending the copy variable, you can unset it so it wont keeptaking its space.
Edit :
The above solution is also viable. Though I suggest you use:
$tmp = array_shift($arr);
doStuf($arr);
array_unshift($arr, $tmp);
i have a problem i want to add slashes at the starting and the end of each string of my array.
This is an example of my actual array :
$patte = array();
$patte[0] = "httpd";
$patte[1] = "vsftpd";
$patte[2] = 'gohphp';
$patte[3] = 'abcdef';
i use this array for taking information into a DataBase so i can't place slashes now, or this is going to not working.
(mysql_query ... while mysql_fetch_array ...)
I need to rename these entry.
For this i use a second array, and with the command : "preg_replace" i can translate every strings like i want.
But preg_replace want me to add slashes in $patte
I want to obtain an array like this
$pattes = array();
$pattes[0] = "/httpd/";
$pattes[1] = "/vsftpd/";
$pattes[2] = '/gohphp/';
$pattes[3] = '/abcdef/';
Can you help me please.
I'm gonna have like 1000 line into this array.
Using array_map() you can apply callback to every element of your array :
function addSlashes($str)
{
return "/".$str."/";
}
$newArray = array_map("addSlashes", $patte);//array with the new values
Use array_map:
$pattes = array_map(function($str) {
return '/'.$str.'/';
}, $pattes);
I'm using a nestedsortable jQuery plugin that gives me the order/sort of the elements serialized.
And example of this serialitzation (root means parent_id=0):
id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3
First thing I'll do is explode by &:
$serialized = "id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3";
$exploded = explode("&", $serialized);
But I don't know then how to manage a id[1]=root or id[3]=1. How I can do it?
And another question. In this way I don't know which is how to store the order. When I've the exploded with in array like array("id"=>1, "parent"=>"root"); I've to store the order. I will do it with an index, but how I recognise nested levels?
An example:
$i = 0;
foreach($exploded as $explode)
{
//update every id in MySQL and set parent=$explode["parent"] and order=$i
$i++;
}
But if I've N levels, how I can have a index $i for every one of them?
Thank you in advance!
Rather than exploding, you could try parse_str()
<?php
parse_str("id[1]=root&id[5]=1&id[2]=1&id[3]=1&id[4]=3",$result);
print_r($result);
?>
From there you can work with the array.