Turning PHP key=>value into sub array: [key,value] [duplicate] - php

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":[""],"sec‌​ond":[""],"third":["‌​"],"fourth":[""],"fi‌​fth":[""],"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.

Related

Convert js stringified string to multidimensional array in php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I get this kind of string from JS to PHP.
[[1,"10.00","10.00","A"],[2,"20.00","25.00","B"],[3,"10.00","25.00","C"],[6,"10.00","25.00","D"]]
How can I convert it to a php multidimensional array?
Note:
I tried json_encode without getting the desired result.
<?php
$string = '[[1,"10.00","10.00","A"],[2,"20.00","25.00","B"],[3,"10.00","25.00","C"],[6,"10.00","25.00","D"]]';
$result = json_decode($string);
var_dump($result);
?>
You should use json_decode instead of json_encode for convert JSON to array PHP
Result:

Use array values as new multidimensional array keys [duplicate]

This question already has answers here:
Using a string path to set nested array data [duplicate]
(8 answers)
Closed 4 years ago.
Edit (after downvote): There is a similar question here > Using a string path to set nested array data
However I didn't find that question when searching for an answer due to the way it's worded, and I'm sure this will happen for other people, so this question may act as a useful gateway to that question and it's answers.
I'm sure I'm missing something obvious, but I can't think how to do this: I have an array containing one or more items:
array('value1', 'value2');
I need to use these values as the keys in a multidimensional array :
array['value1']['value2'] = 'somevalue';
How do I do this?
You can use a nice recursion here:
function nestArray($items, $value) {
return $items ?
array($items[0] => nestArray(array_slice($items, 1), $value))
: $value;
}
$array = array('value1', 'value2');
print_r(nestArray($array, 'somevalue'));

Fastest way to convert php associate array to string [duplicate]

This question already has answers here:
Convert flat array to a delimited string to be saved in the database
(13 answers)
Closed 6 years ago.
What will be the fastest and best way to convert the following PHP associate array to a string without json.
array
Array ( [0] => A [1] => S [2] => G)
to a string, exactly like
String = "A, S, G";
Most of the solutions available are using JSON, that I don't want to use. plus I want to know the "Fastest" and "Best" way.
Use the implode function:
$str = implode(array('a', 'b', 'c'));
http://php.net/manual/en/function.implode.php
Use
implode()
And read more here http://php.net/manual/en/function.implode.php
One way is using print_r(array, true) and it will return string representation of array

Obtaining all instances of a specific field from json in PHP [duplicate]

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.

Compare two arrays without sort them [duplicate]

This question already has answers here:
comparing arrays in php, without caring for the order
(6 answers)
Closed 9 years ago.
I'm trying solving a problem where i need to check if the arrays are same no matter how they are sorted i cannot use sorting because it add extra over head to time this function is taking in answering.
I am currently using array_diff_assoc
$arr1 = array(1,2,3);
$arr2 = array(3,2,1);
$result = array_diff_assoc($arr1,$arr2);
print_r($result);
Array
(
[0] => 1
[2] => 3
)
But the above arrays are same!! The human way.
Any idea for comparing two arrays.
Well interpreter is not human right ? ;)
Even if you do a simple var_dump($arr1==$arr2) on your existing array, it will return false.
This below code returns true !
$arr1 = array(1,2,3);
$arr2 = array(2=>3,1=>2,0=>1);//position is same as yours., i've just set a key
var_dump($arr1==$arr2); //true

Categories