PHP create new array by value - php

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'
)
)
)

Related

how to add new key and value to multidimensional in PHP without using the loops....?

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
)
)
)

Remove or merge multidimensionals arrays

How to from array like this
[0] => Array
(
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
[1] => Array
(
[0] => Array
(
[SKU] => R
[title] => RT
[min] => XPads
)
[1] =>
)
)
make arrays like this:
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
[1] => Array
(
[0] => Array
(
[SKU] => R
[title] => RT
[min] => XPads
)
[1] =>
)
And how to array like this
[0] => Array
(
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
)
make array like this:
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
I only found soultions with removing multidimensionals arrays using value, but here im not having value, im having just key, any help?
array_splice and array_diff is php functions that i using to solve my problem from this website https://www.php.net but i didnt found the solution.
Removing first index can be achieve by using array_shift, see demo below.
<?php
//Enter your code here, enjoy!
$a = [
0 => [
0 => [
0 => [
'SKU' => 'T',
'title' => 'BT',
'min' => 'SPads',
],
1 => '',
],
[
0 => [
'SKU' => 'R',
'title' => 'RT',
'min' => 'XPads',
],
1 => '',
],
],
];
$a = array_shift($a);
print_r($a);
array_shift - Shift an element off the beginning of array
Output
Array
(
[0] => Array
(
[0] => Array
(
[SKU] => T
[title] => BT
[min] => SPads
)
[1] =>
)
[1] => Array
(
[0] => Array
(
[SKU] => R
[title] => RT
[min] => XPads
)
[1] =>
)
)
Demo.
You can iterate in foreach loop and create new array in reqired formate.
$newAr =array();
foreach ($oldArr as $tempArr){
foreach($tempArr as $tempArr2){
$newAr[] = $tempArr2;
}
}
print_r($newArr);

PHP array merge to subarray with keys

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;

trouble with array push php

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
)
)

How to combine multiple arrays with identical keys into a single array?

I am new to php. I need some help.
I had a array as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
Array ( [_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
so..whats my problem is i want an array with all these keys and values as
Array ( [_] => Array ( [0] => [1] => )
[123_] => Array ( [0] => 123 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
[_] => Array ( [0] => [1] => )
[12345_] => Array ( [0] => 12345 [1] => )
[1234_] => Array ( [0] => 1234 [1] => )
)
there would be duplicate keys and values.. but I want all of them as a array.. any help plz..
That is not possible. A PHP array cannot have two identical keys.
As the others said, it's impossible to have a single array with duplicate keys. But you can build an array of array :
<?php
$arr1 = array( '_' => Array ( '0' => '', '1' => ''),
'123_' => Array ( '0' => 123, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$arr2 = array ( '_' => Array ( '0' => '', '1' => ''),
'12345_' => Array ( '0' => 12345, '1' => ''),
'1234_' => Array ( '0' => 1234, '1' => '')
);
$result = array();
foreach( $arr1 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arr2 as $key => $val) {
$result[] = array('key'=>$key, 'value'=>$val);
}
print_r($result);
?>
Ouput:
Array
(
[0] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[1] => Array
(
[key] => 123_
[value] => Array
(
[0] => 123
[1] =>
)
)
[2] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
[3] => Array
(
[key] => _
[value] => Array
(
[0] =>
[1] =>
)
)
[4] => Array
(
[key] => 12345_
[value] => Array
(
[0] => 12345
[1] =>
)
)
[5] => Array
(
[key] => 1234_
[value] => Array
(
[0] => 1234
[1] =>
)
)
)
Have a look at PHP's array_merge()-function.

Categories