Making array into multidimensional array with same key => value [duplicate] - php

This question already has answers here:
Create an assoc array with equal keys and values from a regular array
(3 answers)
Closed 4 years ago.
I have an array like this
$arr = ['Hello', 'World'];
What I am trying to achieve is
$arr [
'Hello' => 'Hello',
'World' => 'World'
]
Is there a array method to achieve this or should I run a foreach loop and do it manually? I am just thinking if there is a more elegant way

You could use array_combine for this
$new = array_combine($arr, $arr);
print_r($new);

Related

in PHP how to change the key of an array with tke key of a nested array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 2 years ago.
I have an array with nested associative array for each element, like this:
array (size=47)
0 =>
array (size=1)
'name' => 'Saitama'
1 =>
array (size=1)
'email' => 'Saitama#onepunch.man'
...
I want to build a function that return an associative array like this:
array (size=47)
'name' => 'Saitama',
'email' => 'Saitama#onepunch.man'
...
I tried with array_map() and array_combine() but I cannot manage to do this job.
Thank you very much
Pasquale
It's called flattening and just merge the nested arrays:
$result = call_user_func_array('array_merge', $array);
Obviously this only works with unique keys, as duplicates will be overwritten.

How to join items of multidimensional array using php? [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 8 months ago.
I have an array as shown bottom
array (
[det1] => 1,2,3,4
[det2] => 5,6
);
So i want to join items of this array and convert array to an string like bottom
$uru = 1,2,3,4,5,6
How can i do this work?
Did you try like this with implode()
<?php
$arr = array('det1'=>'1,2,3,4', 'det2'=>'5,6');
$uru = implode(',',$arr);
echo $uru;
?>
DEMO: https://3v4l.org/UBQrv
You say multidimensional array, but your array is single dimensional with strings?
I assume that is a typo and your array is multidimensional.
In that case loop the array and merge the new array with the subarray.
$arr = array (
"det1" => [1,2,3,4],
"det2" => [5,6]
);
$new= [];
foreach($arr as $sub){
$new = array_merge($new, $sub);
}
echo implode(",",$new); // 1,2,3,4,5,6
https://3v4l.org/NaDXN
This can be easily done by extracting into array_merge and then joining the resulting array.
$data = [
[1,2,3,4,5],
[6,7,8],
];
echo join(',', array_merge(... $data));
Output:
1,2,3,4,5,6,7,8

How to change array indexes in php? [duplicate]

This question already has answers here:
How to reindex an array?
(6 answers)
Closed 6 years ago.
array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
If delete second element, you have
array(
[0]=>1
[2]=>3
[3]=>4
)
Now how to change indexes to
array(
[0]=>1
[1]=>3
[2]=>4
)
If you want to re-index your array starting to zero, simply do the following:
$myNewArray = array_values($myOldArry);
From the php docs:
http://php.net/manual/en/function.array-splice.php
array_splice: Removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied.
So, you would use:
$arr = array(1,2,3,4);
array_splice($arr, 2, 1); //Will give you an array: [1, 2, 4]

PHP - Find key in multidimensional array [duplicate]

This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 3 years ago.
Is there any predefined PHP function to find a key in a multi dimensional array?
In the below example - there is a variable name 'rose' and I need to get the key of the array by using the variable name.
The result of the key is "flower".
$array = array (
'fruits' => array (
'mango',
'cherry'
),
'flowers' => array (
'rose'
)
);
How do I achieve this?
Loop it up using a foreach
$keyword='mango';
foreach($array as $k=>$arr)
{
if(in_array($keyword,$arr))
{
echo $k;break;// "prints" fruits
}
}
Working Demo

How to move the keys of array to the top in PHP [duplicate]

This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
Closed 9 years ago.
I have got an array
$array = array(
0=>'c'
1=>'a',
2=>'b',
3=>'d',
);
Now, i am going to unset the 1 and 2 keys.
unset($array[1]); unset($array[2]);
How to do if i want an array
array(0=>'c', 1=>'d');
Try This:
$array = array_values($array);
Try
unset($array[1]);
unset($array[2]);
$array = array_values($array);
This works because array_values returns the array you gave, but with increasing keys starting at 0.
Have you tired to use something like that ?
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
array_values
$myarray = array_values($myarray);
btw here is a good answer posted

Categories