This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I have a webservice that returns a very large json object. I decode this to an array for parsing.
What I'm trying to do though is extract all array members from the json array where there is [id] => 21 at the start of the individual array object.
A nice simple task using LINQ, but not an idea how to do this in PHP. There has to be a way to filter through and extract.
I'd use something like array_filter for this
function find_id($var)
{
// returns whether the is is 21
return($var['id'] === 21);
}
$result = array_filter($jsondata, "find_id"));
array_filter Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
Related
This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
if i solve this i will have my desired result for any expert this will take few minutes to guess and i am stuck in this from last 2 hours searching and googling but couldn't found the one i want. Okay so here is the thing ...
I am sending data through ajax to my php by doing JSON.stringify and receving that that to my php in the form of this
{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}
All i want to do is to get the values and insert them to the separate variables.
You need to use json_decode to convert the string to array. You can pass the second parameter to this method to convert this to array instead of object. Then you can retrieve the data like
$string = '{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}';
$array = json_decode($string, true);
print_r($array[0][0]);
Here's the example of it https://repl.it/HhI8/1
This question already has answers here:
reformat an php array [duplicate]
(2 answers)
Closed 10 months ago.
I'm having following data:
Array ( [one] => this [two] => this2 )
Which I want to convert to json type which looks like:
data:{[one,this],[two,this2]}
How can I get through this in effecient manner?
Edit:
I've tried a lot of things This is actual data which I need to make datatable compatible:
{"draw":0,"recordsTotal":null,"recordsFiltered":null,"data":[[{"first":[""],"second":[""],"third":[""],"fourth":[""],"fifth":[""],"sixth":["value"]}]]}
as the data here is in key=>value form json is not compatible for datatables (PHP)
You can use array_map and array_keys:
$result = array_map(null, array_keys($array), $array);
$json = json_encode($result);
Here is working demo.
This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];
This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 9 years ago.
I've been searching around but i'm failing to find an answer to something that seems it should be simple to fix!
I'm reading products from XML and putting their data into an array on a loop, the array is called $res.
Now, i need to put a value from $res into another array for loading to the DB (magento SOAP API). But when i do this i do not get a string value i wxpect, instead i get an the first array inside the second.
Here is the problem line:
$fieldDateData = array('rts_date'=>$res[0]->BackInStockDate1);
I've tried a few different things, none have worked. I thought it would be simply enough to do this:
$data = $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
But sadly not, i'm unsure as to why?
Thanks,
EDIT:
This is an example of the output
Array
(
[rts_date] => SimpleXMLElement Object
(
[0] => 28/06/13
)
)
Try
$data = (string)$res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
You need to cast the value you are setting as a string:
$data = (string) $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);