i want to get multiple value of multi array to put in one array
i creat this code but is doesn't work
it just push the last value the full array
the result must be
$rest=Array ( [OPEN] => Array ( ), [HAPPY] => Array ( ) , [ALIVE] => Array ( ) ,[GOOD] => Array ( ) )
<?
error_reporting(0);
$arr=array(
0 => array(
'OPEN' => 'understanding',
'HAPPY' => 'great',
'ALIVE' => 'playful',
'GOOD' => 'calm'
),
1 => array(
'OPEN' => 'confident',
'HAPPY' => 'gay',
'ALIVE' => 'courageous',
'GOOD' => 'peaceful'
),
2 => array(
'OPEN' => 'reliable',
'HAPPY' => 'joyous',
'ALIVE' => 'energetic',
'GOOD' => 'at ease'
));
$feeling = array();
$x=0;
foreach ($arr[0] as $key => $value) {
$rest = array_merge($feeling,array("$key" => array()));
}
//$rest=Array ( [OPEN] => Array ( ) ) Array ( [HAPPY] => Array ( ) ) Array ( [ALIVE] => Array ( ) ) Array ( [GOOD] => Array ( ) )
for ($i=0; $i <count($arr) ; $i++) {
foreach ($arr[$i] as $key => $value) {
array_push($rest[$key], $arr[$i][$key]);
}
}
print_r($rest);
?>
-----
result what is give me
-----
Array
(
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
[OPEN] =>
[HAPPY] =>
[ALIVE] =>
)
----
i want is like that
----
Array
(
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
[OPEN] =>Array
(
[0] => understanding
[1] => confident
[2] => reliable
)
[HAPPY] =>Array
(
[0] => great
[1] => gay
[2] => joyous
)
[ALIVE] =>Array
(
[0] => playful
[1] => courageous
[2] => energetic
)
)
I believe this is returning the desired output?
$arr=array(
0 => array(
'OPEN' => 'understanding',
'HAPPY' => 'great',
'ALIVE' => 'playful',
'GOOD' => 'calm'
),
1 => array(
'OPEN' => 'confident',
'HAPPY' => 'gay',
'ALIVE' => 'courageous',
'GOOD' => 'peaceful'
),
2 => array(
'OPEN' => 'reliable',
'HAPPY' => 'joyous',
'ALIVE' => 'energetic',
'GOOD' => 'at ease'
)
);
$output=array();
$keys=array_keys( $arr[0] );
for( $i=0; $i < count( $arr ); $i++ ){
foreach( $keys as $key )$output[$key][]=$arr[$i][$key];
}
echo '<pre>', print_r($output,true), '</pre>';
The result:
Array
(
[OPEN] => Array
(
[0] => understanding
[1] => confident
[2] => reliable
)
[HAPPY] => Array
(
[0] => great
[1] => gay
[2] => joyous
)
[ALIVE] => Array
(
[0] => playful
[1] => courageous
[2] => energetic
)
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
)
If I don't understood your requirement then this solution will work for you.I used the array_column() approach because your current code with for() and foreach() loop seems little bit messy.
$arr=array(
0 => array(
'OPEN' => 'understanding',
'HAPPY' => 'great',
'ALIVE' => 'playful',
'GOOD' => 'calm'
),
1 => array(
'OPEN' => 'confident',
'HAPPY' => 'gay',
'ALIVE' => 'courageous',
'GOOD' => 'peaceful'
),
2 => array(
'OPEN' => 'reliable',
'HAPPY' => 'joyous',
'ALIVE' => 'energetic',
'GOOD' => 'at ease'
));
$expected = [];
$keys = array_keys($arr[0]);
foreach($keys as $key){
$expected[$key] = array_column($arr,$key);
}
print '<pre>';
print_r($expected);
print '</pre>';
Output:
Array
(
[OPEN] => Array
(
[0] => understanding
[1] => confident
[2] => reliable
)
[HAPPY] => Array
(
[0] => great
[1] => gay
[2] => joyous
)
[ALIVE] => Array
(
[0] => playful
[1] => courageous
[2] => energetic
)
[GOOD] => Array
(
[0] => calm
[1] => peaceful
[2] => at ease
)
)
Related
I have two arrays in PHP:
Array 1
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => incomplete
)
[1] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => incomplete
)
[2] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[3] => Array
(
[autoid] => yc5qcwg2
[task] => Trillian
[progress] => incomplete
)
)
Array 2
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => complete
)
[1] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[2] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => pending
)
)
I need to:
Step through Array 1, and check if the autoid exists in Array 2.
If it does exist, update the progress field to match.
So the ideal output when using the arrays above would be:
Array 3
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => complete
)
[1] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => pending
)
[2] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[3] => Array
(
[autoid] => yc5qcwg2
[task] => Trillian
[progress] => incomplete
)
)
I am not sure how best to go about this, if anyone has any thoughts or pointers that would be awesome.
First change the arr2 so its index is the autoid then you can use it easily to get the progress value.
Then just foreach over arr1 and apply changes to progress if they exist
$arr1 = [
['autoid' => 't35wmkbg','task' => 'Zaphod','progress' => 'incomplete'],
['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'incomplete'],
['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
['autoid' => 'yc5qcwg2', 'task' => 'Trillian', 'progress' => 'incomplete']
];
$arr2 = [
['autoid' => 't35wmkbg', 'task' => 'Zaphod', 'progress' => 'complete'],
['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'pending']
];
#first make arr2 indexable on the autoid
foreach ($arr2 as $a) {
$arr2search[$a['autoid']] = $a;
}
foreach ($arr1 as $a){
// do we have an autoid in arr2
if ( isset($arr2search[$a['autoid']])){
$a['progress'] = $arr2search[$a['autoid']]['progress'];
}
$merged[] = $a;
}
print_r($merged);
RESULT
Array
(
[0] => Array
([autoid] => t35wmkbg, [task] => Zaphod, [progress] => complete)
[1] => Array
([autoid] => cwg2yc5q, [task] => Arthur, [progress] => pending )
[2] => Array
([autoid] => 85q4bcmy, [task] => Ford, [progress] => incomplete )
[3] => Array
([autoid] => yc5qcwg2, [task] => Trillian, [progress] => incomplete )
)
Array ( [0] => Array (
[date] => 01-06-2018
[nav] => 30.65100 )
[1] => Array (
[date] => 31-05-2018
[nav] => 30.84900 )
[2] => Array (
[date] => 30-05-2018
[nav] => 30.73200 )
[3] => Array (
[date] => 29-05-2018
[nav] => 30.81500 )
The above code is the Multi-array, we have added a common id like id_code = 0089 to every array in it without using any loops in PHP. Can anyone helps me and it is possible or not .....?
If you mean not manually loop through the array then yes, it is possible using array_walk:
$array = [
0 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
1 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
2 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
3 => [
"date" =>"01-06-2018",
"nav" => "30.65100"]
];
array_walk($array, function(&$item1) {
$item1['id_code'] = "0089";
});
print_r($array);
Output:
Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[1] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[2] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[3] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
)
Demo https://3v4l.org/lCGIO
An effective solution will be to use array_map function as:
$keyValue = 'some value';
$data = array_map(function($d) use ($keyValue){
return $d + ['keyName' => $keyValue];
}, $data);
I think this is the closer you can get, but it is probably not your expected result. The exact result you need cannot be done without using a loop as far as I know.
$t = array( 0 => array( 'date' => '01-06-2018', 'nav' => '30.65100' ), 1 => array( 'date' => '31-05-2018', 'nav' => '30.84900' ), 2 => array( 'date' => '30-05-2018', 'nav' => '30.73200' ), 3 => array( 'date' => '29-05-2018', 'nav' => '30.81500' ));
$tt = array( 0 => array( 'id' => '648'), 1 => array( 'id' => '332'), 2 => array( 'id' => '889'), 3 => array( 'id' => '285') );
$final = array_map(null, $t, $tt);
print_r($final);
The output will look like
Array
(
[0] => Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
)
[1] => Array
(
[id] => 648
)
)
[1] => Array
(
[0] => Array
(
[date] => 31-05-2018
[nav] => 30.84900
)
[1] => Array
(
[id] => 332
)
)
[2] => Array
(
[0] => Array
(
[date] => 30-05-2018
[nav] => 30.73200
)
[1] => Array
(
[id] => 889
)
)
[3] => Array
(
[0] => Array
(
[date] => 29-05-2018
[nav] => 30.81500
)
[1] => Array
(
[id] => 285
)
)
)
Assuming I have an array and recursively want to do some task. I have spent one day for this unable to solve this. May be my logic is not good.
Any help on how to do such thing in an efficient way would save my days.
I have an array with n level deep, looking like:
Array
(
[0] => Array
(
[name] => user1
[email] => user1#demo.com
[depth] => 1
)
[1] => Array
(
[name] => user2
[email] => user2#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user2.1
[email] => user2.1#demo.com
[depth] => 2
)
)
)
[2] => Array
(
[name] => user3
[email] => user3#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user3.1
[email] => user3.1#demo.com
[depth] => 2
[0] => Array
(
[0] => Array
(
[name] => user3.1.1
[email] => user3.1.1#demo.com
[depth] => 3
)
)
)
[1] => Array
(
[name] => user3.2
[email] => user3.2#demo.com
[depth] => 2
)
)
)
)
I want to change above array in exactly this format:
array(
0 => array(
'name' => 'user1',
),
1 => array(
'name' => 'user2',
'children' => array(
0 => array(
'name' => 'user2.1',
) ,
) ,
) ,
2 => array(
'name' => 'user3',
'children' => array(
0 => array(
'name' => 'user3.1',
'children' => array(
0 => array(
'name' => 'user3.1.1',
) ,
) ,
) ,
1 => array(
'name' => '3.2',
)
) ,
) ,
)
Edited:
I am using this code and working fine if i want to show data in tree format but unable to push data into array as i want.
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
displayArrayRecursively($value, $indent . '-->');
} else {
if ($key == 'name')
{
echo "$indent $value <br>";
}
else {
continue;
}
}
}
}
}
displayArrayRecursively($userListArray);
Can anyone suggest me how to do this?
Thank you
This function will do what you want:
function process_nodes($nodes) {
$new = array();
foreach ($nodes as $node) {
$new[] = array('name' => $node['name']);
if (isset($node[0])) {
$new[count($new)-1]['children'] = process_nodes($node[0]);
}
}
return $new;
}
print_r(process_nodes($data));
Output:
Array
(
[0] => Array
(
[name] => user1
)
[1] => Array
(
[name] => user2
[children] => Array
(
[0] => Array
(
[name] => user2.1
)
)
)
[2] => Array
(
[name] => user3
[children] => Array
(
[0] => Array
(
[name] => user3.1
[children] => Array
(
[0] => Array
(
[name] => user3.1.1
)
)
)
[1] => Array
(
[name] => user3.2
)
)
)
)
I have a problem with merging array's. I'll try array merge and combine but nothing is working.
First array
Array (
[type1] => Array (
[userid] => Array (
[0] => 35
[1] => 37
)
[from] => Array (
[0] => 07-06-2017
[1] => 09-06-2017
)
[till] => Array (
[0] => 07-07-2017
[1] => 09-07-2017
)
)
[type3] => Array (
[userid] => Array (
[0] => 13
)
[from] => Array (
[0] => 10-06-2017
)
[till] => Array (
[0] => 10-07-2017
)
)
)
Second array
The second array is filled with room details, but the assigned users are not added yet.
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
)
I'll will put these arrays together to one array, so that the data is insert into the "rented_to" section. How can I get this array into an another array like this:
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 35
[from] => 07-06-2017
[till] => 07-07-2017
)
[1] => Array
(
[userid] => 37
[from] => 09-06-2017
[till] => 09-07-2017
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 13
[from] => 10-06-2017
[till] => 10-07-2017
)
)
)
)
The code
$manager_add_new_room_rented_to is an array that contains the value of the first array in my example.
$manager_add_new_room_type_desc = $_POST['manager_add_new_room_type_desc'];
$manager_add_new_room_type_m2 = $_POST['manager_add_new_room_type_m2'];
$manager_add_new_room_type_rent_price = $_POST['manager_add_new_room_type_rent_price'];
$manager_add_new_room_type_rooms = $_POST['manager_add_new_room_type_rooms'];
$manager_add_new_room_type_extra = $_POST['manager_add_new_room_type_extra'];
$manager_add_new_room_rented_to = $_POST['manager_add_new_room_rented_to'];
$add_new_room_information = array();
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => array(
array(
'userid' => '',
'from' => '',
'till' => ''
)
)
);
endforeach;
Loop through $manager_add_new_room_rented_to[$room_type] and create the new array that you want.
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$renters = $manager_add_new_room_rented_to[$room_type];
$rented_to = array();
foreach ($renters['userid'] as $index => $userid) :
$rented_to[] = array('userid' => $userid, 'from' => $renters['from'][$index], 'to' => $renters['to'][$index]);
endforeach;
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => $rented_to
)
);
endforeach;
I have an array like this...
Array
(
[0] => Array
(
[path] => Array
(
[0] => level_1
)
[name] => 'Computer'
)
[1] => Array
(
[path] => Array
(
[0] => level_1
[1] => level_2
[2] => level_3
)
[name] => 'Laptop'
)
[2] => Array
(
[path] => Array
(
[0] => level_1
[1] => level_2
)
[name] => 'Smartphone'
)
)
and I want to create an new array like this...
Array(
[level_1] => Array(
[0] => 'Computer'
[level_2] => Array(
[0] => 'Smartphone',
[level_3] => Array(
[0] => 'Laptop'
)
)
)
)
Is there a native PHP way to do this?
I'm not a PHP wizard, Thanks!
<?php
$techArray = array(
"level_1" => array (
"0" => "Computer",
"level_2" => array (
"0" => "SmartPhone",
"level_3" => array(
"0" => "Laptop"
)
)
)
);
?>
You can try this, it will work!
thanks for your tips.
level_1, level_2 and level_3 is a synonym for a path (parent / child).
an example:
['path'] => array ([0] => red[1] => blue[2] => yellow)
what will be the output you expect?
an array in which the keys are formed from the values of the path array, like
array(
['red'] => array(
[0] => 'computer'
['blue'] => array(
[0] => 'smartphone',
['yellow'] => array(
[0] => 'laptop'
)
)
)