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);
Related
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
I have an array that I need to get a value from within the same array that is unassigned to a variable:
return ['a' => 1, 'b'=> 'a', 'c' => 2];
So in this case I need 'b' to return the same value as 'a'. Which would be 1
Thanks for the help.
edit
I intend on running a function on b's value so the value of b is slightly different than a
return ['a' => 1, 'b'=> myFunction('a'), 'c' => 2];
You can try this way.
foreach ($array as $key => $agent) {
$array[$key]['agent_address_1'] = $agent['agent_company_1'] . ', ' .$agent['agent_address_1'];
unset($array[$key]['agent_company_1']);
}
What you want is not clear.
But i am assuming that you are trying to get the 'b' element of an array to be assigned a value similar to the value of 'a' element of that same array
If that is what you need, this will do it.
<?php
$a = array('a' => 1, 'b' => null, 'c' => 2);
$a['b'] = myFunction($a, 'a');
function myFunction($a, $b)
{
return $a[$b];
}
var_dump($a);
You can then return the array, or do what you want with it.
Maybe something like
<?php
function resolve(array $arr) {
foreach($arr as &$v) {
if ( isset($arr[$v])) {
$v = $arr[$v];
}
}
return $arr;
}
function foo() {
return resolve( ['a' => '5', 'b'=>'a', 'c' => '1'] );
}
var_export( foo() );
will do, prints
array (
'a' => '5',
'b' => '5',
'c' => '1',
)
But keep in mind that resolve( ['b'=>'a', 'a' => 'c', 'c' => '1'] ); will return
array (
'b' => 'c',
'a' => '1',
'c' => '1',
)
(you could resolve that with while( isset($arr[$v])) { instead of if ( isset($arr[$v]) ) { ...but there are most likely more elegant/performant ways to do that)
I have an array like this:
$a = array(
array(
'a' => 'x',
'b' => 'asdasd',
),
array(
'a' => 'f',
'b' => '123123qwe',
),
);
And I expect an array like this:
$a = array(
'asdasd',
'123123qwe',
);
I can transform this by iterating and filling a new array,
I wonder if I can do this in 1 line without temporary variables?
Update: Using PHP 5.3 , thanks for the 5.5 suggestions tho!
If you're using PHP 5.5 you can use array_column():
$new_array = array_column($a, 'b');
See it in action
Old school method (pre 5.5):
<?php
function simplify($el)
{
return $el['b'];
}
$a = array(
array(
'a' => 'x',
'b' => 'asdasd',
),
array(
'a' => 'f',
'b' => '123123qwe',
),
);
$a = array_map('simplify',$a);
echo '<pre>'.print_r($a,true).'</pre>';
5.3 method with anonymous function:
$a = array_map(function($el){return $el['b'];},$a);
echo '<pre>'.print_r($a,true).'</pre>';
In php is there a way to get an element from each sub array without having to loop - thinking in terms of efficiency.
Say the following array:
$array = array(
array(
'element1' => a,
'element2' => b
),
array(
'element1' => c,
'element2' => d
)
);
I would like all of the 'element1' values from $array
There are a number of different functions that can operate on arrays for you, depending on the output desired...
$array = array(
array(
'element1' => 'a',
'element2' => 'b'
),
array(
'element1' => 'c',
'element2' => 'd'
)
);
// array of element1s : array('a', 'c')
$element1a = array_map(function($item) { return $item['element1']; }, $array);
// string of element1s : 'ac'
$element1s = array_reduce($array, function($value, $item) { return $value . $item['element1']; }, '');
// echo element1s : echo 'ac'
array_walk($array, function($item) {
echo $item['element1'];
});
// alter array : $array becomes array('a', 'c')
array_walk($array, function(&$item) {
$item = $item['element1'];
});
Useful documentation links:
array_map
array_reduce
array_walk
You can use array_map.
Try code below...
$arr = $array = array(
array(
'element1' => a,
'element2' => b
),
array(
'element1' => c,
'element2' => d
)
);
print_r(array_map("getFunc", $arr));
function getFunc($a)
{
return $a['element1'];
}
See Codepad.
But I think array_map will also use loop internally.
If you're running PHP 5.5 (currently the beta-4 is available), then the following
$element1List = array_column($array, 'element1');
should give $element1List as an simple array of just the element1 values for each element in $array
$array = array(
array(
'element1' => a,
'element2' => b
),
array(
'element1' => c,
'element2' => d
)
);
$element1List = array_column($array, 'element1');
print_r($element1List);
gives
Array
(
[0] => a
[1] => c
)
Without loop? Recursion!
$array = array(
array(
'element1' => 'a',
'element2' => 'b'
),
array(
'element1' => 'c',
'element2' => 'd'
)
);
function getKey($array,$key,$new = array()){
$count = count($array);
$new[] = $array[0][$key];
array_shift($array);
if($count==1)
return $new;
return getKey($array,$key,$new);
}
print_R(getKey($array,'element1'));
As I understood from Wikipedia Recursion is not a loop.
Say I have the following code:
$arr = array('id' => $tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam'));
echo '' . implode( ', ', $tarr) . '';
This displays: Fred, Wilma, Bam Bam
but the href shows value Array instead of Fred for Fred, Wilma for Wilma etc
Cheers
You can build an output string (or array as shown here) using a foreach loop:
foreach($tarr as $v){
$out[] = "<a href='?tag=$v'>$v</a>";
}
echo implode(', ', $out)
I think what youy are trying to do is this:
$arr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam');
echo '';
$tarr is an array, so when it's converted to a string, it prints Array.
Don't use implode here, you should use a for loop to get each value of the array.
What you should do is something like this:
$tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam');
$aTags = array();
foreach($tarr as $v){
$aTags[] = ''.$v.'';
}
echo implode(', ', $aTags);
Also, why do you have $arr here? It's totally useless.
$arr = array('id' => $tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam'));
This is the same as:
$tarr = array('1' => 'Fred', '2' => 'Wilma', 'c' => 'Bam Bam');
$arr = array('id' => $tarr);