I have this array:
ARRAY 1
Array
(
[0] => Array
(
[required] => Tip:
)
[1] => Array
(
[required] => Flux:
)
[2] => Array
)
ARRAY 2
Array
(
[0] => Array
(
[name] => Flux:
)
[1] => Array
(
[name] => Tip:
)
[2] => Array
(
[name] => Weight:
)
)
How insert data from array 1 to array 2 when value of name and required are same.
Data from array some time don't have same values of name and required.
On output of array 2 I want to display something when value of required exist.
I need this:
Array
(
[0] => Array
(
[name] => Flux:
[required] => Flux:
)
[1] => Array
(
[name] => Tip:
[required] => Tip:
)
[2] => Array
(
[name] => Weight:
[required] =>
)
)
I tried with this:
foreach($char as $key =>$value){
foreach($mandatory as $key =>$val){
$data[] = array("name" => $value->charact_name, "required" => $val);
}
}
But is not output what I expect! It show me diferent values on name and required
This question is not a technical problem and you needed a little more tries to achieve a true algorithm.
I did some modifications to your code. $key variable must be different for two foreachs. Based on your question new key required just added to $arr2.
$arr1 = [['required' => 'Tip:'], ['required' => 'Flux:']];
$arr2 = [['name' => 'Flux:'], ['name' => 'Tip:'], ['name' => 'Weight:']];
foreach($arr2 as $key2 => $value2) {
$arr2[$key2]['required'] = NULL;
foreach($arr1 as $value1)
if($value2['name'] == $value1['required']) {
$arr2[$key2]['required'] = $value1['required'];
break;
}
}
I tried this:
foreach($char as $key2 => $value2) {
foreach($mandatory as $value1)
if($value2->charact_name == $value1) {
$data[] = array("name" => $value2->charact_name, "required" => $value1);
}
}
But data array it show me name and required only for values that is in $mandatory array, I want to display all from $char and when value exist in $mandatory add [required]=> name of value from $mandatory
You can simply create it with nested loops which cause time complexity O(n*m). However if you use a Hashmap (array with key value) which get O(1) to search you can create it very fast by O(n+m) as below:
<?php
$arr1 = array(0 => array('name' => 'Flux'),1 => array('name' => 'Tip'),2 => array('name' => 'Weight'));
$arr2 = array(0 => array('required' => 'Tip'),1 => array('required' => 'Flux'),2 => array());
$arrkey = array();
foreach ($arr1 as $ix=>$value) {
$arrkey[$value['name']]=$ix;
}
$arr3 = $arr1;
foreach ($arr2 as $elem) {
if(!empty($elem) && array_key_exists($elem['required'],$arrkey)) {
$arr3[$arrkey[$elem['required']]]['required'] = $elem['required'];
}
}
print_r($arr3);
?>
Try this online demo.
Related
I've been stuck on this for the better part of the day and I'm out of ideas. I have an array like this:
Array
(
[rank] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[name] => Array
(
[0] => 'Hideki'
[1] => 'Rory'
[2] => 'Sam'
[money] => Array
(
[0] => '$100'
[1] => '$200'
[2] => '$500'
)
)
and I have the task to create an array with the following format from it:
Array
(
[Hideki] => Array
(
[rank] => 1
[money] => '$100'
)
[Rory] => Array
(
[rank] => 2
[money] => '$200'
[Sam] => Array
(
[rank] => 3
[money] => '$500'
)
)
The catch is that 'rank' and 'money' have to be dynamic names
It should be simple as that:
$new = [];
foreach($array['name'] as $key => $name) {
$new[$name] = [
'rank' => $array['rank'][$key],
'money' => $array['money'][$key]
];
}
Little late but here my answear. My approach was to use the array_walk() function.
$array = [
'rank' => [1,2,3],
'name' => ['Hideki', 'Rory', 'Sam'],
'money' => ['$100', '$200', '$500'],
];
$i = 0;
$newArray = [];
array_walk($array['name'], function($name) use (&$i, $array, &$newArray) {
$newArray[$name] = ['rank'=> $array['rank'][$i], 'money' => $array['money'][$i]];
$i++;
});
print_r($newArray);
Run your first array through a foreach loop referencing only the "name" key and using key=>value pairs. Then reference the other keys from the first array when you build the new array, setting the value as the key to the second array.
You will need to first get the keys using array_keys() and use a nested foreach to loop through all the keys.
Example:
$keys1 = array_keys($array1);
foreach ($array1['name'] as $key => $value) {
$val2 = array();
foreach ($keys1 as $k){
if ($k != 'name') $val2[$k] = $array1[$k][$key];
}
$array2[$value] = $val2;
}
I have an array like below: all the values I am getting one array only, but I don't want this way. This is the best way to do so, in php or jQuery both languages are ok for me
Array
(
[0] => Array
(
[val] => facebook
)
[1] => Array
(
[val] => snapchat
)
[2] => Array
(
[val] => instagram
)
[3] => Array
(
[expenses] => 986532
)
[4] => Array
(
[expenses] => 45456
)
[5] => Array
(
[expenses] => 56230
)
[6] => Array
(
[social_id] => 15
)
[7] => Array
(
[social_id] => 16
)
[8] => Array
(
[social_id] => 17
)
)
and I want to output like this..
$result = array(
array(
"val" => "Facebook",
"expenses" => "84512",
"social_id" => 1
),
array(
"val" => "Instagram",
"expenses" => "123",
"social_id" => 2
)
);
but this should be dynamic, the length of the above array can be varies
You can merge the arrays to one and use array_column to get all vals or expenses in a separate array.
Then foreach one array and build the new array with the key.
//$a = your array
// Grab each column separate
$val = array_column($a, "val");
$expenses= array_column($a, "expenses");
$social_id = array_column($a, "social_id");
Foreach($val as $key => $v){
$arr[] = [$v, $expenses[$key], $social_id[$key]];
}
Var_dump($arr);
https://3v4l.org/nVtDf
Edit updated with the 'latest' version of the array. My code works just fine with this array to without any changes.
to get that array style you can do it by hand for each array
function test(array ...$arr)
{
$result = array();
foreach ($arr as $key => $pair) {
foreach ($pair as $item => $value) {
$result[$item] = $value;
}
}
return $result;
}
$arr1 = test( ['facebook' => 1], ['expenses' => 100]);
print_r($arr1);
/* Array (
[facebook] => 1,
[expenses] => 100
)
*/
$arr2 = test( $arr1, ['more info' => 'info'] );
print_r($arr2);
/* Array (
[facebook] => 1,
[expenses] => 100,
[more info] => info
)
*/
you can pass it any number of
['Key' => 'Pair']
array('Key' => 'Pair')
or even a previous made array and it will add them up into 1 big array and return it
I have this array. I want to extract to a new one array the content of only one of this array ([choices]). How can i do it in php?
Array
(
[0] => Array
(
[key] => field_54df3275b708a
[label] => Idioma
[name] => idioma
[type] => select
[instructions] =>
[required] => 0
[choices] => Array
(
[Catalan] => Catalan
[Castellano] => Castellano
[Ingles] => Ingles
)
[default_value] =>
[allow_null] => 0
[multiple] => 0
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => null
[operator] => ==
[value] =>
)
)
[allorany] => all
)
[order_no] => 0
)
)
I try this code (i think that's its a bad code):
foreach($array as $k=>$v){
if (is_array($v)){
foreach($v as $l=>$w){
if ($w){
foreach($w as $s=>$t){
$idiomas[]=$t.'<br />';
}
}
}
}
}
But it saves [choices] and [conditional_logic] to new array, and i only want [choices]
Thankyou so much
$new_array_choices = $array[0]['choices'];
Took me awhile to create a test code fiddle. I had to manually recreate the array.
Since you take the key in every foreach all the time, you can use it to ensure it's the array you want :
foreach($array as $k=>$v) {
if (is_array($v)) {
foreach($v as $l=>$w) {
if ($w && $l == 'choices') { // $w is the wanted array
foreach($w as $s=>$t) {
$idiomas[]=$t.'<br />';
}
}
}
}
}
I'm not sure what you are testing with if ($w) though
Replace $arr with your actual array and it should work nonetheless.
// your array (replace)
$arr = array(
"required" => 0,
"choices" => array(
"Catalan" => "Catalan",
"Castellano" => "Castellano",
"Ingles" => "Ingles",
),
"default" => NULL,
);
// the empty resulting array
$new_arr = array();
foreach($arr["choices"] as $key)
array_push($new_arr, $arr["choices"][$key]);
// your resulting array
print_r($new_arr);
Your desired array is saved in the array $new_arr.
I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)
Hello I have POST array that looks like this,
Array (
[email_address] => Array (
[0] => simon#simonainley.info
[1] => simon2#simonainley.info
)
[firstname] => Array (
[0] => Simon
[1] => Simon2
)
[surname] => Array (
[0] => Ainley
[1] => Ainley2
)
[companies_company_id] => NULL,
[save_user] => Save User
)
I wanting to create an new array where I would get the first email_address, firstname, and surname into an array, deal with that data and then proceed to the next email-address, firstname and surname.
Is this possible? I have tried this code,
$newArray = array();
foreach($_POST as $key => $value) {
$newArray[] = $value;
}
however that code just produces this,
Array (
[0] => Array (
[0] => simon#simonainley.info
[1] => simon2#simonainley.info
)
[1] => Array (
[0] => Simon
[1] => Simon2
) [2] => Array ( [0] => Ainley [1] => Ainley2 ) [3] => [4] => Save User ) 1
What do I need to do?
$count = count($_POST['firstname']);
$result = array();
for ($i = 0; $i <= $count; $i ++) {
$result[] = array(
'email_address' => $_POST['email_address'][0],
'firstname' => $_POST['firstname'][0],
'lastname' => $_POST['lastname'][0]
);
}
or (if the numeric indices have any meaning)
$result = array();
foreach (array_keys($_POST['email_address']) as $index) {
$result[$index] = array(
'email_address' => $_POST['email_address'][$index],
'firstname' => $_POST['firstname'][$index],
'lastname' => $_POST['lastname'][$index]
);
}
You could try:
foreach($_POST as $key=>$value)
{
$count=0;
foreach($value as $val)
{
$newArray[$count++]=Array($key=>$val);
}
}
You only need to re-order the elements into the $_POST array:
$users = array();
foreach($_POST as $key => $values) {
if (is_array($values)) {
foreach($values as $index => $value) {
$users[$index][$key] = $value;
}
}
}
With the data you provided, it will give you this in the $users array:
[0] => Array
(
[email_address] => simon#simonainley.info
[firstname] => Simon
[surname] => Ainley
)
[1] => Array
(
[email_address] => simon2#simonainley.info
[firstname] => Simon2
[surname] => Ainley2
)
Elements in $_POST that are not an array are ignored and filtered out.