Something like:
[0 => 'a', 1 => 'b']
to json
{
"0": "a",
"1": "b",
}
instead of
["a","b"]
This is what you're looking for.
Forcing the JSON Object is the only solution you're looking for.
$array = array( '0' => 'a', '1' => 'b', '2' => 'c', '3' => 'c' );
$json = json_encode($array, JSON_FORCE_OBJECT);
echo $json;
You can use the JSON_FORCE_OBJECT option:
$array = array(
0 => 'Banana',
1 => 'Minions',
2 => array(
5 => 'MariaOzawa',
6 => 'YukiOsawa'
)
);
$myJsonString = json_encode($MyArray, JSON_FORCE_OBJECT);
print_r($myJsonString);
Then you can see result like this:
{"0":"Banana","1":"Minions","2":{"5":"MariaOzawa","6":"YukiOsawa"}}
With this way, you can keep your array keys whatever how many layers is it into json_object
Hope this help
Related
I have two arrays. One has group names the other one has group items. I want to assign group names as keys to the second array.
Example:
$array1 = array(
0 => "A",
1 => "B"
);
$array2 = array(
0 => "a,b,c,d",
1 => "e,f,g,h"
);
The second array should become:
$array3 = array(
A => "a,b,c,d",
B => "e,f,g,h"
);
How can i achieve this in PHP?
Thanks
use array_combine as such :
$array2 = array_combine($array1, $array2);
you need to use array_combine, api here
would work like this:
<?php
$grpNames = array(0 => "A", 1 => "B");
$grpItems = array(0 => "a,b,c,d", 1 => "e,f,g,h");
$newArray = array();
foreach($grpItems as $grpItemKey => $grpItems){
if(isset($grpNames[$grpItemKey])){
$newArray[$grpNames[$grpItemKey]] = $grpItems;
}
}
var_dump($newArray);
?>
I would like to make an array of arrays with keys associated, and I don't really know what is the proper way to do it.
For example if I have
$array1 = array(
"foo" => "bar",
"bar" => "foo",
);
$array2 = array(
"foo2" => "bar2",
"bar2" => "foo2",
);
Would it be correct to write:
$key_array=array("first"=>$array1,
"second"=>$array2);
And, if not, how should I do this?
Thanks in advance
your code is correct, in fact you could also do:
$key_array = array(
'first' => array('first' => $array1, 'second' => $array2),
'second' => array('first' => $arra1, 'second' => $array2)
);
and nest it as deep as you want.
How can I split a single array into it's sub-keys?
$arr = array(
0 => array(
'foo' => '1',
'bar' => 'A'
),
1 => array(
'foo' => '2',
'bar' => 'B'
),
2 => array(
'foo' => '3',
'bar' => 'C'
)
);
What is the most efficient way to return an array of foo and bar separately?
I need to get here:
$foo = array('1','2','3');
$bar = array('A','B','C');
I'm hoping there's a clever way to do this using array_map or something similar. Any ideas?
Or do I have to loop through and build each array that way? Something like:
foreach ($arr as $v) {
$foo[] = $v['foo'];
$bar[] = $v['bar'];
}
In a lucky coincidence, I needed to do almost the exact same thing earlier today. You can use array_map() in combination with array_shift():
$foo = array_map('array_shift', &$arr);
$bar = array_map('array_shift', &$arr);
Note that $arr is passed by reference! If you don't do that, then each time it would return the contents of $arr[<index>]['foo']. However, again because of the reference - you won't be able to reuse $arr, so if you need to do that - copy it first.
The downside is that your array keys need to be ordered in the same way as in your example, because array_shift() doesn't actually know what the key is. It will NOT work on the following array:
$arr = array(
0 => array(
'foo' => '1',
'bar' => 'A'
),
1 => array(
'bar' => 'B',
'foo' => '2'
),
2 => array(
'foo' => '3',
'bar' => 'C'
)
);
Update:
After reading the comments, it became evident that my solution triggers E_DEPRECATED warnings for call-time-pass-by-reference. Here's the suggested (and accepted as an answer) alternative by #Baba, which takes advantage of the two needed keys being the first and last elements of the second-dimension arrays:
$foo = array_map('array_shift', $arr);
$bar = array_map('array_pop', $arr);
$n = array();
foreach($arr as $key=>$val) {
foreach($val as $k=>$v) {
$n[$k][] = $v;
}
}
array_merge_recursive will combine scalar values with the same key into an array. e.g.:
array_merge_recursive(array('a',1), array('b',2)) === array(array('a','b'),array(1,2));
You can use this property to simply apply array_merge_recursive over each array in your array as a separate argument:
call_user_func_array('array_merge_recursive', $arr);
You will get this result:
array (
'foo' =>
array (
0 => '1',
1 => '2',
2 => '3',
),
'bar' =>
array (
0 => 'A',
1 => 'B',
2 => 'C',
),
)
It won't even be confused by keys in different order.
However, every merged value must be scalar! Arrays will be merged instead of added as a sub-array:
array_merge_recursive(array(1), array(array(2)) === array(array(1,2))
It does not produce array(array(1, array(2)))!
I am generating json from an array using json_encode(), it's working properly, but it uses the key:value from the array, as usual. but I want to change the name of the key only in the json output.. is it possible to do it ? or should I prepare the json key:values myself manually ?
Example:
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
O/P
{"a":1,"b":2,"c":3,"d":4,"e":5}
I want .
{"foo":1,"something":2,"bar":3,"foo":4,"baz":5}
edit : I cannot edit the original array..( generated using framweork)
Only if you rewrite 'm yourself. You could use:
$rewriteKeys = array('a' => 'foo', 'b' => 'something', 'c' => 'bar', 'd' => 'foo', 'e' => 'baz');
$newArr = array();
foreach($arr as $key => $value) {
$newArr[ $rewriteKeys[ $key ] ] = $value;
}
echo json_encode($newArr);
Not sure if that's what you were aiming for.
You can always json_decode it and then re-encode it. But it's going to be easiest if you simply prepare your keys before you encode it.
There is another option I described here. The main idea is to consider JSON as a string and then use str_replace or preg_replace(str_replace for regexp).
There is a code for your case.
$mapping_array = array('a' => 'foo', 'b' => 'something', 'c' => 'bar', 'd' => 'foo', 'e' => 'baz');
$tmp_arr = array_map(function($k){ return '/\b'.$k.'\b/u'; }, array_keys($mapping_array));
$new_json = preg_replace($tmp_arr, array_values($mapping_array), $old_json);
This may be a short answer:
foreach ($list as $key => $val) {
$newList[$key] = [
'foo' => $val['a'],
'something' => $val['b'],
'bar' => $val['c'],
'foo' => $val['d'],
'baz' => $val['e'],
];
}
Basically I want some function like array_as_php which is essentially the inverse of eval:
$array = array( '1' => 'b', '2' => 'c' );
echo array_as_php($array);
will print the following eval-able string:
array( '1' => 'b', '2' => 'c' )
var_export() is what you are looking for.
$array = array ('1' => 'b', '2' => 'c' );
echo var_export($array, true);