I have an array $minus
array(3) { [0]=> string(6) "people"
[1]=> string(7) "friends"
[2]=> string(8) "siblings"
}
And I have an array $user
array(3) { ["people"]=> string(3) "100"
["friends"]=> string(2) "10"
["siblings"]=> string(2) "57"
}
I can get the values of $user by using the values of $minus like,
echo $user[$minus[0]] . ', ' . $user[$minus[1]] . ', ' . $user[$minus[2]];
// Would echo: 100, 10, 57
But how can I get the values of $user by using the values of $minus into a new array, the new array should be like,
array(3) { [0]=> string(3) "100"
[1]=> string(2) "10"
[2]=> string(2) "57"
}
I have tried using foreach loops but can never get it right?
foreach($minus as $key=>$value) {
$new_array[$key] = $user[$value];
}
Use array_map, PHP >= 5.3 only
$new_array = array_map(function($item) use ($user) {return $user[$item];}, $minus);
$new_array= array();
foreach ($minus as $key => $value){
$new_array[$key] = $user[$value];
}
print_r($new_array);
$new_array = array($user[$minus[0]], $user[$minus[1]], $user[$minus[2]]);
$minus = array(0 => "people",
1 => "friends",
2 => "siblings"
);
$user = array("people" => "100",
"friends" => "10",
"siblings" => "57"
);
$newArray = $minus;
array_walk($newArray,function(&$item, $key, $prefix) { $item = $prefix[$item]; },$user);
var_dump($newArray);
Related
I have a PHP array, which looks like follows:
array(8) {
[0]=>
string(3) "639"
[1]=>
string(2) "33"
[2]=>
string(2) "68"
[3]=>
string(3) "196"
[4]=>
string(3) "275"
[5]=>
string(3) "309"
[6]=>
string(3) "331"
[7]=>
string(3) "378"
}
I would like to change all the keys to these values to incrementing letters (a, b, c, etc.) - how would I do this?
I realise I can increment letters like so:
$x = "a";
$x++;
echo $x;
"b"
But how can I perform this within a loop?
The desired result would be something like this:
"a" => "639"
"b" => "33"
"c" => "68"
etc.
I think the following can help
$newArray = array();
$index = "a";
foreach($oldArray as $value)
{
$newArray[$index] = $value;
$index++;
}
You have provided the answer pretty much yourself already
$array = array('639', '33', '68', '196', '275', '309', '331', '378');
$index = 'a';
$newArray = array();
foreach ($array as $value) {
$newArray[$index++] = $value;
}
Following code will surely help you:
$result = [];
array_walk($data,function($v,$k)use (&$result){
$result[chr(65 + $k)] = $v;
});
print_r($result);
Demo
I have this situation:
$qty = array(1) {[0]=> array(1) { ["qty"]=> string(5) "35254" }
$price = array(1) {[0]=> array(1) { ["price"]=> string(5) "1000" }
How can I get this?
$res = array(1) {[0]=> array(1) { ["qty"]=> string(5) "35254" ["price"]=> string(5) "1000"}
Thanks for the answers
May be it's that you want:
$res = array();
foreach($qty as $k => $v){
$res[$k] = array_merge($qty[$k],$price[$k]);
}
The result :
array(1) {[0] => array(2) { 'qty' => string(5) "35254" 'price' => string(4) "1000" } }
$qty = array("qty"=>"35254" );
$price = array ( "price"=> "1000" );
$combine = array_merge($qty,$price);
var_dump($combine);
try with
$res = array_merge_recursive($qty, $price);
print_r($res);
Not as pretty but with the same result.
$result = array_map(function ($e1,$e2) {
return array_merge_recursive($e1, $e2);
}, $qty,$price);
$result =
array(1) {
[0]=>
array(2) {
["qty"]=>
string(5) "35254"
["price"]=>
string(4) "1000"
}
}
and for indexed arrays
$a = ['a', 'b', 'c'];
$n = [1, 2, 3];
$result = array_map(function ($e1,$e2) {
return [$e1, $e2];
}, $a,$n);
$result = [
0 => ['a', 1],
1 => ['b', 2],
2 => ['c', 3]
];
$input = "hello|world|look|at|this";
$explode = explode("|", $input);
$array = array("Title" => "Hello!", "content" => $explode);
This will output:
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
[2]=>
string(4) "look"
[3]=>
string(2) "at"
[4]=>
string(4) "this"
}
}
But I want them to be keys with a NULL as value as I add values in a later step.
Any idea how to get the explode() function to return as keys? Is there a function from php available?
array_fill_keys can populate keys based on an array:
array_fill_keys ($explode, null);
Use a foreach loop on the explode to add them:
foreach($explode as $key) {
$array["content"][$key] = "NULL";
}
how about array_flip($explode)? That should give you this
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[hello]=> 1
No nullvalues but atleast you got the keys right
$input = "hello|world|look|at|this";
$explode = explode('|', $input);
$nulls = array();
foreach($explode as $x){ $nulls[] = null; };
$array = array("Title" => "Hello!", "content" => array_combine($explode, $nulls));
Played for hours, but couldn't do this. Task looks very simple, though..I need recursively combine 2 arrays into one. Using first array's values as keys, and second array's leave values as they are. This is what I have:
array(2) {
[0]=>
array(4) {
[0]=>
string(9) "First"
[1]=>
string(6) "Something"
}
[1]=>
array(4) {
[0]=>
string(3) "More"
[1]=>
string(6) "Nomore"
}
}
Second array
array(2) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
[1]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
What I'm trying to achive:
array(2) {
[0]=>
array(4) {
["First"]=>
string(1) "1"
["Something"]=>
string(1) "2"
}
[1]=>
array(4) {
["More"]=>
string(1) "1"
["Nomore"]=>
string(1) "2"
}
}
Another solution using array_combine
$first_array = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$second_array = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$new_array = array();
foreach($first_array AS $k => $v) {
$new_array[$k] = array_combine($v,$second_array[$k]);
}
$firstArray = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$secondArray = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$newArray = array();
for ($i=0; $i<count($firstArray); ++$i) {
$subArray1 = $firstArray[$i];
$subArray2 = $secondArray[$i];
$newArray[$i] = array();
for ($j=0; $j<count($subArray1); ++$j) {
$key = $subArray1[$j];
$value = $subArray2[$j];
$newArray[$i][$key] = $value;
}
}
var_dump($newArray);
Wouldn't be more elegant to do something like this ?
$newArray = array();
foreach ($firstArray as $key => $firstVal)
foreach ($secondArray as $key => $secondVal)
array_push($newArray, array_combine($firstVal, $secondVal));
This way you'll have the same result you wanted inside $newArray
with a bit simpler code.
I haven't tested that though, let me know if it works or breaks :)
So I'm suppose to build a multidimensional array dynamically from a text file, and everything works perfectly except that the numeric keys are screwing me over...
The text file looks something like this:
a=1
b.c=2
b.d.0.e=3
b.d.0.f=4
b.d.1.e=5
b.d.1.f=6
As the array_merge_recursive doesn't work with numeric keys, the output is like:
array(2) {
["a"]=>
string(3) "1"
["b"]=>
array(2) {
["c"]=>
string(3) "2"
["d"]=>
array(4) {
[0]=>
array(1) {
["e"]=>
string(9) "3"
}
[1]=>
array(1) {
["f"]=>
string(4) "4"
}
[2]=> array(1) {
["e"]=>
string(8) "5"
}
[3]=>
array(1) {
["f"]=>
string(9) "6"
}}}}
Is there any easy solution to make the output like...?
array(2) {
["a"]=>
string(3) "1"
["b"]=>
array(2) {
["c"]=>
string(3) "2"
["d"]=>
array(2) {
[0]=>
array(2) {
["e"]=>
string(9) "3"
["f"]=>
string(4) "4"
}
[1]=>
array(3) {
["e"]=>
string(9) "5"
["f"]=>
string(4) "6"
}}}}
Thanks
You could break each bit into its components and build up the array one step at a time.
$path = "b.d.0.e";
$val = 3;
$output = array();
$parts = explode(".", $path);
// store a pointer to where we currently are in the array.
$curr =& $output;
// loop through up to the second last $part
for ($i = 0, $l = count($parts); $i < $l - 1; ++$i) {
$part = $parts[$i];
// convert numeric strings into integers
if (is_numeric($part)) {
$part = (int) $part;
}
// if we haven't visited here before, make an array
if (!isset($curr[$part])) {
$curr[$part] = array();
}
// jump to the next step
$curr =& $curr[$part];
}
// finally set the value
$curr[$parts[$l - 1]] = $val;
My output, using the same input as yours:
Array (
[a] => 1
[b] => Array (
[c] => 2
[d] => Array (
[0] => Array (
[e] => 3
[f] => 4
)
[1] => Array (
[g] => 5
[h] => 6
)
)
)
)
Or you could use eval():
$raw_data = file($txt_file, FILE_IGNORE_NEW_LINES);
foreach ($raw_data as $line) {
list($keys, $value) = explode('=', $line);
$keys = explode('.', $keys);
$arr_str = '$result';
foreach ($keys as $key) {
if (ctype_digit($key)) {
$arr_str .= "[" . $key . "]";
} else {
$arr_str .= "['" . $key . "']";
}
}
eval($arr_str . ' = $value;');
}
print_r($result);
I know this is an old one, but the best solution I have found is to use array_replace_recursive. It will achieve what you are looking to do:
$start = array(
"600" => array("total" => 100),
"700" => array("total" => 200)
);
$finish = array(
"600" => array("average" => 25),
"700" => array("average" => 50)
);
$out = array_replace_recursive($start,$finish);
var_dump($out):
array(2) {
[600]=>
array(2) {
["total"]=>
int(100)
["average"]=>
int(25)
}
[700]=>
array(2) {
["total"]=>
int(200)
["average"]=>
int(50)
}
}